zoukankan      html  css  js  c++  java
  • 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("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

  • 相关阅读:
    git push 小结
    在GitHub上创建代码仓库
    本人在CSDN上的技术博客访问量突破了10万次,特此截图留念
    和菜鸟一起学linux总线驱动之i2c死锁问题
    《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
    Access text files using SQL statements by DB Query Analyzer
    git分享:Git_MinaPro
    git分享:Git_DataPro
    vimgrep 搜索总结
    HTML+CSS+JavaScript网页制作从新手到高手
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15453465.html
Copyright © 2011-2022 走看看