php 接口中常用的aes加密
<?php /** * Aes 对称加密 */ class Aes { public $key; public function __construct($key){ $this->key = $key; $this->iv = $key; } /** * 加密 ECB - 128 - addPkcs7Padding */ public function encrypts($data) { $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $this->addPkcs7Padding($data), MCRYPT_MODE_ECB,$this->iv); return base64_encode($encrypted); } /** * 解密 */ public function decrypts($data) { $data =base64_decode($data); return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $data, MCRYPT_MODE_ECB, $this->iv)); } public function addPkcs7Padding($string, $blocksize = 16) { $len = strlen($string); //取得字符串长度 $pad = $blocksize - ($len % $blocksize); //取得补码的长度 $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段 return $string; } public function stripPkcs7Padding($string){ $slast = ord(substr($string, -1)); $slastc = chr($slast); $pcheck = substr($string, -$slast); if(preg_match("/$slastc{".$slast."}/", $string)){ $string = substr($string, 0, strlen($string)-$slast); return $string; } else { return false; } } }
$sir = 'zimakaimen';
$key = md5($_POST['key'].$sir);
$aes = new Aes($key);
$data = $aes->decrypts($params);