【发布时间】:2020-05-17 06:40:04
【问题描述】:
目前我们在我们的系统上有一个 mcrypt 实现来加密我们的 PHP 应用程序中的一些 Id。 但是 Mcrypt 现在已被弃用,我必须更换它。
很遗憾,我无法转换所有保存的信息。 解密就够了。
这是我使用的两个函数:
self::$key = '123456';
public static function encrypt($plaintext)
{
$td = mcrypt_module_open('cast-256', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::$key, $iv);
$encrypted_data = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$encoded_64 = base64_encode($encrypted_data);
return trim($encoded_64);
}
和
public static function decrypt($crypttext)
{
$decoded_64 = base64_decode($crypttext);
$td = mcrypt_module_open('cast-256', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::$key, $iv);
$decrypted_data = mdecrypt_generic($td, $decoded_64);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim($decrypted_data);
}
【问题讨论】: