2017-08-15 103 views
0

我写在使用AES-256位加密安全的聊天程序,昨天我在AES,在下面的工作测试不同的方法,我的计划将允许用户设置加密键并且程序将增加或减少,但是需要许多字符才能具有32字节的键。现在,当我救下面的代码昨天它的工作,但是,今天当我通过终端我得到这个无效的输出运行,但是当我从昨天运行的程序,它给了我正确的输出!任何人都将能够帮助谁,那将是巨大AES加密

>>> import Crypto 
>>> from Crypto.Cipher import AES 
>>> iv = 'xxxxxxxxxxxxxxxx' 
>>> key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
>>> cipher = AES.new(key, AES.MODE_CFB, iv) 
>>> cipher.encrypt('Hello') 
'c\x0f\x81\xc4\xde' 
>>> cipher.decrypt('c\x0f\x81\xc4\xde') 
'\x88\xd4;YR' 
+1

重新初始化IV。 –

+0

对于使用相同密钥的每次加密,IV必须不同(读取:随机)。不要使用静态IV,因为这会使密码具有确定性,并允许攻击者在观察到多个密文时推断明文。这就是所谓的许多时间垫(或[两次垫(https://twitter.com/angealbertini/status/425561082841690112/photo/1))。 IV不是秘密的,所以你可以把它和密文一起发送。通常,它只是在密文前面加上,然后在解密之前切掉。 –

+0

适当的建议是甚至不要使用AES。 –

回答

0

这是因为重复使用AES状态和AES状态现在正突变。您需要创建一个新状态:

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
>>> from Crypto.Cipher import AES 
>>> iv = 'xxxxxxxxxxxxxxxx' 
>>> iv = b'xxxxxxxxxxxxxxxx' 
>>> key = b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
>>> cipher = AES.new(key, AES.MODE_CFB, iv) 
>>> cipher.encrypt('Hello') 
b'c\x0f\x81\xc4\xde' 
>>> cipher.decrypt(b'c\x0f\x81\xc4\xde') 
b'\x88\xd4;YR' 

不要重复使用状态,而是重新创建一个新的从IV初始化的状态。

>>> cipher = AES.new(key, AES.MODE_CFB, iv) 
>>> cipher.decrypt(b'c\x0f\x81\xc4\xde') 
b'Hello' 
+0

谢谢!这工作完美。 –