目前市面上应用AES-ECB格式的加密是比较少见的
pkcs5padding更是凤毛麟角
但是业务需要
哎!谁叫我们是乙方呢
@key 秘钥
@input 所需加密的字符串 str
public function encrypt($input, $key) {
$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, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
public function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public function decrypt($sStr, $sKey, $iv) {
$len_key = strlen($sKey);
if($len_key <=16) {
return openssl_decrypt($sStr, 'AES-128-ECB', $sKey, 0 , $iv);
}elseif( $len_key >16 && $len_key <= 24 ){
return openssl_decrypt($sStr, 'AES-192-ECB', $sKey, 0 , $iv);
}else{
return openssl_decrypt($sStr, 'AES-256-ECB', $sKey, 0 , $iv);
}
}