php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法

摘要:
公司开发微信第三方平台,之前用着一直是没有问题的。后来服务器到期进行项目搬迁就怎么也接收不到微信每10分钟的ticketle。经过调试发现php版本由原来的7.0升到了7.1。mcrypt_module_open()函数在7.1中被贬低,将在7.2中被移除,要用openssl_decrypt()函数代替。直接给代码明文加密:原代码$random=$this-˃getRandomStr();$text=$random.pack.$text.$appid;$size=mcrypt_get_block_size;$module=mcrypt_module_open;$iv=substr;$pkc_encoder=newPKCS7Encoder;$text=$pkc_encoder-˃encode;mcrypt_generic_init;$encrypted=mcrypt_generic;mcrypt_generic_deinit;mcrypt_module_close修改为$random=$this-˃getRandomStr();$text=$random.pack.$text.$appid;$iv=substr;$pkc_encoder=newPKCS7Encoder;$text=$pkc_encoder-˃encode;$encrypted=openssl_encrypt;明文解密原代码$ciphertext_dec=base64_decode;$module=mcrypt_module_open;$iv=substr;mcrypt_generic_init;$decrypted=mdecrypt_generic;mcrypt_generic_deinit;mcrypt_module_close;修改为$ciphertext_dec=base64_decode;$iv=substr;$decrypted=openssl_decrypt;特此记录一下!

公司开发微信第三方平台,之前用着一直是没有问题的。后来服务器到期进行项目搬迁就怎么也接收不到微信每10分钟的ticketle。

经过调试发现php版本由原来的7.0升到了7.1(该死....为什么没人告诉我)。mcrypt_module_open()函数在7.1中被贬低,将在7.2中被移除,要用openssl_decrypt()函数代替。废话不多说了。直接给代码

明文加密:

原代码

$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $appid;
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16);
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
mcrypt_generic_init($module, $this->key, $iv);
$encrypted = mcrypt_generic($module, $text);
mcrypt_generic_deinit($module);
mcrypt_module_close($module)

修改为

$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $appid;
$iv = substr($this->key, 0, 16);
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
$encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);

明文解密

原代码

$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);
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);

特此记录一下!完毕。

原文:https://blog.csdn.net/haibo_j/article/details/80759706

免责声明:文章转载自《php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTTP3.0(QUIC的实现机制)PB TreeView控件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Laravel 5.5 将会要求 PHP 7.0+

Laravel 5.5 都要用 PHP7 了呢!你还在用 PHP 5 吗? Laravel 一直是一个精 (sheng) 进 (ji) 不 (hen) 休 (kuai) 的框架。就在前几天,下图这位 Taylor 在 Twitter 上轻描淡写地宣布了 Laravel 5.5 将需要 PHP 7+ 。这又是能玩多一年的节奏! 也就是说还有不到 7 个月...

PHP操作Mysql数据库记录操作函数

简介:这是PHP操作Mysql数据库记录操作函数的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。 frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=325731' scrolling='no'> 数据库记录操作函数(5个): 1、mysql_fet...

接口中转stream传输 request/response

php实现 CURLOPT_WRITEFUNCTION: for response ,把接口返回的结果拿回来,会进行多次回调,直到接口中的内容全部读完CURLOPT_READFUNCTION :for request ,把要请求接口的参数数据写出去 CURLOPT_READFUNCTION 回调函数名。该函数应接受三个参数。第一个是 cU...

php的cookie和session相同主域名共享

如何使用chrome查看cookie和session详见另一篇文章,点这里 首先说cookie, $cookieDomain = '.elf.com'; setcookie('elf', 'im elf cookie', time()+300, '/', $cookieDomain); 如上代码设置的cookie,在主域名为 elf.com的所有二级域...

linux中查看nginx、apache、php、mysql配置文件路径

linux高效、稳定,但是也带来维护上的一些问题。配置文件究竟在哪里?????       如何在linux中查看nginx、apache、php、mysql配置文件路径了,如果你接收一个别人配置过的环境,但没留下相关文档。这时该怎么判断找到正确的加载文件路径了。可以通过以下来判断1、判断apache 首先执行命令找到httpd路径 ps aux | gr...

php : mysql数据库操作类演示

设计目标:  1,该类一实例化,就可以自动连接上mysql数据库;  2,该类可以单独去设定要使用的连接编码(set names XXX)  3,该类可以单独去设定要使用的数据库(use XXX);  4,可以主动关闭连接; <?php /* 设计一个类:mysql数据库操作类 设计目标: 1,该类一实例化,就可以自动连接上mysql数据库...