php
<?php class AesCrypt { //初始化向量 private $iv = '84195bd96a8a2e7f'; //Mcrypt算法 private $cipher = MCRYPT_RIJNDAEL_128; //Mcrypt支持的加密模型 特别适用于对文件进行加密。 相比 ECB, 它的安全性有明显提升。 private $mode = MCRYPT_MODE_CBC; public function encrypt($str, $key = '') { $str = $this->addPKCS7Padding($str); $encrypted = mcrypt_encrypt($this->cipher, $key, $str, $this->mode, $this->iv); return $encrypted; } public function decrypt($code, $key = '') { $decrypted = mcrypt_decrypt($this->cipher, $key, $code, $this->mode, $this->iv); $decrypted = $this->stripPKSC7Padding($decrypted); return $decrypted; } protected function addPKCS7Padding($source) { $block = mcrypt_get_block_size($this->cipher, $this->mode); $pad = $block - (strlen($source) % $block); $char = chr($pad); $source .= str_repeat($char, $pad); return $source; } public function stripPKSC7Padding($source) { $char = substr($source, -1); $num = ord($char); $source = substr($source, 0, -$num); return $source; } }