AES:输入字符串的长度必须是 16 的倍数

新手上路,请多包涵

我想制作一个脚本来解密我的文件,但是当我尝试运行我的脚本然后显示这条消息时,我该如何修复它?

追溯(最近调用最后):文件“F:\bug_bounty\decrypt.py”,第 46 行,在文件“F:\bug_bounty\decrypt.py”,第 24 行,解密文件“C:\Python27\lib\ site-packages\Crypto\Cipher\blockalgo.py”, line 295, in decrypt return self._cipher.decrypt(ciphertext) ValueError: 输入字符串的长度必须是 16 的倍数

from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import os
import random
import sys

def decrypt(key, filename):
    outFile = os.path.join(os.path.dirname(filename),
                           os.path.basename(filename[11:]))
    chunksize = 64 * 1024
    with open(filename, 'rb') as infile:
        filesize = infile.read(16)
        IV = infile.read(16)

        decryptor = AES.new(key, AES.MODE_CBC, IV)

        with open(outFile, 'wb') as outfile:
            while True:
                chunk = infile.read(chunksize)
                if len(chunk) == 0:
                    break

                outfile.write(decryptor.decrypt(chunk))

            outfile.truncate(int(filesize))

def allfiles():
    allFiles = []
    for (root, subfiles, files) in os.walk(os.getcwd()):
        for names in files:
            allFiles.append(os.path.join(root, names))

    return allFiles

password = 'M4st3rRul3zs'
files = allfiles();
for filename in files:
    if os.path.basename(filename).startswith("(encrypted)"):
       print "%s is already encrypted" %filename
       pass

    else:
        decrypt(SHA256.new(password).digest(), filename)
        print "Done decrypting %s" %filename
        """os.remove(filename)"""

原文由 Aron Imperial 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 489
1 个回答

如果您的数据量 不大,这里是您可以在加密数据时使用的小技巧。

 plaintext = "some text"
encryptor = AES.new(key, AES.MODE_CBC, iv)
ciphertext = encryptor.encrypt(plaintext*16)

这将确保您输入的数据是 16 的倍数。当然,您希望在解密时取回原始数据。

 cipher = AES.new(key, AES.MODE_CBC, iv)
decrypttext = cipher.decrypt(ciphertext)
decrypttext = decrypttext[0:len(plaintext)]

现在, decrpyttext 有了你的原始明文。

原文由 Mayank Pathela 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题