【问题标题】:How to decrypt unsalted openssl compatible blowfish CBC/PKCS5Padding password in python?如何在python中解密未加盐的openssl兼容河豚CBC/PKCS5Padding密码?
【发布时间】:2012-09-01 21:20:41
【问题描述】:

我一直在寻找一个 python 库来帮助解密 openssl 河豚加密密码。

我已经设法在 Java 中实现了这一点,但支持这一点的 python 库看起来更像是一个学习曲线,需要自己动手。

就我们需要实现的目标而言,密码未加盐并使用密码短语,出于本问题的目的,我将其设置为“AAAAAAA”。密码是“Blowfish/CBC/PKCS5Padding”。加密后的文本将作为字符串读入,与密钥和iv相同。

在 openssl 中,这是“简单”:

~$ # This is encrypting
~$ echo -n 'password' | openssl enc -bf -nosalt -a -K AAAAAAAA -iv AAAAAAAA
eAIUXziwB8QbBexkiIDR3A==
~$ # This is reversing the encryption
~$ echo 'eAIUXziwB8QbBexkiIDR3A==' | openssl enc -d -bf -nosalt -a -K AAAAAAAA -iv AAAAAAAA
password

在java中,解密是这样的:

private static final String KEY = "AAAAAAAA000000000000000000000000";
private static final String IV = "AAAAAAAA00000000";
private static final String FCN = "Blowfish/CBC/PKCS5Padding";
private static final String CN = "Blowfish";

final byte[] encoded = Base64.decode("eAIUXziwB8QbBexkiIDR3A==");
final SecretKeySpec key =
new SecretKeySpec(Hex.decodeHex(KEY.toCharArray()), CN);
final Cipher cipher = Cipher.getInstance(FCN, JCE_PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Hex.decodeHex(IV.toCharArray())));
final byte[] decrypted = cipher.doFinal(encoded);
return new String(decrypted);

有人可以提供一些关于python的指导吗?

【问题讨论】:

    标签: python encryption blowfish


    【解决方案1】:

    内置解码十六进制和base64编码字符串:

    In [1]: "AAAAAAAA000000000000000000000000".decode('hex')
    Out[1]: '\xaa\xaa\xaa\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    
    In [2]: "eAIUXziwB8QbBexkiIDR3A==".decode('base64')
    Out[2]: 'x\x02\x14_8\xb0\x07\xc4\x1b\x05\xecd\x88\x80\xd1\xdc'
    

    PyCrypto 库处理 BlowFish(以及其他)。

    In [1]: from Crypto.Cipher import Blowfish
    
    In [2]: KEY = "AAAAAAAA000000000000000000000000".decode('hex')
    
    In [3]: IV = "AAAAAAAA00000000".decode('hex')
    
    In [4]: cipher = Blowfish.new(KEY, Blowfish.MODE_CBC, IV)
    
    In [5]: ciphertext = "eAIUXziwB8QbBexkiIDR3A==".decode('base64')
    
    In [6]: cipher.decrypt(ciphertext)
    Out[6]: 'password\x08\x08\x08\x08\x08\x08\x08\x08'
    

    如果你想一次性从明文中去除填充:

    In [14]: cipher.decrypt(ciphertext).replace('\x08', '')
    Out[14]: 'password'
    

    【讨论】:

    • 完美运行,谢谢。我曾尝试过该库,但显然错过了 hex/base64 之间的适当转换。
    • 不客气!顺便说一句,您应该使用盐。不使用盐可以对密码执行有效的字典攻击。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多