/**
* 利用mcrypt做AES加密解密 把下面的代码运行起来就看到了。
require_once APP_ROOT_PATH . "sapi/Lib/tool/Aes.php"; //加载个php文件
public function testaes(){
$arr = [
'orderId' => 11211,
'payType' => 'ordertype',
'type' => 'order'
];
//第一种方式
$str1=Security::encrypt( http_build_query($arr) ,Security::key);
// $str1 = base64_encode($str1);
$arrurl=['url'=>'wufustore://?data='.$str1,'type'=>'12'];
print_r($arrurl);
echo Security::decrypt($str1, Security::key);
echo '
';
//第二种方式
$str1=Aes::encode(Aes::key, http_build_query($arr));
$str1 = base64_encode($str1);
$arrurl=['url'=>'wufustore://?data='.$str1,'type'=>'12'];
print_r($arrurl);
echo Aes::decode(Aes::key, base64_decode($str1));
echo '
';
//第三种方式
$aes = new aestwo;
$str1 = $aes->aes128cbcEncryptecb(http_build_query($arr));
$str1 = base64_encode($str1);
$arrurl=['url'=>'wufustore://?data='.$str1,'type'=>'12'];
print_r($arrurl);
echo $aes->aes128cbcHexDecryptecb(base64_decode($str1));
echo '
';
}
*/
/**
* AES128加解密类
* Class Security
*/
class Security
{
const key = '1111111111111111';
/**
* 加密
* @param $input
* @param $key
* @return string
*/
public static function encrypt($input, $key)
{
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = Security::pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
/**
* 补码
* @param $text
* @param $blocksize
* @return string
*/
private static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
/**
* 解密
* @param $sStr
* @param $sKey
* @return string
*/
public static function decrypt($sStr, $sKey)
{
$decrypted = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
}
class AES{
/**
* 算法,另外还有192和256两种长度
*/
const CIPHER = MCRYPT_RIJNDAEL_128;
/**
* 模式
*/
const MODE = MCRYPT_MODE_ECB;
/**
* 秘钥一定要16位才与ios与android一样
*/
const key = '1111111111111111';
/**
* vi一定要16位
*/
const iv = '1111111111111111';
/**
* 加密
* @param string $key 密钥
* @param string $str 需加密的字符串
* @return type
*/
static public function encode( $key, $str ){
// $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND);
return mcrypt_encrypt(self::CIPHER, $key,self::addPkcs7Padding($str,16), self::MODE, self::iv); //这个iv代替了$iv
//(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$this->addPkcs7Padding($str,16) , MCRYPT_MODE_ECB, $iv));
}
/**
* pkcs7补码
* @param string $string 明文
* @param int $blocksize Blocksize , 以 byte 为单位
* @return String
*/
static private function addPkcs7Padding($string, $blocksize = MCRYPT_MODE_ECB) {
$len = strlen($string); //取得字符串长度
$pad = $blocksize - ($len % $blocksize); //取得补码的长度
$string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
return $string;
}
/**
* 解密
* @param type $key
* @param type $str
* @return type
*/
static public function decode( $key, $str ){
// $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND);
return mcrypt_decrypt(self::CIPHER, $key, $str, self::MODE, self::iv); //这个iv代替了$iv
}
}
class aestwo{
// const KEY="625202f9149e061d";
// const IV ="5efd3f6060e20330";
const KEY="1111111111111111";
const IV ="1111111111111111";
/**
* pkcs7补码
* @param string $string 明文
* @param int $blocksize Blocksize , 以 byte 为单位
* @return String
*/
private function addPkcs7Padding($string, $blocksize = 32) {
$len = strlen($string); //取得字符串长度
$pad = $blocksize - ($len % $blocksize); //取得补码的长度
$string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
return $string;
}
/**
* 加密然后base64转码
*
* @param String 明文
* @param 加密的初始向量(IV的长度必须和Blocksize一样, 且加密和解密一定要用相同的IV)
* @param $key 密钥
*/
function aes256cbcEncrypt($str, $iv, $key ) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $this->addPkcs7Padding($str) , MCRYPT_MODE_CBC, $iv));
}
/**
* 除去pkcs7 padding
*
* @param String 解密后的结果
*
* @return String
*/
private 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;
}
}
/**
* 解密
*
* @param String $encryptedText 二进制的密文
* @param String $iv 加密时候的IV
* @param String $key 密钥
* @return String
*/
function aes256cbcDecrypt($encryptedText, $iv, $key) {
$encryptedText =base64_decode($encryptedText);
return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encryptedText, MCRYPT_MODE_CBC, $iv));
}
function aes128cbcDecrypt($encryptedText, $iv=self::IV, $key=self::KEY) {
$encryptedText =base64_decode($encryptedText);
return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encryptedText, MCRYPT_MODE_CBC, $iv));
}
function hexToStr($hex)//十六进制转字符串
{
$string="";
for($i=0;$i $string.=chr(hexdec($hex[$i].$hex[$i+1]));
return $string;
}
function strToHex($string)//字符串转十六进制
{
$hex="";
$tmp="";
for($i=0;$i {
$tmp = dechex(ord($string[$i]));
$hex.= strlen($tmp) == 1 ? "0".$tmp : $tmp;
}
$hex=strtoupper($hex);
return $hex;
}
function aes128cbcHexDecrypt($encryptedText, $iv=self::IV, $key=self::KEY) {
$str = $this->hexToStr($encryptedText);
return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv));
}
function aes128cbcEncrypt($str, $iv=self::IV, $key=self::KEY ) { // $this->addPkcs7Padding($str,16)
$base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$this->addPkcs7Padding($str,16) , MCRYPT_MODE_CBC, $iv));
return $this->strToHex($base);
}
function aes128cbcEncryptecb($str, $iv=self::IV, $key=self::KEY ) { // $this->addPkcs7Padding($str,16)
$base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$this->addPkcs7Padding($str,16) , MCRYPT_MODE_ECB, $iv));
// return $this->strToHex($base);
return $base;
}
function aes128cbcHexDecryptecb($encryptedText, $iv=self::IV, $key=self::KEY) {
// $str = $this->hexToStr($encryptedText);
$str = $encryptedText;
return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB, $iv));
}
}