问题描述
由于不推荐使用Mcrypt,因此我想在代码中使用OpenSSL,因为我们已经在服务器中使用php 7.2.4.
Since Mcrypt is deprecated, I want to use OpenSSL instead in my code since we already using php 7.2.4 in our server.
我已使用以下代码进行加密/解密.
I have used following code for Encryption/Decryption.
//加密
function encrypt($text, $salt='') {
if ($text == "") return "";
if ($salt == "") $salt = 'DiAo74dOO09T48YESmuvbS0T';
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv
(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
//DECRYPTION
//DECRYPTION
function decrypt($text, $salt = '') {
if ($text == "")
return "";
if ($salt == "")
$salt = 'DiAo74dOO09T48YESmuvbS0T';
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv
(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
"AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU ="我已经使用上述加密功能对该字符串进行了加密,但是我希望它使用OPENSSL对其进行解密.我使用以下代码对其进行解密.
"AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU=" This String I had encrypted using above encryption function, But I want it to decrypt it using OPENSSL. I used following code to decrypt it.
$string = 'AFdT9sa81krHkp/GoYCSwh7/lZn/gLZLHJSldi5/QCU=';
$output = false;
$secret_key = 'DiAo74dOO09T48YESmuvbS0T';
$secret_iv1 = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-ECB'));
$secret_iv = bin2hex($secret_iv1);
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = base64_encode(openssl_encrypt($string, 'aes-256-ecb', $key, OPENSSL_RAW_DATA));
我希望解密输出为:"durhs-14767-w0163j1-89047"预先感谢您的答复.
I want decrypted output as : "durhs-14767-w0163j1-89047"Thanks in advance for your reply.
推荐答案
很遗憾,您的方法有误.
Saddly, you are on the wrong way.
请参阅:
http://php.net/manual/en/function. mcrypt-encrypt.php#117667
https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
因此,您不能使用OpenSSL的AES-256解密MCrypt的输出.
So you can not use OpenSSL's AES-256 to decrypt the MCrypt's output.
一些可能的方法:
-
继续使用PECL的mcrypt扩展名使用mcrypt(幸运的是,它仍然存在),直到您可以完全替换旧数据为止.
Keep using mcrypt by PECL's mcrypt extension (luckily, it is still there), until you can replace the legacy data totally.
用PHP重写正确的RIJNDAEL-256密码.
Rewrite a correct RIJNDAEL-256 cipher in PHP.
这篇关于MCrypt rijndael-256到OpenSSL AES-256-ECB转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!