一些关键的业务主键id, 通过post/get请求提交时, 需要进行加密/解密,
避免手动修改参数后提交, 提高安全性
包含:
-
对id进行加密
-
对加了密的id进行解密
-
通用加密方法 -- 对字符串进行加密
-
通用解密方法 -- 对字符串进行解密
-
Base64加密
-
Base64解密
-
import org.apache.commons.codec.binary.Base64;
-
import org.apache.commons.lang.StringUtils;
-
-
import javax.crypto.Cipher;
-
import javax.crypto.spec.SecretKeySpec;
-
import java.io.UnsupportedEncodingException;
-
-
/**
-
* 加密解密工具类
-
* 采用AES加密算法
-
*/
-
public class EncryptionUtil {
-
/**
-
* 密钥
-
*/
-
private static final String KEY = "xxx";
-
/**
-
* 密钥算法
-
*/
-
private static final String ALGORITHM = "AES";
-
/**
-
* 加解密算法/工作模式/填充方式
-
*/
-
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
-
-
/**
-
* 对id进行加密
-
* @param bizId 业务id
-
* @return 密文
-
*/
-
public static String encryptBizId(Integer bizId) {
-
if (bizId == null || bizId == 0) {
-
return null;
-
}
-
-
return encryptData(bizId.toString());
-
}
-
-
/**
-
* 对加了密的id进行解密
-
* @param ciphertext 密文
-
* @return 业务id
-
*/
-
public static Integer decryptBizId(String ciphertext) {
-
if (ciphertext == null || ciphertext.length() == 0) {
-
return null;
-
}
-
String plaintext = decryptData(ciphertext);
-
if (plaintext == null) {
-
return null;
-
}
-
-
return Integer.valueOf(plaintext);
-
}
-
-
/**
-
* 通用加密方法 -- 对字符串进行加密
-
* @param data 明文
-
* @return 密文
-
*/
-
public static String encryptData(String data) {
-
if (StringUtils.isEmpty(data)) {
-
return null;
-
}
-
-
try {
-
// SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
-
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
-
// 创建密码器
-
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
-
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
-
byte[] encrypted = cipher.doFinal(data.getBytes());
-
-
return byte2hex(encrypted).toLowerCase();
-
} catch (Exception e) {
-
LogUtil.error("EncryptionUtil#encryptData#data:" + data, e);
-
return null;
-
}
-
}
-
-
/**
-
* 通用解密方法 -- 对字符串进行解密
-
* @param data 密文
-
* @return 明文
-
*/
-
public static String decryptData(String data) {
-
if (StringUtils.isEmpty(data)) {
-
return null;
-
}
-
-
try {
-
// SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
-
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
-
// 创建密码器
-
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
-
cipher.init(Cipher.DECRYPT_MODE, keySpec);
-
-
return new String(cipher.doFinal(hex2byte(data)));
-
} catch (Exception e) {
-
LogUtil.debug("EncryptionUtil#decryptData#data" + data);
-
return null;
-
}
-
-
}
-
-
/**
-
* byte数组
-
* @param strhex hex字符串
-
* @return
-
*/
-
private static byte[] hex2byte(String strhex) {
-
if (strhex == null) {
-
return null;
-
}
-
int l = strhex.length();
-
if (l % 2 == 1) {
-
return null;
-
}
-
byte[] b = new byte[l / 2];
-
for (int i = 0; i != l / 2; i++) {
-
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
-
16);
-
}
-
return b;
-
}
-
-
/**
-
* byte数组 转 hex字符串
-
* @param b byte数组
-
* @return
-
*/
-
private static String byte2hex(byte[] b) {
-
String hs = "";
-
String stmp = "";
-
for (int n = 0; n < b.length; n++) {
-
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
-
if (stmp.length() == 1) {
-
hs = hs + "0" + stmp;
-
} else {
-
hs = hs + stmp;
-
}
-
}
-
return hs.toUpperCase();
-
}
-
-
public static void main(String[] args) throws Exception {
-
long lStart = System.currentTimeMillis();
-
String enString = encryptBizId(1);
-
-
long lUseTime = System.currentTimeMillis() - lStart;
-
System.out.println("加密后的字串是:" + enString);
-
System.out.println("加密耗时:" + lUseTime + "毫秒");
-
// 解密
-
lStart = System.currentTimeMillis();
-
Integer deString = EncryptionUtil.decryptBizId("84d692dcc66e5477d80151b7394227fa");
-
System.out.println("解密后的字串是:" + deString);
-
lUseTime = System.currentTimeMillis() - lStart;
-
System.out.println("解密耗时:" + lUseTime + "毫秒");
-
-
System.out.println(encryptBizId(391));
-
}
-
-
-
/**
-
* 加密
-
* @return
-
* @throws Exception
-
*/
-
public static String encryptInBase64(String data) {
-
if(StringUtils.isEmpty(data)) {
-
return null;
-
}
-
try {
-
return getBASE64(encryptData(data));
-
} catch (Exception e) {
-
LogUtil.error("", e);
-
return null;
-
}
-
}
-
-
/**
-
* 解密
-
* @return
-
* @throws Exception
-
*/
-
public static String desEncryptInBase64(String data) throws Exception {
-
if(StringUtils.isEmpty(data)) {
-
return null;
-
}
-
try {
-
return decryptData(getFromBASE64(data));
-
} catch (Exception e) {
-
LogUtil.error("", e);
-
return null;
-
}
-
}
-
-
/**
-
* 解析base64获取字符串
-
* @param s
-
* @return
-
*/
-
public static String getFromBASE64(String s) {
-
if (s == null) return null;
-
-
try {
-
return new String(Base64.decodeBase64(s.getBytes("utf-8")), "utf-8");
-
} catch (UnsupportedEncodingException e) {
-
return null;
-
}
-
}
-
-
/**
-
* 获取字符串的base64
-
* @param s
-
* @return
-
*/
-
public static String getBASE64(String s) {
-
if (s == null) return null;
-
try {
-
return new String(Base64.encodeBase64(s.getBytes("utf-8")), "utf-8");
-
} catch (Exception e) {
-
return null;
-
}
-
}
-
-
}
阅读(913) | 评论(0) | 转发(0) |