• 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>';
    }
  • 相关阅读:
    pysocketserver
    协程
    py模块和包
    py网络编程
    GridView绑定技巧终结者
    iOS 证书/私钥/代码签名/描述文件
    八拜之交
    POJ3230(Travel)
    POJ2553(The Bottom of a Graph)
    动态规划的实质
  • 原文地址:https://www.cnblogs.com/xiong63/p/6801069.html
Copyright © 2020-2023  润新知