Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pycryptodecryption-Mess_Python_Python 3.x_Encryption_Cryptography_Pycrypto - Fatal编程技术网

Python pycryptodecryption-Mess

Python pycryptodecryption-Mess,python,python-3.x,encryption,cryptography,pycrypto,Python,Python 3.x,Encryption,Cryptography,Pycrypto,所以我现在正试图用Python制作一个简单的AES加密/解密系统。。。但是,当它解密时,在解密的字符串前面有一堆/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,

所以我现在正试图用Python制作一个简单的AES加密/解密系统。。。但是,当它解密时,在解密的字符串前面有一堆/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...'
cipher.decrypt(msg[16:])
国家:

这也意味着您不能重用对象来加密或解密具有相同密钥的其他数据

这有点神秘,但if似乎意味着不能重用
密码
来解密。另外,我不知道为什么要将
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,即如下所示:

b'\xfb\xb8\xf0\xc3\xffH\xfc~\x19[\xecy?\xf8\xcc\x80Attack at dawn...'
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:]))
但是,当它解密时,在解密的字符串前面有一堆/xxx/xxx/xxx/


幸运的是,您在最后看到了解密的字符串。这仅仅是因为IV在消息和CFB操作模式的内部工作之前。如果您使用的是CTR模式,则看起来会有很大不同。

您可以共享您获得的输出吗?请参阅。IV在加密数据前加上前缀,因此可以对其进行解密,它不需要保密。只有加密需要共享,并且可以多次使用。但是对于使用相同密钥的每个加密,IV应该是唯一的,因此它与加密数据一起提供。