由于最近在项目中用到,之前在网上找了好多,来来回回,终于整出来了。 贴出来以后用起来方便

C#

 [csharp] view plaincopyprint?
#region AES加解密
/// <summary>
///AES加密(加密步骤)
///1,加密字符串得到2进制数组;
///2,将2禁止数组转为16进制;
///3,进行base64编码
/// </summary>
/// <param name="toEncrypt">要加密的字符串</param>
/// <param name="key">密钥</param>
public String 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, , _Source.Length);
String HexCryptString = Hex_2To16(cryptData);
Byte[] HexCryptData = Encoding.UTF8.GetBytes(HexCryptString);
String CryptString =Convert.ToBase64String(HexCryptData);
return CryptString;
} /// <summary>
/// AES解密(解密步骤)
/// 1,将BASE64字符串转为16进制数组
/// 2,将16进制数组转为字符串
/// 3,将字符串转为2进制数据
/// 4,用AES解密数据
/// </summary>
/// <param name="encryptedSource">已加密的内容</param>
/// <param name="key">密钥</param>
public String Decrypt(string encryptedSource, string key)
{
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, , _Source.Length);
String originalString = Encoding.UTF8.GetString(originalSrouceData);
return originalString;
} /// <summary>
/// 2进制转16进制
/// </summary>
String Hex_2To16(Byte[] bytes)
{
String hexString = String.Empty;
Int32 iLength = ;
if (bytes != null)
{
StringBuilder strB = new StringBuilder(); if (bytes.Length < iLength)
{
iLength = bytes.Length;
} for (int i = ; i < iLength; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
} /// <summary>
/// 16进制转2进制
/// </summary>
Byte[] Hex_16To2(String hexString)
{
if ((hexString.Length % ) != )
{
hexString += " ";
}
Byte[] returnBytes = new Byte[hexString.Length / ];
for (Int32 i = ; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * , ), );
}
return returnBytes;
}
#endregion #region AES加解密
/// <summary>
///AES加密(加密步骤)
///1,加密字符串得到2进制数组;
///2,将2禁止数组转为16进制;
///3,进行base64编码
/// </summary>
/// <param name="toEncrypt">要加密的字符串</param>
/// <param name="key">密钥</param>
public String 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, , _Source.Length);
String HexCryptString = Hex_2To16(cryptData);
Byte[] HexCryptData = Encoding.UTF8.GetBytes(HexCryptString);
String CryptString =Convert.ToBase64String(HexCryptData);
return CryptString;
} /// <summary>
/// AES解密(解密步骤)
/// 1,将BASE64字符串转为16进制数组
/// 2,将16进制数组转为字符串
/// 3,将字符串转为2进制数据
/// 4,用AES解密数据
/// </summary>
/// <param name="encryptedSource">已加密的内容</param>
/// <param name="key">密钥</param>
public String Decrypt(string encryptedSource, string key)
{
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, , _Source.Length);
String originalString = Encoding.UTF8.GetString(originalSrouceData);
return originalString;
} /// <summary>
/// 2进制转16进制
/// </summary>
String Hex_2To16(Byte[] bytes)
{
String hexString = String.Empty;
Int32 iLength = ;
if (bytes != null)
{
StringBuilder strB = new StringBuilder(); if (bytes.Length < iLength)
{
iLength = bytes.Length;
} for (int i = ; i < iLength; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
} /// <summary>
/// 16进制转2进制
/// </summary>
Byte[] Hex_16To2(String hexString)
{
if ((hexString.Length % ) != )
{
hexString += " ";
}
Byte[] returnBytes = new Byte[hexString.Length / ];
for (Int32 i = ; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * , ), );
}
return returnBytes;
}
#endregion
JAVA: [java] view plaincopyprint?
/**
* 加密--把加密后的byte数组先进行二进制转16进制在进行base64编码
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
throw new IllegalArgumentException("Argument sKey is null.");
}
if (sKey.length() != ) {
throw new IllegalArgumentException(
"Argument sKey'length is not 16.");
}
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
String tempStr = parseByte2HexStr(encrypted); Base64Encoder encoder = new Base64Encoder();
return encoder.encode(tempStr.getBytes("UTF-8"));
} /**
*解密--先 进行base64解码,在进行16进制转为2进制然后再解码
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) {
throw new IllegalArgumentException("");
}
if (sKey.length() != ) {
throw new IllegalArgumentException("");
} byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec); Base64Encoder encoder = new Base64Encoder();
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");
return originalString;
} /**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = ; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == ) {
hex = '' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} /**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < )
return null;
byte[] result = new byte[hexStr.length() / ];
for (int i = ; i < hexStr.length() / ; i++) {
int high = Integer.parseInt(hexStr.substring(i * , i * + ), );
int low = Integer.parseInt(hexStr.substring(i * + , i * + ),
);
result[i] = (byte) (high * + low);
}
return result;
} /**
* 加密--把加密后的byte数组先进行二进制转16进制在进行base64编码
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
throw new IllegalArgumentException("Argument sKey is null.");
}
if (sKey.length() != ) {
throw new IllegalArgumentException(
"Argument sKey'length is not 16.");
}
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
String tempStr = parseByte2HexStr(encrypted); Base64Encoder encoder = new Base64Encoder();
return encoder.encode(tempStr.getBytes("UTF-8"));
} /**
*解密--先 进行base64解码,在进行16进制转为2进制然后再解码
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) {
throw new IllegalArgumentException("");
}
if (sKey.length() != ) {
throw new IllegalArgumentException("");
} byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec); Base64Encoder encoder = new Base64Encoder();
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");
return originalString;
} /**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = ; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == ) {
hex = '' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} /**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < )
return null;
byte[] result = new byte[hexStr.length() / ];
for (int i = ; i < hexStr.length() / ; i++) {
int high = Integer.parseInt(hexStr.substring(i * , i * + ), );
int low = Integer.parseInt(hexStr.substring(i * + , i * + ),
);
result[i] = (byte) (high * + low);
}
return result;
}

最新文章

  1. C 标准库系列之ctype.h
  2. *CF2.D(哥德巴赫猜想)
  3. UNIX网络编程-非阻塞connect和非阻塞accept
  4. Winform 中DataGridView的checkbox列,当修改checkbox状态时实时获得其状态值
  5. 【月末轻松篇】--- 那些奇葩的Bugs
  6. ArcGIS Server新建主题图服务的步骤
  7. 台积电16nm工艺为什么好过三星14nm
  8. XP下的进程静音技术(遍历进程,遍历输入模块,遍历输入函数,找到函数并HOOK) good
  9. 用Qt开发Web和本地混合的应用
  10. Oracle的substr函数
  11. DiscuzX3.1搬家全过程
  12. jquery中防止冒泡事件
  13. Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -&gt; SQL Editor and recon
  14. leetcode数据库题目及答案汇总
  15. semantic-ui 分割线
  16. Android中HttpURLConnection对象是怎么生成的
  17. 【bzoj 3110】[Zjoi2013]K大数查询
  18. Centos7上安装Apache
  19. 如何将相册中的动态GIF图转化成NSData类型
  20. Python文件读写及网站显示

热门文章

  1. iOS强引用和弱引用
  2. nl命令(转)
  3. Java Lombok 减少代码冗余 get set
  4. 【Docker】拉取Oracle 11g镜像配置
  5. SpringMVC之ModelAndView的用法(转)
  6. 关于iOS 类扩展Extension的进一步理解
  7. HDUOJ---The Moving Points
  8. map reduce相关程序
  9. Android学习系列(6)--App模块化及工程扩展
  10. java程序员学习路线图 java程序员进阶路线