最近做的项目中有一个领取优惠券的功能,项目是用php写得,不得不佩服,php自带的方法简洁而又方便好用。项目是为平台为其他公司发放优惠券,结果很囧的是接了三家优惠券公司,一家php,一家java,一家c#,下面就是整理出来的三种语言的加密解密方法:
简单介绍一下这里踩过的坑,最开始没有意识到不同的语言对加密实现的差别,服务端这里实现使用的ECB模式,PKCS7Padding填充模式,key没有位数限制(对比java中是有位数限制的)。java如果实现PKCS7Padding填充模式需要替换系统中的jar包,考虑到对方系统稳定性就放弃了PKCS7的实现转而去实现PKCS5,如果想实现PKCS7具体请参考http://www.cnblogs.com/haogj/p/4226178.html
php ECB模式,PKCS7Padding填充,块大小为256,代码如下(java、C#版本代码待补充)
/**
* 加密方法
* @param string $str
* @return string
*/
public function encrypt($str)
{
//AES, 128 ECB模式加密数据
$screct_key = $this->_app_key;
$screct_key = base64_decode($screct_key);
$str = trim($str);
$str = $this->addPKCS7Padding($str);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $screct_key, $str, MCRYPT_MODE_ECB, $iv);
return base64_encode($encrypt_str);
}
/**
* 解密方法
* @param string $str
* @return string
*/
private function decrypt($str)
{
//AES, 128 ECB模式加密数据
$screct_key = $this->_app_key;
$str = base64_decode($str);
$screct_key = base64_decode($screct_key);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $screct_key, $str, MCRYPT_MODE_ECB, $iv);
$encrypt_str = trim($encrypt_str);
$encrypt_str = $this->stripPKSC7Padding($encrypt_str);
return $encrypt_str;
}
/**
* 填充算法
* @param string $source
* @return string
*/
private function addPKCS7Padding($source)
{
$source = trim($source);
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$pad = $block - (strlen($source) % $block);
if ($pad <= $block) {
$char = chr($pad);
$source .= str_repeat($char, $pad);
}
return $source;
}
/**
* 移去填充算法
* @param string $source
* @return string
*/
private function stripPKSC7Padding($source)
{
$source = trim($source);
$char = substr($source, -1);
$num = ord($char);
if ($num > 32) return $source;
$source = substr($source, 0, -$num);
return $source;
}
ECB模式,PKCS5Padding填充。注意注意注意java只支持16位的key!!!
php 版本
/*
* 加密输入的字符串
*/
private 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->_app_key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
/*
* 解密字符串
*/
private function decrypt($sStr) {
$decrypted= mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$this->_app_key,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
/*
* 填充模式
*/
private static function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
java版本
/**
* 加密源数据
*
* @param input
* @return
*/
private String encrypt(String input) {
byte[] crypted = null;
try {
SecretKeySpec skey = new SecretKeySpec(AppKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes("UTF-8"));
} catch (Exception e) {
System.out.println(e.toString());
}
return new String(Base64.encodeBase64(crypted));
}
/**
* 解密源数据
*
* @param input
* @return
*/
private String decrypt(String input) {
byte[] output = null;
try {
SecretKeySpec skey = new SecretKeySpec(AppKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(Base64.decodeBase64(input));
} catch (Exception e) {
System.out.println(e.toString());
}
return new String(output);
}
c#版本
/// <summary>
/// AES 加密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public 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);
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesDecrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(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.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
|
请发表评论