使用Python解密openssl密码

2024-10-02 18:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的PHP代码来加密密码,然后将其保存到数据库中,我需要能够使用Python解密它。在

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

$mypass = "somepassword"
$encryptionMethod = "AES-256-CBC";  
$secretHash = "25c6c78835b7479b151f2136cd888777";

$encpass = openssl_encrypt($mypass, $encryptionMethod, $secretHash);

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


Tags: 代码数据库密码建议encryptaesphpcbc
1条回答
网友
1楼 · 发布于 2024-10-02 18:19:06

终于找到了解决办法。。。下面的代码似乎是有效的,我相信有一个更有效的方法来做到这一点,但现在它正在做我需要它做的事情。。。希望这能在将来帮助别人。请记住,这不是保护数据的安全方法!在

#!/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

相关问题 更多 >