Function mcrypt_get_block_size() is deprecated
解決方案a:
php7.1以上. mcrypt_generic_open is deprecated 這個錯誤什乙,
就是因為mcrypt擴展,在php7.1以上被廢棄,服務器不設置報錯等級的話,
這個錯誤會被框架攔截忌穿,然后報出微信demo里的40007錯誤惶凝,
具體解決方案糙申,所有 mcrypt擴展的代碼今魔,全部加上 錯誤抑制符 ,例如
//使用BASE64對需要解密的字符串進行解碼
代碼
? ? ? ? ? ? $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默認有的是小寫滔驾,
2.一個文件有多個class得拆開
3.構造函數(shù)獲取的變量名稱不對得修改…
這些用斷點調試都可以進行解決
解決方案b:
mcrypt十年過去谒麦,現(xiàn)在php7+中已經開始淘汰。官方給出掉提示:
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長度后,簽名結果保持不變胞此,這里是哥坑臣咖,在替代方案測試時候容易出錯,具體直接上代碼對比:
低于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);
加密結果:
? ? da07f6363eb0024b4bdd264e5fd4a2f5
下面是php7以上漱牵。使用openssl加密:
class AES {
? ? /**
? ? *
? ? * @param string $string 需要加密的字符串
? ? * @param string $key 密鑰
? ? * @return string
? ? */
? ? public static function encrypt($string, $key)
? ? {
? ? ? ? // openssl_encrypt 加密不同Mcrypt夺蛇,對秘鑰長度要求,超出16加密結果不變
? ? ? ? $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;
加密結果:
8c927c42f93a83c5de763aa18e4e6c7d
雖然key長度32位酣胀,但是openssl_encrypt加密時候刁赦,key長度只使用了16長度,后面未參加簽名灵临,而mcrypt_encrypt會整個key參與加密截型,這樣就會出現(xiàn)加密出來對結果不一致。造成困惑儒溉,key=g87y65ki6e8p93av8zjdrtfdrtgdwetd與key=g87y65ki6e8p93av結果都是一致對宦焦;
原文:https://blog.csdn.net/qq_38686693/article/details/81388329
原文:https://blog.csdn.net/ranlv91/article/details/81916393