使用Python进行openssl密码解密


openssl password decryption using Python

我使用以下PHP代码加密密码,然后将其保存到数据库中,我需要能够使用Python对其进行解密。

我成功地使用PHP解密了它,但无法找到使用Python(如果重要的话,我使用的是2.7.9版本)。。

$mypass = "somepassword"
$encryptionMethod = "AES-256-CBC";  
$secretHash = "25c6c78835b7479b151f2136cd888777";
$encpass = openssl_encrypt($mypass, $encryptionMethod, $secretHash);

我打开和读取数据库没有问题,我唯一的问题是解密部分。欢迎提出任何建议,谢谢。

终于找到了解决方案。。。下面的代码似乎在起作用,我相信有一种更有效的方法可以做到这一点,但目前它正在做我需要它做的事情…希望这能在未来帮助其他人。请记住,这不是保护数据的安全方法!

#!/usr/bin/python
from Crypto.Cipher import AES
import base64
import os
def decryption(encryptedString):
    PADDING = '{'
    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
    #Key is FROM the printout of 'secret' in encryption
    #below is the encryption.
    encryption = encryptedString
    key = "25c6c78835b7479b151f2136cd888777"
    cipher = AES.new(key)
    decoded = DecodeAES(cipher, encryption)
    #print decoded
    return decoded
enc_message = "p7OslgBJ5RlTBDG4ZD8HEA"  # in my case it will be the data from the database
enc_message = enc_message + "=="
data = decryption(enc_message)
data = data.rstrip()
print data