• php aes128加密


        //[加密数据]AES 128 ECB模式
        public function aesEncrypt($str){
            $screct_key = Yii::$app->params['encryptKey'];
            $str = $this->addPKCS7Padding(trim($str));
            $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
            $encrypt_str =  mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_ECB, $iv);
            return bin2hex($encrypt_str);
        }
    
        //[解密数据]AES 128 ECB模式
        public function aesDecrypt($str){
            $screct_key = Yii::$app->params['encryptKey'];
            //AES, 128 ECB模式加密数据
            $str=hex2bin($str);
            $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
            $encrypt_str =  mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_ECB, $iv);
            return $this->stripPKSC7Padding($encrypt_str);
        }
    
        //[加密数据填充算法]
        function addPKCS7Padding($source){
            $source = trim($source);
            $block = mcrypt_get_block_size('rijndael-128', 'ecb');
            $pad = $block - (strlen($source) % $block);
            if ($pad <= $block) {
                $char = chr($pad);
                $source .= str_repeat($char, $pad);
            }
            return $source;
        }
    
        //[解密数据移去填充算法]
        function stripPKSC7Padding($text){ 
          $len = strlen($text);    
          $c = $text[$len-1];    
          if(ord($c) <$len){    
              for($i=$len-ord($c); $i<$len; $i++){    
                  if($text[$i] != $c){    
                      return $text;    
                  }    
              }    
              return substr($text, 0, $len-ord($c));    
          }      
        }
     public function actionIndex()
        {
            $c='yii2 php加密算法  aes128';
            $a=$this->aesEncrypt($c);
            $b=$this->aesDecrypt($a);
            echo $a;
            echo '<br>';
            echo $b;
            echo '<br>';
    }
  • 相关阅读:
    ACCESS中默认值要填双引号
    错误一直找不到
    员工自行车的摆放处
    连接占线导致另一个hstmt
    去裕利面试
    路上又一见闻
    企业的形象
    骏泰面试感觉
    IE 标点符号输入不顺的原因
    C Primer Plus(十七)
  • 原文地址:https://www.cnblogs.com/xiong63/p/6801069.html
Copyright © 2020-2023  润新知