Php 与Mcrypt相比,使用OpenSSL进行加密/解密会导致不同的结果
我们用OpenSSL替换了Mcrypt,用于加密和解密数据库中存储的密码。以下是我们正在使用的新功能:Php 与Mcrypt相比,使用OpenSSL进行加密/解密会导致不同的结果,php,encryption,openssl,mcrypt,Php,Encryption,Openssl,Mcrypt,我们用OpenSSL替换了Mcrypt,用于加密和解密数据库中存储的密码。以下是我们正在使用的新功能: function encrypt_openssl_password($key, $pwd) { $cipher="AES-256-CBC"; $ivlen = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes( $ivlen ); $encrypted = base64_en
function encrypt_openssl_password($key, $pwd)
{
$cipher="AES-256-CBC";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes( $ivlen );
$encrypted = base64_encode(
$iv .
openssl_encrypt(
$pwd,
$cipher,
hash('sha256', $key, true),
OPENSSL_RAW_DATA,
$iv
)
);
return $encrypted;
}
function decrypt_openssl_password($key, $str)
{
$data = base64_decode($str);
$cipher="AES-256-CBC";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = substr($data, 0, $ivlen);
$decrypted = rtrim(
openssl_decrypt(
base64_encode(substr($data, $ivlen)),
$cipher,
hash('sha256', $key, true),
OPENSSL_ZERO_PADDING,
$iv),
"\0"
);
return $decrypted;
}
问题:
当我们解密旧密码(使用Mcrypt加密)时,使用新的OpenSSL函数返回正确的值string(12)“password2019”
当我们现在使用新的OpenSSL函数存储完全相同的密码时,我们收到了一个错误的值string(16)“password2019”
由于旧密码的解密工作正常,我们假设
encrypt\u openssl\u password
函数中存在故障。有什么想法吗?你在其中引入了一些多字节字符集吗?解密后的字符串如何在解密中添加4个04
字节,如果这对主题有帮助的话。你为什么不使用PHP内置的密码处理?密码永远不应该,甚至一次都不应该是可解密的。不。娜达。废话。永远不要使用PHP来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用密码\u hash()
。在散列之前,不需要对它们执行任何操作或使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。^^^^实际上,这是目前为止最重要的一点。^^^好吧,还有你之前的评论:)