如何将PHP中mcrypt_encrypt函数升级为openssl_encrypt函数,实现加密功能迁移?

2026-04-01 06:151阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计144个文字,预计阅读时间需要1分钟。

如何将PHP中mcrypt_encrypt函数升级为openssl_encrypt函数,实现加密功能迁移?

PHP中从mcrypt_encrypt升级到openssl_encrypt的原因:PHP 8中已弃用mcrypt_encrypt。以下是具体测试代码:

php$account=[ 'encodeKey'=> '0f5264038205EDFB1AC05fbb0e8c5e94', 'ivKey'=> '0f5264038205EDFB', 'password'=> 'testpwd'];

如何将PHP中mcrypt_encrypt函数升级为openssl_encrypt函数,实现加密功能迁移?

php中mcrypt_encrypt升级到openssl_encrypt

原因: php8中已经弃用 mcrypt_encrypt

详细测试代码

$account= [ 'encodeKey' => '0f5264038205EDFB1AC05fbb0e8c5e94', 'ivKey' => '0f5264038205EDFB', 'password' => 'testpwd', //登陆密码 ]; // 历史代码, 使用mcrypt_encrypt加密数据 echo $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $account['encodeKey'], $account['password'], MCRYPT_MODE_CBC, $account['ivKey'])); echo '<hr>'; // error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:data not multiple of block length // 要加密的数据长度不是密钥长度的倍数 $pwd = $account['password']; while (strlen($pwd)%16 != 0){ $pwd = $pwd."\x0"; } // 使用openssl_encrypt加密base64编码后的数据 $encrypted_openssl = openssl_encrypt($pwd, 'AES-256-CBC', $account['encodeKey'], OPENSSL_ZERO_PADDING, $account['ivKey']); // var_dump('error: '.openssl_error_string()); echo $encrypted_openssl;

本文共计144个文字,预计阅读时间需要1分钟。

如何将PHP中mcrypt_encrypt函数升级为openssl_encrypt函数,实现加密功能迁移?

PHP中从mcrypt_encrypt升级到openssl_encrypt的原因:PHP 8中已弃用mcrypt_encrypt。以下是具体测试代码:

php$account=[ 'encodeKey'=> '0f5264038205EDFB1AC05fbb0e8c5e94', 'ivKey'=> '0f5264038205EDFB', 'password'=> 'testpwd'];

如何将PHP中mcrypt_encrypt函数升级为openssl_encrypt函数,实现加密功能迁移?

php中mcrypt_encrypt升级到openssl_encrypt

原因: php8中已经弃用 mcrypt_encrypt

详细测试代码

$account= [ 'encodeKey' => '0f5264038205EDFB1AC05fbb0e8c5e94', 'ivKey' => '0f5264038205EDFB', 'password' => 'testpwd', //登陆密码 ]; // 历史代码, 使用mcrypt_encrypt加密数据 echo $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $account['encodeKey'], $account['password'], MCRYPT_MODE_CBC, $account['ivKey'])); echo '<hr>'; // error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:data not multiple of block length // 要加密的数据长度不是密钥长度的倍数 $pwd = $account['password']; while (strlen($pwd)%16 != 0){ $pwd = $pwd."\x0"; } // 使用openssl_encrypt加密base64编码后的数据 $encrypted_openssl = openssl_encrypt($pwd, 'AES-256-CBC', $account['encodeKey'], OPENSSL_ZERO_PADDING, $account['ivKey']); // var_dump('error: '.openssl_error_string()); echo $encrypted_openssl;