zoukankan      html  css  js  c++  java
  • 小程序 mcrypt加密拓展在php7.1 废弃 使用openssl替代方案

    原加密方法 使用mcrypt

    //获得16位随机字符串,填充到明文之前
    $random = $this->getRandomStr();
    $text = $random . pack("N", strlen($text)) . $text . $appid;
    $iv = substr($this->key, 0, 16);
    
    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    
    //使用自定义的填充方式对明文进行补位填充
    $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);

     新加密方法 使用openssl

    //获得16位随机字符串,填充到明文之前
    $random = $this->getRandomStr();
    $text = $random . pack("N", strlen($text)) . $text . $appid;
    $iv = substr($this->key, 0, 16);
    
    $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); 

    原解密方法 使用mcrypt

    $ciphertext_dec = base64_decode($encrypted);
    $iv = substr($this->key, 0, 16);
    
    $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    mcrypt_generic_init($module, $this->key, $iv);
    
    //解密
    $decrypted = mdecrypt_generic($module, $ciphertext_dec);
    mcrypt_generic_deinit($module);
    mcrypt_module_close($module);

     新加密方法 使用openssl

    $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); 
  • 相关阅读:
    cookie 保存信息 例子
    win7 远程桌面 不用域账户登录
    jfreechart demo 源代码 下载
    PostgreSQL学习手册(数据表)
    apache tomcat 伪静态 struts2 伪静态
    List methods
    DX 骨骼动画
    如何成为一名优秀的程序员?
    程序员改编游戏向女友求婚
    引用 病毒是怎么命名的?教你认识病毒命名规则
  • 原文地址:https://www.cnblogs.com/zjhblogs/p/9467948.html
Copyright © 2011-2022 走看看