Aes/cbc/pkcs5padding/128 Decryption Example:
<?php class aesencrypt{//Custom key protected $key;
Cipher algorithm type protected $cipher = ' rijndael-128 ';
Mode model Protected $mode = ' CBC ';
Public function __construct ($key) {$this->key = $key; //aes Cryptographic Public Function Aesencrypt ($data) {//randomly generated 16-bit IV, initial vector $iv = self::aesrandom (16, ' 123456789
0abcdefghijklmnopqrstuvwxyz ');
Pkcs5padding//Get encryption algorithm packet size $block = mcrypt_get_block_size ($this->cipher, $this->mode);
Way one calculates the complement length, chr-returns a single character//$pad = Chr ($block-(strlen ($data)% $block) as specified in ASCII;
Str_pad-uses another string to fill the string for the specified length//$pad = Str_pad ($data, Ceil (strlen ($data)/16.0) *16, $pad);
Mode two gets the complement length $pad = $block-(strlen ($data)% $block);
str_repeat-repeats a string Chr ($pad), $pad times $pad = $data. Str_repeat (Chr ($pad), $pad); Mcrypt_module_open-Opens the module corresponding to the algorithm and pattern $TD = Mcrypt_module_open ($this->cipher, ", $this->mode,");
mcrypt_generic_init-the buffer mcrypt_generic_init ($TD, $this->key, $IV) required to initialize the encryption;
mcrypt_generic-encrypt data, must call Mcrypt_generic_init $encrypt = Mcrypt_generic ($TD, $pad) before use;
mcrypt_generic_deinit-the encryption module clean up, it will clean the buffer, but does not close the module mcrypt_generic_deinit ($TD);
Mcrypt_module_close-off the Encryption module mcrypt_module_close ($TD);
IV Base64 return Base64_encode ($iv. $encrypt) after the spell string;
//aes Decrypt Public Function Aesdecrypt ($data) {$data = Base64_decode ($data);//base64//Top 16 for IV
$iv = substr ($data, 0, 16);
16-digit Posterior $data = substr ($data, 16);
Mcrypt_module_open-Opens the module corresponding to the algorithm and mode $TD = Mcrypt_module_open ($this->cipher, ', $this->mode, ');
mcrypt_generic_init-the buffer mcrypt_generic_init ($TD, $this->key, $IV) required to initialize the encryption;
mdecrypt_generic-decrypt the data, before use must call Mcrypt_generic_init $decrypt = Mdecrypt_generic ($TD, $data); Mcrypt_generic_deinit-the encryption module to clean the work mcrypt_generic_deinit ($TD);
Mcrypt_module_close-off the Encryption module mcrypt_module_close ($TD);
Remove the padding//ord-return the ASCII code value of the character $pad = Ord ($decrypt [($len = strlen ($decrypt))-1]);
Returns the complement start index position $beforePad = strlen ($decrypt)-$pad; Judge whether the complement $decrypt = substr ($decrypt, $beforePad) = = Str_repeat (substr ($decrypt,-1), $pad)?
substr ($decrypt, 0, $len-$pad): $decrypt;
return $decrypt; ///Randomly generate 16-bit IV public Function aesrandom ($length, $chars = ' ABCDEFGHJKLMNPQRSTUVWXYZ23456789 ') {$hash
= '';
$max = strlen ($chars)-1;
for ($i = 0; $i < $length; $i + +) {$hash. = $chars [Mt_rand (0, $max)];
return $hash;
}//Modify cipher Public Function Setcipher ($cipher) {$this->cipher = $cipher;
}//Modify mode Public Function SetMode ($mode) {$this->mode = $mode; }
}
//initialization Class $aes = new Aesencrypt (' i4fe6xmsndfrgk4g ');
$str = ' xiaoming ';
Encryption $res = $aes->aesencrypt ($STR); echo $res. "
<br/> ";
Decryption $res 2 = $aes->aesdecrypt ($res); echo $res 2. "
<br/> ";
$resstr = ' rtnay7a6vnhkuqficrxgtixbyxcp2f+a+zxy4onsg3w= ';
$res 3 = $aes->aesdecrypt ($RESSTR); echo $res 3. " <br/> ";
Sample results:
aesencrypt:mwy5bzbvoxdidwuxmnu4n/f8andzuw3csza+qcniqui=
aesdecrypt:xiaoming
aesdecrypt:xiaoming
URL with encryption parameter to get problem
Example:
http://test.local?token=rTnAY7a6vnHkUqfICRxgtiXBYXcP2f+A+zxy4ONsg3w=
Get the parameters with the above URL token
The browser will automatically transcoding some special characters, so you need to be aware of the conversion when getting
1) UrlDecode
After UrlDecode, convert the + number to spaces and replace
$token = Input::get (' token ');
$token = UrlDecode ($token);
echo $token. ' <br> ';
$token = Str_replace (', ' + ', $token);
Echo $token;
Sample results:
rtnay7a6vnhkuqficrxgtixbyxcp2f A zxy4onsg3w=
rtnay7a6vnhkuqficrxgtixbyxcp2f+a+zxy4onsg3w=
2) Rawurldecode
$token = Input::get (' token ');
echo Rawurldecode ($STR);
Sample results:
rtnay7a6vnhkuqficrxgtixbyxcp2f+a+zxy4onsg3w=