【发布时间】:2014-10-02 12:40:57
【问题描述】:
这个问题和这个有关:Replicate Java's PBEWithMD5AndDES in Python 2.7
这是问题的答案(Python代码):
from Crypto.Hash import MD5
from Crypto.Cipher import DES
_password = 'q1w2e3r4t5y6'
_salt = '\x80\x40\xe0\x10\xf8\x04\xfe\x01'
_iterations = 50
if "__main__" == __name__:
"""Mimic Java's PBEWithMD5AndDES algorithm to produce a DES key"""
print "Enter the password to encrypt:",
plaintext_to_encrypt = raw_input()
hasher = MD5.new()
hasher.update(_password)
hasher.update(_salt)
result = hasher.digest()
for i in range(1, _iterations):
hasher = MD5.new()
hasher.update(result)
result = hasher.digest()
# Pad plaintext per RFC 2898 Section 6.1
padding = 8 - len(plaintext_to_encrypt) % 8
plaintext_to_encrypt += chr(padding) * padding
encoder = DES.new(result[:8], DES.MODE_CBC, result[8:16])
encrypted = encoder.encrypt(plaintext_to_encrypt)
print encrypted.encode('base64')
我现在正尝试在知道 _password、_salt 和 _iterations 的情况下进行反向操作(decrypt) em> 当然是变量。
print encoder.decrypt(encrypted) 与初始密码不匹配。
我不知道下一步该做什么。我阅读了rfc2898 的§6.1.2,但它对我没有帮助。谁能指导我正确答案?
编辑:
似乎需要以下内容:
encoder2 = DES.new(result[:8], DES.MODE_CBC, result[8:16])
print encoder2.decrypt(encrypted)
为什么我必须再次使用DES.new()?我怎样才能摆脱填充?
“123456”的实际解密输出为123456☻☻
【问题讨论】:
标签: python python-2.7 encryption md5 des