AES加密 ECB模式 PKCS5填充 128位密码/密码块
ECB模式是将明文按照固定大小的块进行加密的,块大小不足则进行填充,ECB模式没有用到向量。
具体加密算法,请参见米扑博客总结的系列文章:AES、DES、RSA三种典型加密算法
PHP 实现
class AES {
var $key = "1234567812345678";
public function __set($key, $value){
$this->$key = $value;
}
public function __get($key) {
return $this->$key;
}
public function encrypt($input) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = $this->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, $this->key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = $this->base64url_encode($data);
return $data;
}
private function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function strToHex($string)
{
$hex="";
for ($i=0;$i
$hex.=dechex(ord($string[$i]));
$hex=strtoupper($hex);
return $hex;
}
function hexToStr($hex)
{
$string="";
for ($i=0;$i
$string.=chr(hexdec($hex[$i].$hex[$i+1]));
return $string;
}
public function decrypt($sStr) {
$decrypted= mcrypt_