脚本宝典收集整理的这篇文章主要介绍了php – Mcrypt弃用后如何解密?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
无论如何我都可以通过回到旧版本的PHP来解密数据.
这是我使用的代码:
public function encrypt($plaintext) { $ivSize = mcrypt_get_iv_size(self::CIPHER,self::MODE); $iv = mcrypt_create_iv($ivSize,MCRYPT_DEV_URANDOM); $ciphertext = mcrypt_encrypt(self::CIPHER,$this->key,$plaintext,self::MODE,$iv); return base64_encode($iv.$ciphertext); } public function decrypt($ciphertext) { $ciphertext = base64_decode($ciphertext); $ivSize = mcrypt_get_iv_size(self::CIPHER,self::MODE); if (strlen($ciphertext) < $ivSize) { throw new Exception('Missing inITialization vector'); } $iv = substr($ciphertext,$ivSize); $ciphertext = substr($ciphertext,$ivSize); $plaintext = mcrypt_decrypt(self::CIPHER,$ciphertext,$iv); return rtrim($plaintext,"\0"); }
使用常量:
const CIPHER = MCRYPT_RIJNDAEL_128; // Rijndael-128 is AES const MODE = MCRYPT_MODE_CBC;
我看到建议使用OPEnSSL.这是我将从现在开始使用的.但是如何使用此方法解密旧数据?
谢谢
编辑:
我知道我可以使用OpenSSL作为替代方案.
这就是我从现在开始为内容做的事情.
但是我需要从旧内容中解密我的加密代码.
*编辑请求@symcbean
尝试用OpenSSL解密如下:
public function decrypt($ciphertext) { $ciphertext = base64_decode($ciphertext); if (!function_exists("openssl_decrypt")) { throw new Exception("aesDecrypt needs openssl PHP module."); } $key = $this->key; $method = 'AES-256-CBC'; $ivSize = openssl_cipher_iv_length($method); $iv = substr($ciphertext,$ivSize); $data = substr($ciphertext,$ivSize); $clear = openssl_decrypt ($data,$method,$key,'OPENSSL_RAW_DATA'|'OPENSSL_ZERO_PADDING',$iv); return $clear; }
mcrypt_encrypt
零填充输入数据.如果数据本身具有尾随零,则会导致模糊结果.
openssl_decrypt
不会自动删除零填充,因此您只能留下修剪尾随空值的可能性.
$data = "Lorem ipsum"; $key = "1234567890abcdef"; $iv = "1234567890abcdef"; $encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_128,$data,MCRYPT_MODE_CBC,$iv); echo bin2hex($encrypted) . "\n"; $decrypted = openssl_decrypt( $encrypted,"AES-128-CBC",OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,$iv); echo var_export($decrypted,true) . "\n"; $result = rtrim($decrypted,"\0"); echo VAR_export($result,true) . "\n";
输出:
70168f2d5751b3d3bf36b7e6b8ec5843 'Lorem ipsum' . "\0" . '' . "\0" . '' . "\0" . '' . "\0" . '' . "\0" . '' 'Lorem ipsum'
以上是脚本宝典为你收集整理的php – Mcrypt弃用后如何解密?全部内容,希望文章能够帮你解决php – Mcrypt弃用后如何解密?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。