【问题标题】:openssl password decryption using Python使用 Python 解密 openssl 密码
【发布时间】:2016-04-15 17:27:34
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 为什么要这样做?加密这样的密码就像建造一座城堡,然后雇用一个小男孩来吓唬那些通过吊桥进入的人,并说服他们在攻击时保持开放......
  • 您永远不应该加密您的用户密码。您需要使用散列代替一些强大的散列,例如 PBKDF2、bcrypt、scrypt 和 Argon2。由于散列函数是单向函数,因此您将无法“解密”散列。为了验证您的用户,您可以再次通过哈希函数运行密码,以便与存储在数据库中的哈希值进行比较。查看更多:How to securely hash passwords?

标签: php python encryption openssl encryption-symmetric


【解决方案1】:

终于找到了一个解决方案......下面的代码似乎正在工作,我确信有一种更有效的方法可以做到这一点,但现在它正在做我需要它做的事情......希望这会对某人有所帮助否则在未来。请记住,这不是保护数据的安全方式!

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

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2015-10-09
    • 2011-12-13
    相关资源
    最近更新 更多