再过两个月就大学毕业一年了,分享一下公司项目中的工具类。
目前应用场景:比如查看详细信息时,传递的数据id,导致出现安全漏洞。
jsp页面,对数据id进行加密,请求到后台再对参数进行解密
<a href="<%=path%>/url?id=${md:encrypt(item.ID)}" >查看详情</a>
在JSP页面中,引入tld
<%@ taglib uri="/WEB-INF/tlds/md.tld" prefix="md"%>
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 functions library</description>
<display-name>JSTL functions sys</display-name>
<tlib-version>1.1</tlib-version>
<short-name>md</short-name>
<uri>xxx</uri>
<function>
<description>加密</description>
<name>encrypt</name>
<function-class>packageName.Des</function-class>
<function-signature>java.lang.String encrypt(java.lang.String)</function-signature>
<example>${md:encrypt(str)}</example>
</function>
<function>
<description>解密</description>
<name>decrypt</name>
<function-class>packageName.Des</function-class>
<function-signature>java.lang.String decrypt(java.lang.String)</function-signature>
<example>${md:decrypt(str)}</example>
</function>
</taglib>
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import org.springframework.util.Base64Utils;
/**
* DES加密工具类
* modify time 2019 0909 14:04
*
*/
public class Des {
private static final String algorithm = "DES";
private static final String key = "11111111";
/**
* 加密
*
* @return
* @throws Exception
*/
public static String encrypt(String data){
try {
byte[] bt = encrypt(data.getBytes(), key.getBytes());
return Base64Utils.encodeToString(bt);
}catch (Exception e){
return null;
}
}
/**
* 解密
*
* @return
* @throws Exception
*/
public static String decrypt(String data){
if (data == null) return null;
try{
byte[] bt = decrypt(Base64Utils.decodeFromString(data), key.getBytes());
return new String(bt);
}catch (Exception e){
return null;
}
}
/**
* 根据键值进行加密
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
return initCipher(data, key, Cipher.ENCRYPT_MODE);
}
/**
* 根据键值进行解密
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
return initCipher(data, key, Cipher.DECRYPT_MODE);
}
public static byte[] initCipher(byte[] data, byte[] key, int decryptMode) throws Exception {
/** 生成一个可信任的随机数源 **/
SecureRandom sr = new SecureRandom();
/** 从原始密钥数据创建DESKeySpec对象 **/
DESKeySpec dks = new DESKeySpec(key);
/** 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 **/
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
/** 将DESKeySpec对象转换成SecretKey对象 **/
SecretKey securekey = keyFactory.generateSecret(dks);
/** Cipher对象实际完成加密或解密操作 **/
Cipher cipher = Cipher.getInstance(algorithm);
/** 用密钥初始化Cipher对象 **/
cipher.init(decryptMode, securekey, sr);
return cipher.doFinal(data);
}
public static void main(String[] args) throws Exception {
// 待加密内容
String data = "helloworld";
// 密码,长度要是8的倍数
//加密
String str = Des.encrypt(data);
System.out.println(str);
//解密
System.out.println(Des.decrypt(str));
}
}
输出:MVvEhelWxQjoO6DI4SvW7w==
helloworld