• PHP AES128加密解密


    <?php
    /**
     * Class AES
     */
    class AES {
        public static function encrypt($input, $key) {
            $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
            $input = AES::pkcs5_pad($input, $size);
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
            $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            mcrypt_generic_init($td, md5($key), $iv);
            $data = mcrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            $data = base64_encode($data);
    
            return $data;
        }
    
        private static function pkcs5_pad($text, $blocksize) {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
        }
    
        private static function hex2bin($hexdata) {
            $bindata = '';
            $length = strlen($hexdata);
            for ($i = 0; $i < $length; $i += 2) {
                $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
            }
            return $bindata;
        }
    
        public static function decrypt($sStr, $sKey) {
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
    
            $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    
            mcrypt_generic_init($td, md5($sKey), $iv);
    
            $decrypted_text = mdecrypt_generic($td, base64_decode($sStr));
            $rt = rtrim($decrypted_text);
    
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
            $rt = AES::pkcs5_pad($rt, $size);
            $dec_s = strlen($rt);
            $padding = ord($rt[$dec_s - 1]);
            $rt = substr($rt, 0, -$padding);
            $rt = rtrim($rt);
            $rt = preg_replace('/(}[^]}{]*)$/', '}', $rt);
            return $rt;
        }
    }
    

      使用方式如下:

    /**
    **中文取消汉字编码
    */
    protected function decodeUnicode($str)
    {
      return preg_replace_callback('/\\u([0-9a-f]{4})/i',
        create_function(
          '$matches',
          'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
        ),
      $str);
    }

      //加密:

      $data_encrypt = AES::encrypt($this->decodeUnicode(json_encode($data)), 'key');

      //解密:

      $content_decrypt = AES::decrypt($content, 'key');

      $content_decrypt = json_decode($content_decrypt, true);

  • 相关阅读:
    hdu 5648 DZY Loves Math 组合数+深搜(子集法)
    hdu 5647 DZY Loves Connecting 树形DP
    hdu 4550 卡片游戏 贪心
    hdu 5646 DZY Loves Partition 二分+数学分析+递推
    hdu 2196 Computer 树形DP
    poj 2342 Anniversary party 树形DP入门
    Vijos P1003 等价表达式 随机数+单调栈
    【BZOJ】1044: [HAOI2008]木棍分割 二分+区间DP
    【BZOJ】1925: [Sdoi2010]地精部落 DP+滚动数组
    【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值
  • 原文地址:https://www.cnblogs.com/lovekingly/p/8622663.html
Copyright © 2020-2023  润新知