python 3 pycrypto iv 必须是 16 个字节长

2023-12-07

所以我一直在尝试根据 github pycrypto 指南构建一个 AES 加密程序链接到 github但是当我去解码时出现错误:

Traceback (most recent call last):
File "/home/pi/Desktop/aes/newAES.py", line 24, in <module>
    print(decrypt(key,msg,iv))
File "/home/pi/Desktop/aes/newAES.py", line 13, in decrypt
    cipher = AES.new(key,AES.MODE_CFB)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 94, in new
    return AESCipher(key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long

我的代码是:

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
def encrypt(key,msg):
    if key == 0:
        key=get_random_bytes(16)
        print("key: "+key)
    iv = get_random_bytes(16)
    print('iv: '+str(iv))
    cipher = AES.new(key,AES.MODE_CFB,iv)
    ciphertext= cipher.decrypt(msg)
    return("your encrypted message: "+str(ciphertext))
def decrypt(key,ciphertext,iv):
    cipher = AES.new(key,AES.MODE_CFB)
    msg = cipher.decrypt(ciphertext)
ed = input('(e)ncrypt or (d)ecrypt: ')
if ed=='e':
    key = input('16 digit key: ')
    msg = input('message: ')
    print(encrypt(key,msg))
elif ed =='d':
    key = input('16 digit key: ')
    iv = bytes(input('iv: '),'utf-8')
    msg = bytes(input('encrypted message:'),'utf-8')
    print(decrypt(key,msg,iv))

我将不胜感激为解决此问题提供的任何帮助,希望这不是一些愚蠢的错误


问题在于iv是它由随机组成bytes,但它被读入您的程序作为string。呼唤bytes在该字符串上不会执行您所期望的操作。

>>> iv = b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('
>>> siv = str(iv)
>>> siv
"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('"   # Note 'b' is part of the string
>>> biv = bytes(siv, 'utf-8')
>>> biv
b"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('"  # Now there are two 'b's!

您可以使用以下方法解决此问题ast.literal_eval:

>>> ast.literal_eval(siv)
b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('

这是代码的工作版本 - 我不再需要复制/粘贴iv,但有关输入字节的相同观察结果也适用于密文。

import ast

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes


def encrypt(key, msg):
    iv = get_random_bytes(16)
    cipher = AES.new(key, AES.MODE_CFB, iv)
    ciphertext = cipher.encrypt(msg)    # Use the right method here
    return iv + ciphertext


def decrypt(key, ciphertext):
    iv = ciphertext[:16]
    ciphertext = ciphertext[16:]
    cipher = AES.new(key, AES.MODE_CFB, iv)
    msg = cipher.decrypt(ciphertext)
    return msg.decode("utf-8")


if __name__ == "__main__":
    ed = input("(e)ncrypt or (d)ecrypt: ")
    if ed == "e":
        key = input("16 digit key: ")
        msg = input("message: ")
        print("Encrypted message: ", encrypt(key, msg))
    elif ed == "d":
        key = input("16 digit key: ")
        smsg = input("encrypted message: ")
        msg = ast.literal_eval(smsg)
        print("Decrypted message: ", decrypt(key, msg))

正在运行的代码:

(e)ncrypt or (d)ecrypt: e
16 digit key: abcdabcdabcdabcd
message: Spam, spam, spam
Encrypted message:  b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'

(e)ncrypt or (d)ecrypt: d
16 digit key: abcdabcdabcdabcd
encrypted message: b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'
Decrypted message:  Spam, spam, spam
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 3 pycrypto iv 必须是 16 个字节长 的相关文章

随机推荐