【问题标题】:Python 2 - Decrypt Java's PBEWithMD5AndDESPython 2 - 解密 Java 的 PBEWithMD5AndDES
【发布时间】: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


    【解决方案1】:

    这样做

        decrypted = encoder2.decrypt(encrypted)
        print decrypted.rstrip('\2,\1,\3,\4,\5,\6,\7')
    

    “123456”的长度为6,解密器输出8字节。剩余的位置用默认字节填充,因此rstrip 剥离字节并给出字符串。

    编辑:我创建了一个要点参考这个 Link

    【讨论】:

    • "123456" 的长度为 6,解密器输出 8 个字节,剩余位置用默认字节填充,因此 rstrip 剥离字节并给出字符串
    【解决方案2】:

    只取最后一个字节并删除它编码的尽可能多的字符(包括最后一个字节)。

    请注意,如果您容易受到错误预言(例如填充预言)的影响,则应添加身份验证标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多