AES加密代码
2015-11-24 13:28
267 查看
public class EncryptUtil {
// --------------------部分代码-----------
/**
* AES加密
*
* @param key
* 密钥信息
* @param content
* 待加密信息
*/
public static byte[] encodeAES(byte[] key, byte[] content) throws Exception {
// 不是16的倍数的,补足
int base = 16;
if (key.length % base != 0) {
int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(key, 0, temp, 0, key.length);
key = temp;
}
SecretKey secretKey = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] tgtBytes = cipher.doFinal(content);
return tgtBytes;
}
/**
* AES解密
*
* @param key
* 密钥信息
* @param content
* 待加密信息
* @return
* @throws Exception
*/
public static byte[] decodeAES(byte[] key, byte[] content) throws Exception {
// 不是16的倍数的,补足
int base = 16;
if (key.length % base != 0) {
int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(key, 0, temp, 0, key.length);
key = temp;
}
SecretKey secretKey = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] tgtBytes = cipher.doFinal(content);
return tgtBytes;
}
//身份验证使用这个方法。
public static String encryptAES(String content, String key) {
try {
return bytesToHexString(encodeAES(key.getBytes(),
content.getBytes()));
} catch (Exception e) {
return null;
}
}
public static String decryptAES(String content, String key) {
try {
byte[] contentByte = hexStringToByte(content);
return new String(decodeAES(key.getBytes(), contentByte),
"UTF-8");
} catch (Exception e) {
return null;
}
}
/**
* 把字节数组转换成16进制字符串
*
* @parambArray
* @return
*/
public static String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i<bArray.length; i++) {
sTemp = Integer.toHexString(0xFF &bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/*
* 把16进制字符串转换成字节数组
*
* @param hex
*
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i<len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
}
public class EncryptUtil {
// --------------------部分代码-----------
/**
* AES加密
*
* @param key
* 密钥信息
* @param content
* 待加密信息
*/
public static byte[] encodeAES(byte[] key, byte[] content) throws Exception {
// 不是16的倍数的,补足
int base = 16;
if (key.length % base != 0) {
int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(key, 0, temp, 0, key.length);
key = temp;
}
SecretKey secretKey = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] tgtBytes = cipher.doFinal(content);
return tgtBytes;
}
/**
* AES解密
*
* @param key
* 密钥信息
* @param content
* 待加密信息
* @return
* @throws Exception
*/
public static byte[] decodeAES(byte[] key, byte[] content) throws Exception {
// 不是16的倍数的,补足
int base = 16;
if (key.length % base != 0) {
int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(key, 0, temp, 0, key.length);
key = temp;
}
SecretKey secretKey = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] tgtBytes = cipher.doFinal(content);
return tgtBytes;
}
//身份验证使用这个方法。
public static String encryptAES(String content, String key) {
try {
return bytesToHexString(encodeAES(key.getBytes(),
content.getBytes()));
} catch (Exception e) {
return null;
}
}
public static String decryptAES(String content, String key) {
try {
byte[] contentByte = hexStringToByte(content);
return new String(decodeAES(key.getBytes(), contentByte),
"UTF-8");
} catch (Exception e) {
return null;
}
}
/**
* 把字节数组转换成16进制字符串
*
* @parambArray
* @return
*/
public static String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i<bArray.length; i++) {
sTemp = Integer.toHexString(0xFF &bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/*
* 把16进制字符串转换成字节数组
*
* @param hex
*
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i<len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
}
相关文章推荐
- MD5加解密代码
- Java clone() 浅克隆与深度克隆
- [Python] Layer3攻击
- liunx下配置基本的java web 运行环境
- C#SerialPort通信
- C语言-----循环单链表
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- PVS-Studio C/C++/C++11 静态代码分析工具
- PHP 数组拼接成字符串
- Spring缓存注解@Cache使用
- Java 如何有效地避免OOM:善于利用软引用和弱引用
- C# XML流操作简单实例
- PHP常用自定义函数汇总
- Java中时间格式化及相关操作
- java生产者消费者模式代码示例
- 线程安全的的map-CSuperMap
- Eclipse中修改SVN用户名和密码方法
- Google Java编程风格指南中文版
- php模拟post提交网易云信
- github开源社区代码学习目录