demo包含如下几个类:
- 1.AesUtils:
package com.lijie.utils;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Lijie
*
*/
public class AesUtils {
public static void main(String[] args) {
// 密钥的种子,可以是任何形式,本质是字节数组
String strKey = "lijietest";
// 密钥数据
byte[] rawKey = getRawKey(strKey.getBytes());
// 密码的明文
String clearPwd = "{\"name\":\"黎杰\",\"age\":24,\"birthday\":\"2017年02月20日\",\"email\":\"lijiexxxx@xxxx.com\"}";
// 密码加密后的密文
byte[] encryptedByteArr = encrypt(rawKey, clearPwd);
String encryptedPwd = new String(encryptedByteArr);
System.out.println(encryptedPwd);
// 解密后的字符串
String decryptedPwd = decrypt(encryptedByteArr, rawKey);
System.out.println(decryptedPwd);
}
/**
* @param rawKey
* 密钥
* @param clearPwd
* 明文字符串
* @return 密文字节数组
*/
public static byte[] encrypt(byte[] rawKey, String clearPwd) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(rawKey, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encypted = cipher.doFinal(clearPwd.getBytes());
return encypted;
} catch (Exception e) {
return null;
}
}
/**
* @param encrypted
* 密文字节数组
* @param rawKey
* 密钥
* @return 解密后的字符串
*/
public static String decrypt(byte[] encrypted, byte[] rawKey) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(rawKey, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted);
} catch (Exception e) {
System.out.println();
return "";
}
}
/**
* @param seed种子数据
* @return 密钥数据
*/
public static byte[] getRawKey(byte[] seed) {
byte[] rawKey = null;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(seed);
// AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
rawKey = secretKey.getEncoded();
} catch (NoSuchAlgorithmException e) {
}
return rawKey;
}
}
- 2.Base64Util
package com.lijie.utils;
import org.glassfish.grizzly.http.util.Base64Utils;
/**
*
* @author Lijie
*
*/
public class Base64Util {
/**
* base64解密
*
* @param info
* @return
*/
public static byte[] decode(byte[] info) {
return Base64Utils.decode(info);
}
/**
* base64加密
*
* @param info
* @return
*/
public static byte[] encode(byte[] info) {
return Base64Utils.encodeToByte(info, true);
}
}
- 3.JsonUtils(需要导入jackson包)
package com.lijie.utils;
import java.io.IOException;
import org.apache.htrace.fasterxml.jackson.core.JsonParseException;
import org.apache.htrace.fasterxml.jackson.databind.JsonMappingException;
import org.apache.htrace.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author Lijie
*
*/
public class JsonUtils {
/**
* 转换json
*
* @param obj
* @return
* @throws Exception
*/
public static String ObjToJson(Object obj) throws Exception {
ObjectMapper om = new ObjectMapper();
String json = om.writeValueAsString(obj);
return json;
}
/**
* json转换
*
* @param json
* @param clazz
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static Object JsonToObj(String json, Class clazz) throws Exception {
ObjectMapper om = new ObjectMapper();
Object obj = om.readValue(json, clazz);
return obj;
}
}
- 4.Signature(MD5):
package com.lijie.utils;
import java.security.MessageDigest;
/**
*
* @author Lijie
*
*/
public class Signature {
public static String md5(String data, String pv) throws Exception {
// 将字符串转为字节数组
byte[] strBytes = (data + pv).getBytes();
// 加密器
MessageDigest md = MessageDigest.getInstance("MD5");
// 执行加密
md.update(strBytes);
// 加密结果
byte[] digest = md.digest();
// 返回
return byteArrayToHex(digest);
}
private static String byteArrayToHex(byte[] byteArray) {
// 首先初始化一个字符数组,用来存放每个16进制字符
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
// new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))
char[] resultCharArray = new char[byteArray.length * 2];
// 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符数组组合成字符串返回
return new String(resultCharArray);
}
}