PyCrypto 解密混乱
PyCrypto Decryption Mess
所以我现在正在尝试在 Python 中制作一个简单的 AES encrypt/decrypt 系统...但是当它解密时它前面有一堆 /xxx/xxx/xxx/xxx/解密后的字符串。如何清理它并使其仅打印明文。
我的代码是:
import base64
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn...')
print (msg)
print (base64.b64encode(msg))
print (cipher.decrypt(msg))
decrypt
的输出如下所示:
b'\xfb\xb8\xf0\xc3\xffH\xfc~\x19[\xecy?\xf8\xcc\x80Attack at dawn...'
docs状态:
That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.
这有点神秘,但似乎意味着您不能重复使用 cipher
来解密。另外,我不确定您为什么将 iv
连接到您的加密邮件。
下面的代码对我来说很好用:
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
c = AES.new(key, AES.MODE_CFB, iv)
msg = c.encrypt('Attack at dawn...')
d = AES.new(key, AES.MODE_CFB, iv)
d.decrypt(msg)
初始化向量 (IV) 是加密消息 (msg
) 的一部分,但密文本身应包含 IV。这意味着您必须在解密之前删除 IV,即像这样:
cipher.decrypt(msg[16:])
下一个问题是您不应该使用相同的 AES
实例进行加密和解密。 AES
实例包含无法轻易刷新的内部缓冲区。
key = b'Sixteen byte key'
# encryption
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn...')
print (msg)
print (base64.b64encode(msg))
# decryption
cipher = AES.new(key, AES.MODE_CFB, msg[:16])
print (cipher.decrypt(msg[16:]))
However when it decrypts it has a bunch of /xxx/xxx/xxx/xxx/ in front of the decrypted string.
您真幸运,在最后看到了解密后的字符串。这只是因为 IV 被添加到消息和 CFB 操作模式的内部工作原理之前。如果您使用 CTR 模式,这看起来会大不相同。
所以我现在正在尝试在 Python 中制作一个简单的 AES encrypt/decrypt 系统...但是当它解密时它前面有一堆 /xxx/xxx/xxx/xxx/解密后的字符串。如何清理它并使其仅打印明文。
我的代码是:
import base64
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn...')
print (msg)
print (base64.b64encode(msg))
print (cipher.decrypt(msg))
decrypt
的输出如下所示:
b'\xfb\xb8\xf0\xc3\xffH\xfc~\x19[\xecy?\xf8\xcc\x80Attack at dawn...'
docs状态:
That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.
这有点神秘,但似乎意味着您不能重复使用 cipher
来解密。另外,我不确定您为什么将 iv
连接到您的加密邮件。
下面的代码对我来说很好用:
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
c = AES.new(key, AES.MODE_CFB, iv)
msg = c.encrypt('Attack at dawn...')
d = AES.new(key, AES.MODE_CFB, iv)
d.decrypt(msg)
初始化向量 (IV) 是加密消息 (msg
) 的一部分,但密文本身应包含 IV。这意味着您必须在解密之前删除 IV,即像这样:
cipher.decrypt(msg[16:])
下一个问题是您不应该使用相同的 AES
实例进行加密和解密。 AES
实例包含无法轻易刷新的内部缓冲区。
key = b'Sixteen byte key'
# encryption
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn...')
print (msg)
print (base64.b64encode(msg))
# decryption
cipher = AES.new(key, AES.MODE_CFB, msg[:16])
print (cipher.decrypt(msg[16:]))
However when it decrypts it has a bunch of /xxx/xxx/xxx/xxx/ in front of the decrypted string.
您真幸运,在最后看到了解密后的字符串。这只是因为 IV 被添加到消息和 CFB 操作模式的内部工作原理之前。如果您使用 CTR 模式,这看起来会大不相同。