Function mcrypt_get_block_size() is deprecated
解決方案a:
php7.1以上. mcrypt_generic_open is deprecated 這個錯誤,
就是因?yàn)閙crypt擴(kuò)展,在php7.1以上被廢棄,服務(wù)器不設(shè)置報錯等級的話,
這個錯誤會被框架攔截,然后報出微信demo里的40007錯誤,
具體解決方案,所有 mcrypt擴(kuò)展的代碼,全部加上 錯誤抑制符 ,例如
//使用BASE64對需要解密的字符串進(jìn)行解碼
代碼
? ? ? ? ? ? $ciphertext_dec = base64_decode($encrypted);
? ? ? ? ? ? $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
? ? ? ? ? ? $iv = substr($this->key, 0, 16);
? ? ? ? ? ? mcrypt_generic_init($module, $this->key, $iv);
? ? ? ? ? ? //解密
? ? ? ? ? ? $decrypted = mdecrypt_generic($module, $ciphertext_dec);
? ? ? ? ? ? mcrypt_generic_deinit($module);
? ? ? ? ? ? mcrypt_module_close($module);
修改的代碼
? ? ? ? ? ? $ciphertext_dec = base64_decode($encrypted);
? ? ? ? ? ? @$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
? ? ? ? ? ? $iv = substr($this->key, 0, 16);
? ? ? ? ? ? @mcrypt_generic_init($module, $this->key, $iv);
? ? ? ? ? ? //解密
? ? ? ? ? ? @$decrypted = mdecrypt_generic($module, $ciphertext_dec);
? ? ? ? ? ? @mcrypt_generic_deinit($module);
? ? ? ? ? ? @mcrypt_module_close($module);
然后我的問題就解決了。
我的問題主要出在微信的加解密函數(shù)上,遇到的坑有幾個
1.文檔給的class默認(rèn)有的是小寫,
2.一個文件有多個class得拆開
3.構(gòu)造函數(shù)獲取的變量名稱不對得修改…
這些用斷點(diǎn)調(diào)試都可以進(jìn)行解決
解決方案b:
mcrypt十年過去,現(xiàn)在php7+中已經(jīng)開始淘汰。官方給出掉提示:
mcrypt_get_block_size — 獲得加密算法的分組大小
Warning
This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.
在php7中需要openssl替代,這里需要注意的是:
在mcrypt中對加密key長度沒有限制要求,傳入多少長度都會參加加密,但是在openssl_encrypt中。key長度只能是16長度,>16長度后,簽名結(jié)果保持不變,這里是哥坑,在替代方案測試時候容易出錯,具體直接上代碼對比:
低于php7版本代碼
class AES {
? ? public static function encrypt($input,$key) {
? ? ? ? $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
? ? ? ? $paddedData = static::pkcs5_pad($input, $blockSize);
? ? ? ? $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
? ? ? ? $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
? ? ? ? $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $paddedData, MCRYPT_MODE_ECB, $iv);
? ? ? ? return bin2hex($encrypted);
? ? }
? ? private static function pkcs5_pad ($text, $blocksize) {
? ? ? ? $pad = $blocksize - (strlen($text) % $blocksize);
? ? ? ? return $text . str_repeat(chr($pad), $pad);
? ? }
? ? public static function decrypt($sStr,$key) {
? ? ? ? $decrypted= mcrypt_decrypt(
? ? ? ? ? ? MCRYPT_RIJNDAEL_128,
? ? ? ? ? ? $key,
? ? ? ? ? ? hex2bin($sStr),
? ? ? ? ? ? MCRYPT_MODE_ECB
? ? ? ? ? ? );
? ? ? ? $dec_s = strlen($decrypted);
? ? ? ? $padding = ord($decrypted[$dec_s-1]);
? ? ? ? $decrypted = substr($decrypted, 0, -$padding);
? ? ? ? return $decrypted;
? ? }
? ? /**
? ? *url 安全的base64編碼 sunlonglong
? ? */
? ? function base64url_encode($data) {
? ? ? ? return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
? ? }
? ? /**
? ? *url 安全的base64解碼 sunlonglong
? ? */
? ? function base64url_decode($data) {
? ? ? ? return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
? ? }
}
$key = 'g87y65ki6e8p93av8zjdrtfdrtgdwetd';
$encrypt = AES::encrypt('123abc',$key);
$decrypt = AES::decrypt($encrypt,$key);
var_dump($encrypt,$decrypt);
加密結(jié)果:
? ? da07f6363eb0024b4bdd264e5fd4a2f5
下面是php7以上。使用openssl加密:
class AES {
? ? /**
? ? *
? ? * @param string $string 需要加密的字符串
? ? * @param string $key 密鑰
? ? * @return string
? ? */
? ? public static function encrypt($string, $key)
? ? {
? ? ? ? // openssl_encrypt 加密不同Mcrypt,對秘鑰長度要求,超出16加密結(jié)果不變
? ? ? ? $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
? ? ? ? $data = strtolower(bin2hex($data));
? ? ? ? return $data;
? ? }
? ? /**
? ? * @param string $string 需要解密的字符串
? ? * @param string $key 密鑰
? ? * @return string
? ? */
? ? public static function decrypt($string, $key)
? ? {
? ? ? ? $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
? ? ? ? return $decrypted;
? ? }
}
$encrypt = AES::encrypt('123abc', 'g87y65ki6e8p93av8zjdrtfdrtgdwetd');
$decrypt = AES::decrypt($encrypt, 'g87y65ki6e8p93av8zjdrtfdrtgdwetd');
var_dump($encrypt,$decrypt);die;
加密結(jié)果:
8c927c42f93a83c5de763aa18e4e6c7d
雖然key長度32位,但是openssl_encrypt加密時候,key長度只使用了16長度,后面未參加簽名,而mcrypt_encrypt會整個key參與加密,這樣就會出現(xiàn)加密出來對結(jié)果不一致。造成困惑,key=g87y65ki6e8p93av8zjdrtfdrtgdwetd與key=g87y65ki6e8p93av結(jié)果都是一致對;
原文:https://blog.csdn.net/qq_38686693/article/details/81388329
原文:https://blog.csdn.net/ranlv91/article/details/81916393