最近在開發微信公眾號的時候發現了一個問題,那就是PHP7.1在mcrypt中已經是被棄用的了,那麼只能想辦法解決這個問題,今天就給大家說說我如何解決這個問題以及解決思路。
php7.1發表後新特性吸引了不少PHPer,大家都在討論新特性帶來的好處與便利。但是從php7.0 升級到 php7.1 廢棄(過時)了一個在過去普遍應用的擴展(mcrypt擴展)。官方提供了相應的解決提示,卻沒有提供更詳細的解決方案。於是坑來了:
今天在使用微信開放平台對接一個內容管理系統的時候,在綁定公眾號的時候一直失敗
原因:
<?php function aes_decode($message, $encodingaeskey = '', $appid = '') { $key = base64_decode($encodingaeskey . '='); $ciphertext_dec = base64_decode($message); $iv = substr($key, 0, 16); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($module, $key, $iv); $decrypted = mdecrypt_generic($module, $ciphertext_dec); mcrypt_generic_deinit($module); mcrypt_module_close($module); $pad = ord(substr($decrypted, -1)); if ($pad < 1 || $pad > 32) { $pad = 0; }
<?php function aes_decode($message, $encodingaeskey = '', $appid = '') { $key = base64_decode($encodingaeskey . '='); $ciphertext_dec = base64_decode($message); $iv = substr($key, 0, 16); /* mcrypt对称解密代码在PHP7.1已经被抛弃了,所以使用下面的openssl来代替 $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($module, $key, $iv); $decrypted = mdecrypt_generic($module, $ciphertext_dec); mcrypt_generic_deinit($module); mcrypt_module_close($module); */ $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); $pad = ord(substr($decrypted, -1)); if ($pad < 1 || $pad > 32) { $pad = 0; }
##上面的解密已經修改了,那麼對應的Mcrypt加密也需要修改,如果不改的話會導致不能全網發布以及不能推送訊息等事件
<?php function aes_encode($message, $encodingaeskey = '', $appid = '') { $key = base64_decode($encodingaeskey . '='); $text = random(16) . pack("N", strlen($message)) . $message . $appid; $iv = substr($key, 0, 16); $block_size = 32; $text_length = strlen($text); $amount_to_pad = $block_size - ($text_length % $block_size); if ($amount_to_pad == 0) { $amount_to_pad = $block_size; } $pad_chr = chr($amount_to_pad); $tmp = ''; for ($index = 0; $index < $amount_to_pad; $index++) { $tmp .= $pad_chr; } $text = $text . $tmp; $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($module, $key, $iv); $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); $encrypt_msg = base64_encode($encrypted); return $encrypt_msg; }
<?php function aes_encode($message, $encodingaeskey = '', $appid = '') { $key = base64_decode($encodingaeskey . '='); $text = random(16) . pack("N", strlen($message)) . $message . $appid; $iv = substr($key, 0, 16); $block_size = 32; $text_length = strlen($text); $amount_to_pad = $block_size - ($text_length % $block_size); if ($amount_to_pad == 0) { $amount_to_pad = $block_size; } $pad_chr = chr($amount_to_pad); $tmp = ''; for ($index = 0; $index < $amount_to_pad; $index++) { $tmp .= $pad_chr; } $text = $text . $tmp; /* mcrypt对称加密代码在PHP7.1已经被抛弃了,所以使用下面的openssl来代替 $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($module, $key, $iv); $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); */ $encrypted = openssl_encrypt($text, 'AES-256-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); $encrypt_msg = base64_encode($encrypted); return $encrypt_msg; }
特別注意:凡是涉及到微信開發的流程,如果已經升級到PHP 7.1的話,那麼很有必要需要檢查一下是否是使用Mcrypt對稱加解密的,微信開發文檔中使用的demo也是使用Mcrypt加解密的,這一點需要注意。
總結
實例講解Ajax非同步請求技術#AJAX常用的語法是什麼#AJAX原理與CORS跨域的方法以上是在PHP中如何利用OpenSSL取代Mcrypt加解密?的詳細內容。更多資訊請關注PHP中文網其他相關文章!