Function mcrypt_get_block_size() is deprecated

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 加密技術(shù)的重點(diǎn)是加密算法,加密算法主要分為三類: 對稱加密 非對稱加密 不可逆加密 對稱加密算法 加密過程: 將明...
    飛越_7666閱讀 1,211評論 0 0
  • PHP,Android,IOS通信之 AES128加解密案例程序android上調(diào)用代碼: mcrypt = ne...
    YO_GE閱讀 1,101評論 0 0
  • <?php namespace app\common\lib; /** * aes 加密 解密類庫 * @by s...
    進(jìn)擊的PHPer閱讀 618評論 0 0
  • 分組密碼每次只能處理加密固定長度的分組,但是我們加密的明文可能會超過分組密碼處理的長度。這時便需要對所有分組進(jìn)行迭...
    查無此人asdasd閱讀 1,329評論 0 0
  • 姓名 汪海。公司 上海日朗門窗有限公司 【日精進(jìn)打卡第64天】 【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》背誦0遍 《大學(xué)》背誦0...
    汪海_51e2閱讀 142評論 0 0

友情鏈接更多精彩內(nèi)容