我有一个代码,我做了,但出了问题。我需要encrypty一些文件夹,以获得更多的安全在我的业务,我正在使用的AES脚本在Python。它的生成2个文件,encrypt.py和解密。加密工作很好,但当我执行解密,它给予我一个错误。
下面是我的代码:
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
backend = default_backend()
key = os.urandom(32) # 256 bit
iv = os.urandom(16) # 128 bit
# cryptografy
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
# loop all files and folders
for root, dirs, files in os.walk('.'):
# ignores main.py (that is the file name to not encrypt it)
if 'main.py' in files:
continue
# crypt files
for file in files:
# create a new encryptor
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
# read datas
file_path = os.path.join(root, file)
data = open(file_path, 'rb').read()
# adding padding
padder = padding.PKCS7(128).padder()
padded_data = padder.update(data) + padder.finalize()
# encrypts the data
cipher_data = encryptor.update(padded_data) + encryptor.finalize()
# writes the encrypted data to the file
file = open(file_path, 'wb')
file.write(cipher_data)
file.close()
# write the code for decryption in the file
decryptor_file = open('decryptor.py', 'w')
decryptor_file.write("import os\n")
decryptor_file.write("from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes\n")
decryptor_file.write("from cryptography.hazmat.backends import default_backend\n")
decryptor_file.write("from cryptography.hazmat.primitives import padding\n\n")
decryptor_file.write("backend = default_backend()\n")
decryptor_file.write("key = \"" + key.hex() + "\"\n")
decryptor_file.write("iv = \"" + iv.hex() + "\"\n\n")
decryptor_file.write("# descryptografy\n")
decryptor_file.write("cipher = Cipher(algorithms.AES(bytes.fromhex(key)), modes.CBC(bytes.fromhex(iv)), backend=backend)\n")
decryptor_file.write("decryptor = cipher.decryptor()\n\n")
decryptor_file.write("# loops all files and folders\n")
decryptor_file.write("for root, dirs, files in os.walk('.'):\n")
decryptor_file.write(" # decrypt files\n")
decryptor_file.write(" for file in files:\n")
decryptor_file.write(" # data reading\n")
decryptor_file.write(" file_path = os.path.join(root, file)\n")
decryptor_file.write(" data = open(file_path, 'rb').read()\n\n")
decryptor_file.write(" # decrypt data \n")
decryptor_file.write(" decrypted_data = decryptor.update(data) + decryptor.finalize()\n\n")
decryptor_file.write(" # remove padding\n")
decryptor_file.write(" unpadder = padding.PKCS7(128).unpadder()\n")
decryptor_file.write(" unpadded_data = unpadder.update(decrypted_data) + unpadder.finalize()\n\n")
decryptor_file.write(" # writes the data descrypted in the file\n")
decryptor_file.write(" file = open(file_path, 'wb')\n")
decryptor_file.write(" file.write(unpadded_data)\n")
decryptor_file.write(" file.close()\n\n")
decryptor_file.close()
# shows a alert message
print('All files and folders have been encrypted and the decrypt file has been created!')
我得到的错误:
Traceback (most recent call last):
File "C:\Users\User\Desktop\cryptor\decryptor.py", line 23, in <module>
decrypted_data = decryptor.update(data) + decryptor.finalize()
File "C:\Program Files\Python38\lib\site-packages\cryptography\hazmat\primitives\ciphers\base.py", line 186, in finalize
data = self._ctx.finalize()
File "C:\Program Files\Python38\lib\site-packages\cryptography\hazmat\backends\openssl\ciphers.py", line 223, in finalize
raise ValueError(
ValueError: The length of the provided data is not a multiple of the block length.
一些帮助是感激的!
1条答案
按热度按时间cetgtptt1#
它对我很有效,我只需要在 decryptor.py 文件中的
for
循环(for file in files:
)中移动下面的行,就像在 encryptor.py 中所做的那样,否则会触发另一个错误:有了这个变化,一切都很好!
重现错误的唯一方法是在尝试解密已解密文件时,在这种情况下,填充与不匹配。是否可能是错误操作?