php – MCrypt rijndael-256到OpenSSL aes-256-ecb转换
作者:互联网
由于Mcrypt已被弃用,我想在我的代码中使用OpenSSL,因为我们已经在服务器中使用了php 7.2.4.
我使用以下代码进行加密/解密.
//加密
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))));
}
//解密的
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 / gLZLHJSdi5 / QCU =”这个字符串我用上面的加密函数加密了,但是我希望它用OPENSSL解密它.我使用以下代码来解密它.
$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”
在此先感谢您的回复.
解决方法:
可悲的是,你走错了路.
参考:
http://php.net/manual/en/function.mcrypt-encrypt.php#117667
MCRYPT_RIJNDAEL_256 is not AES-256, it’s a different variant of the
Rijndael block cipher.
https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
AES is a variant of Rijndael which has a fixed block size of 128 bits,
and a key size of 128, 192, or 256 bits. By contrast, the Rijndael
specification per se is specified with block and key sizes that may be
any multiple of 32 bits, with a minimum of 128 and a maximum of 256
bits.
因此,您无法使用OpenSSL的AES-256来解密MCrypt的输出.
一些可能的方法:
>通过PECL的mcrypt扩展继续使用mcrypt(幸运的是,它仍然存在),直到你可以完全替换旧数据.
>用PHP重写一个正确的RIJNDAEL-256密码.
标签:php,openssl,php-7-2 来源: https://codeday.me/bug/20190701/1347190.html