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>';
}
学习的热情不因季节的变化而改变