aesc#java_AES加密对应的C#JAVA⽅法
由于最近在项⽬中⽤到,之前在⽹上了好多,来来回回,终于整出来了。 贴出来以后⽤起来⽅便
C#
[csharp] view plaincopyprint?
#region AES加解密
///
///AES加密(加密步骤)///1,加密字符串得到2进制数组;///2,将2禁⽌数组转为16进制;///3,进⾏base64编码///
/// 要加密的字符串
/// 密钥
publicString Encrypt(String toEncrypt, String key)
{
Byte[] _Key=Encoding.ASCII.GetBytes(key);
Byte[] _Source=Encoding.UTF8.GetBytes(toEncrypt);
Aes aes= Aes.Create("AES");
aes.Mode=CipherMode.ECB;
aes.Padding=PaddingMode.PKCS7;
aes.Key=_Key;
ICryptoTransform cTransform=aes.CreateEncryptor();
Byte[] cryptData= cTransform.TransformFinalBlock(_Source, 0, _Source.Length);
String HexCryptString=Hex_2To16(cryptData);
Byte[] HexCryptData=Encoding.UTF8.GetBytes(HexCryptString);
String CryptString=Convert.ToBase64String(HexCryptData);returnCryptString;
}///
///AES解密(解密步骤)///1,将BASE64字符串转为16进制数组///2,将16进制数组转为字符串///3,将字符串转为2进制数据///4,⽤AES解密数据///
/// 已加密的内容
/// 密钥
public String Decrypt(string encryptedSource, stringkey)
{
Byte[] _Key=Encoding.ASCII.GetBytes(key);
Aes aes= Aes.Create("AES");
aes.Mode=CipherMode.ECB;
aes.Padding=PaddingMode.PKCS7;
aes.Key=_Key;
ICryptoTransform cTransform=aes.CreateDecryptor();
Byte[] encryptedData=Convert.FromBase64String(encryptedSource);
String encryptedString=Encoding.UTF8.GetString(encryptedData);
Byte[] _Source=Hex_16To2(encryptedString);
Byte[] originalSrouceData= cTransform.TransformFinalBlock(_Source, 0, _Source.Length); String originalString=Encoding.UTF8.GetString(originalSrouceData);returnoriginalString;
}///
///2进制转16进制///
String Hex_2To16(Byte[] bytes)
{
String hexString=String.Empty;
Int32 iLength= 65535;if (bytes != null)
{
StringBuilder strB= newStringBuilder();if (bytes.Length
{
iLength=bytes.Length;
}for (int i = 0; i < iLength; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString=strB.ToString();
}returnhexString;
}///
/
//16进制转2进制///
Byte[] Hex_16To2(String hexString)
{if ((hexString.Length % 2) != 0)
{
hexString+= " ";
}
Byte[] returnBytes= new Byte[hexString.Length / 2];for (Int32 i = 0; i < returnBytes.Length; i++) {
returnBytes[i]= Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}returnreturnBytes;
}#endregion
#region AES加解密
/
//
///AES加密(加密步骤)///1,加密字符串得到2进制数组;///2,将2禁⽌数组转为16进制;///3,进⾏base64编码///
/// 要加密的字符串
/// 密钥
publicString Encrypt(String toEncrypt, String key)
{
Byte[] _Key=Encoding.ASCII.GetBytes(key);
Byte[] _Source=Encoding.UTF8.GetBytes(toEncrypt);
Aes aes= Aes.Create("AES");
aes.Mode=CipherMode.ECB;
aes.Padding=PaddingMode.PKCS7;
aes.Key=_Key;
ICryptoTransform cTransform=aes.CreateEncryptor();
Byte[] cryptData= cTransform.TransformFinalBlock(_Source, 0, _Source.Length);
String HexCryptString=Hex_2To16(cryptData);
Byte[] HexCryptData=Encoding.UTF8.GetBytes(HexCryptString);
String CryptString=Convert.ToBase64String(HexCryptData);returnCryptString;
}///
///AES解密(解密步骤)///1,将BASE64字符串转为16进制数组///2,将16进制数组转为字符串///3,将字符串转为2进制数据///4,⽤AES解密数据///
/// 已加密的内容
/// 密钥
public String Decrypt(string encryptedSource, stringkey)
{
Byte[] _Key=Encoding.ASCII.GetBytes(key);
Aes aes= Aes.Create("AES");
aes.Mode=CipherMode.ECB;
aes.Padding=PaddingMode.PKCS7;
aes.Key=_Key;
ICryptoTransform cTransform=aes.CreateDecryptor();
Byte[] encryptedData=Convert.FromBase64String(encryptedSource);
String encryptedString=Encoding.UTF8.GetString(encryptedData);
Byte[] _Source=Hex_16To2(encryptedString);
Byte[] originalSrouceData= cTransform.TransformFinalBlock(_Source, 0, _Source.Length); String originalString=Encoding.UTF8.GetString(originalSrouceData);returnoriginalString;
}///
///2进制转16进制///
String Hex_2To16(Byte[] bytes)
{
String hexString=String.Empty;
Int32 iLength= 65535;if (bytes != null)
{
StringBuilder strB= newStringBuilder();if (bytes.Length
{
iLength=bytes.Length;
}for (int i = 0; i < iLength; i++)
java的tostring方法
{
strB.Append(bytes[i].ToString("X2"));
}
hexString=strB.ToString();
}returnhexString;
}///
///16进制转2进制///
Byte[] Hex_16To2(String hexString)
{if ((hexString.Length % 2) != 0)
{
hexString+= " ";
}
Byte[] returnBytes= new Byte[hexString.Length / 2];for (Int32 i = 0; i < returnBytes.Length; i++) {
returnBytes[i]= Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}returnreturnBytes;
}#endregionJAVA:
[java] view plaincopyprint?
/**
* 加密--把加密后的byte数组先进⾏⼆进制转16进制在进⾏base64编码
* @param sSrc
* @param sKey
* @return
* @throws Exception*/
public staticString encrypt(String sSrc, String sKey) throws Exception {if (sKey == null) {throw new IllegalArgumentException("Argument sKey is null.");
}if (sKey.length() != 16) {throw newIllegalArgumentException("Argument sKey'length is not 16.");
}byte[] raw = Bytes("ASCII");
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
Cipher cipher= Instance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);byte[] encrypted = cipher.Bytes("UTF-8"));
String tempStr=parseByte2HexStr(encrypted);
Base64Encoder encoder= newBase64Encoder();Bytes("UTF-8"));
}/**
*解密--先 进⾏base64解码,在进⾏16进制转为2进制然后再解码
* @param sSrc
* @param sKey
* @return
* @throws Exception*/
public staticString decrypt(String sSrc, String sKey) throws Exception {if (sKey == null) {throw new IllegalArgumentException("499");
}if (sKey.length() != 16) {throw new IllegalArgumentException("498");
}byte[] raw = Bytes("ASCII");
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
Cipher cipher= Instance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
Base64Encoder encoder= newBase64Encoder();byte[] encrypted1 =encoder.decode(sSrc);
String tempStr= new String(encrypted1, "utf-8");
encrypted1=parseHexStr2Byte(tempStr);byte[] original =cipher.doFinal(encrypted1);
String originalString= new String(original, "utf-8");returnoriginalString;
}/**
* 将⼆进制转换成16进制
*
* @param buf
* @return*/
public static String parseByte2HexStr(bytebuf[]) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。