Heim  >  Artikel  >  Backend-Entwicklung  >  Wie verwende ich OpenSSL anstelle von Mcrypt-Verschlüsselung und -Entschlüsselung in PHP?

Wie verwende ich OpenSSL anstelle von Mcrypt-Verschlüsselung und -Entschlüsselung in PHP?

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 14:37:191718Durchsuche

最近在开发微信公众号的时候发现了一个问题,那就是PHP7.1在mcrypt中已经是被弃用的了,那么只能想办法解决这个问题,今天就给大家说说我如何解决这个问题以及解决思路。

php7.1发布后新特性吸引了不少PHPer,大家都在讨论新特性带来的好处与便利。但是从php7.0 升级到 php7.1 废弃(过时)了一个在过去普遍应用的扩展(mcrypt扩展)。官方提供了相应的解决提示,却没有提供更详细的解决办法。于是坑来了:

今天在使用微信开放平台对接一个内容管理系统的时候,在绑定公众号的时候一直失败

原因:

调试的时候发现,直接原因是因为开放平台里面填写的授权事件(该授权事件每十分钟会通送一次事件来更新ticket),即:


这个地方填写的url,调试发现,这个URL没错,微信也有每10分钟推送过来,但是到最后一直接收不到ticket,看代码发现是因为解密微信过来的数据的时候报错了:

<?php
  
function aes_decode($message, $encodingaeskey = &#39;&#39;, $appid = &#39;&#39;) {
 $key = base64_decode($encodingaeskey . &#39;=&#39;);
  
 $ciphertext_dec = base64_decode($message);
 $iv = substr($key, 0, 16);
  
 $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
 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 7.1,查找资料发现PHP 7.1已经废弃了Mcrypt,所以这个代码里面的mcrypt_*都是无法运行的。

解决:

查找资料发现,可以通过OpenSSL来代替Mcrypt(前提是已经安装了OpenSSL扩展,不过一般都是默认安装的)

openssl是一个功能强大的工具包,它集成了众多密码算法及实用工具。我们即可以利用它提供的命令台工具生成密钥、证书来加密解密文件,也可以在利用其提供的API接口在代码中对传输信息进行加密。

所以上面的代码可以改为:

<?php
  
function aes_decode($message, $encodingaeskey = &#39;&#39;, $appid = &#39;&#39;) {
 $key = base64_decode($encodingaeskey . &#39;=&#39;);
  
 $ciphertext_dec = base64_decode($message);
 $iv = substr($key, 0, 16);
  
 /* mcrypt对称解密代码在PHP7.1已经被抛弃了,所以使用下面的openssl来代替
 $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
 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, &#39;AES-256-CBC&#39;, $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 = &#39;&#39;, $appid = &#39;&#39;) {
 $key = base64_decode($encodingaeskey . &#39;=&#39;);
 $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 = &#39;&#39;;
 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, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
 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 = &#39;&#39;, $appid = &#39;&#39;) {
 $key = base64_decode($encodingaeskey . &#39;=&#39;);
 $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 = &#39;&#39;;
 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, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
 mcrypt_generic_init($module, $key, $iv);
 $encrypted = mcrypt_generic($module, $text);
 mcrypt_generic_deinit($module);
 mcrypt_module_close($module);
 */
  
 $encrypted = openssl_encrypt($text, &#39;AES-256-CBC&#39;, $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跨域的方法

Das obige ist der detaillierte Inhalt vonWie verwende ich OpenSSL anstelle von Mcrypt-Verschlüsselung und -Entschlüsselung in PHP?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn