python aes加密/解密不会返回相同的结果 [英] python aes encrypt/decrypt does not return the same results

查看:669
本文介绍了python aes加密/解密不会返回相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码示例不加密/解密操作后返回原始文本,我想弄清楚为什么

 从Crypto.Cipher导入AES 

text =这是纯文本
要使用
它应该是128个字符长,以避免填充,它是拆分
中有新行,如
文件

password =password ........

block = 32
mode = AES.MODE_CBC

enc = AES.new(password,mode)

encrypted = enc.encrypt(text)

print ORIGINAL:+ text

printENCRYPTED:+ str(encrypted)

printDECRYPTED:+ str(enc.decrypt(encrypted))

h2_lin>解决方案

我认为,你需要重置初始化向量( IV ),以获得所需的结果。方法可能是创建一个新的AES对象进行解密:

  enc = AES.new(password,mode)
encrypted = enc.encrypt(text)
printORIGINAL:+ text
printENCRYPTED:+ str(encrypted)
dec = AES.new b $ b printDECRYPTED:+ str(dec.decrypt(encrypted))


below code sample does not return the original text after encrypt/decrypt operation and I am trying to figure it out why

from Crypto.Cipher import AES

text = """This is plain text 
to use.
It should be exqctly 128 characters long to avoid padding and it is split
with new lines as in 
file"""

password = "password........"

block = 32
mode = AES.MODE_CBC

enc = AES.new(password, mode)

encrypted = enc.encrypt(text)

print "ORIGINAL: " + text

print "ENCRYPTED: " + str(encrypted)

print "DECRYPTED: " + str(enc.decrypt(encrypted))

can one tell why first part of the text is malformed ?

解决方案

I think, you need to reset the initialisation vector (IV), in order to get the desired result. The easist way might be to create a new AES object for decrypting:

enc = AES.new(password, mode)
encrypted = enc.encrypt(text)
print "ORIGINAL: " + text
print "ENCRYPTED: " + str(encrypted)
dec = AES.new(password, mode)
print "DECRYPTED: " + str(dec.decrypt(encrypted))

这篇关于python aes加密/解密不会返回相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆