php aes ecb pkcs7padding 加密
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php aes ecb pkcs7padding 加密相关的知识,希望对你有一定的参考价值。
static string AesEncrypt(string str, string key)
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
;
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
以上代码如何用php代码编写?
class CryptAES
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = '';
public function set_cipher($cipher)
$this->cipher = $cipher;
public function set_mode($mode)
$this->mode = $mode;
public function set_iv($iv)
$this->iv = $iv;
public function set_key($key)
$this->secret_key = $key;
public function require_pkcs5()
$this->pad_method = 'pkcs5';
protected function pad_or_unpad($str, $ext)
if ( is_null($this->pad_method) )
return $str;
else
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
return $str;
protected function pad($str)
return $this->pad_or_unpad($str, '');
protected function unpad($str)
return $this->pad_or_unpad($str, 'un');
public function encrypt($str)
$str = $this->pad($str);
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
else
$iv = $this->iv;
mcrypt_generic_init($td, $this->secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
$rt=base64_encode($cyper_text);
//$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $rt;
public function decrypt($str)
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
else
$iv = $this->iv;
mcrypt_generic_init($td, $this->secret_key, $iv);
//$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->unpad($rt);
public static function hex2bin($hexdata)
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i<$length;$i += 2)
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
return $bindata;
public static function pkcs5_pad($text, $blocksize)
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
public static function pkcs5_unpad($text)
$pad = ord($textstrlen($text) - 1);
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
$keyStr = '187522'; //私钥
$aes = new CryptAES();
$aes->set_key($keyStr);
/* $arr = array('1'=>'aaa1111','2'=>array(1,2));
$bb = serialize($arr);
//echo $aes->encrypt($arr);
$a = $aes->decrypt($aes->encrypt($bb));
$c=unserialize($a); */
?>本回答被提问者采纳
PHP对接java的AES/ECB/PKCS5Padding加密方式
因项目需要,要和一家保险公司对接调用API,我公司是PHP后台,保险公司是java后台,中间的数据传输就避免不了要加密、解密了,目前通行的加密AES比较推荐。
对接的过程中,就难免要翻山越水的了,
下面是我对接公司的加密说明:
一定要屡清楚自己的加密方式,否则一个加密模式ECB、CBC的差别,结果就千差万别的。
附上最终能使用的代码:
<?php class Security { 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; } private static function pkcs5_pad ($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } 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; } } // $key = "1234567891234567"; // $data = "example"; // $value = Security::encrypt($data , $key ); // echo "加密::".$value.‘<br/>‘; // echo Security::decrypt($value, $key );
公共函数中调用:
/** * request body加密 * @param array $content 投保人的信息 * @return string */ function hetai_encrypt($content) { // 方案七 print_r("\r\n"); vendor(‘encrypt.Security‘) or die("方案7引入失败"); $sec = new \Security(); $string = $content; $sec_res = $sec->encrypt($string, base64_decode("eeSvvVtUDLi5lTBHDjCeFw==")); $sec_res = strToHex($sec_res);// 结果转16进制并转成大写 // 这里做了好几次的转换 // 只是为了迎合出来我需要的结果而已 // 根据自己的加密要求来定 $encrypt_upper = strToHex(base64_decode(hexToStr($sec_res))); var_dump("\r\n方案7加密的结果\r\n" . $encrypt_upper); // 解密 $sec_res_lower = strtolower($sec_res);// 转小写 $sec_res_lower_tostr = hexToStr($sec_res);// 16进制转成string $sec_dec = $sec->decrypt($sec_res_lower_tostr, base64_decode("eeSvvVtUDLi5lTBHDjCeFw==")); var_dump("\r\n方案7解密的结果\r\n" . $sec_dec); return $encrypt_upper; }
二进制字符串转16进制、16进制字符串转二进制:
/** * 字符串转十六进制 * @param string $string * @return string */ function strToHex($string) { $hex=""; for($i=0;$i<strlen($string);$i++) $hex.=dechex(ord($string[$i])); $hex=strtoupper($hex); return $hex; } /** * 十六进制转字符串 * 16进制的转为2进制字符串 * @param 十六进制 $hex * @return string */ function hexToStr($hex) { $string=""; for($i=0;$i<strlen($hex)-1;$i+=2) $string.=chr(hexdec($hex[$i].$hex[$i+1])); return $string; }
附上折磨我三天的加密功能块,仅供自己作提醒之用,不喜勿喷~
本文出自 “为了以后” 博客,谢绝转载!
以上是关于php aes ecb pkcs7padding 加密的主要内容,如果未能解决你的问题,请参考以下文章
java使用AES-256-ECB(PKCS7Padding)解密——微信支付退款通知接口指定解密方式
python Crypto AES-256-ECB 与PHP之间完成加解密