项目场景:
在系统开发的过程中有的时候,我们为了数据的安全性会把前端的数据进行加密,然后再在后端程序中对加密数据进行解密或解密,今天我们就在这里说2种简单的方法(DES加密、base64编码)
具体实现方案:
我们在前端页面进行加密的时候需要用到 JavaScript加密库CryptoJS v3.1.2 下载链接
页面代码:
1、DES加密
<script th:src="@{/lib/CryptoJS v3.1.2/rollups/tripledes.js}" charset="utf-8"></script>
<script th:src="@{/lib/CryptoJS v3.1.2/components/mode-ecb.js}" charset="utf-8"></script>
<script type="text/javascript">
/**
* @Title des加密
* @Author YangWanJie
* @Date 2020/12/10 16:36
* message 加密文本内容
* key 加密的盐
* @versioin V1.0
**/
$.fn.encryptByDES = function (message, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
};
</script>
java后端解密:
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @Title DES加密 解密算法
* @Author YangWanJie
* @Date 2020/5/21 17:02
* @versioin V1.0
**/
public class DesUtils {
private final static String DES = "DES";//方式
private final static String ENCODE = "UTF-8";//编码
private final static String defaultKey = "admin";//秘钥
/**
* @Title 使用 默认key 加密
* @Author YangWanJie
* @Date 2020/5/21 17:02
* @versioin V1.0
**/
public static String encrypt(String data) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* @Title 使用 默认key 解密
* @Author YangWanJie
* @Date 2020/5/21 17:02
* @versioin V1.0
**/
public static String decrypt(String data) throws IOException, Exception {
if (data == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* @Title 根据键值进行加密
* @Author YangWanJie
* @Date 2020/5/21 17:03
* @versioin V1.0
**/
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* @Title 根据键值进行解密
* @Author YangWanJie
* @Date 2020/5/21 17:03
* @versioin V1.0
**/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, key.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* @Title 根据键值进行加密
* @Author YangWanJie
* @Date 2020/5/21 17:04
* @versioin V1.0
**/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* @Title 根据键值进行解密
* @Author YangWanJie
* @Date 2020/5/21 17:04
* @versioin V1.0
**/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
}
2、前端base64编码
$.base64.encode('要被编码的文本内容');
Java后端解码
import org.apache.commons.codec.binary.Base64;
/**
* @Title base64 编码与解码
* @Author YangWanJie
* @Date 2020/6/24 19:38
* @versioin V1.0
**/
public class Base64Code {
/**
* @Title base64 编码
* @Author YangWanJie
* @Date 2020/6/24 19:37
* @versioin V1.0
**/
public static String encode(byte[] bytes) {
return new String(Base64.encodeBase64(bytes));
}
/**
* @Title base64 解码
* @Author YangWanJie
* @Date 2020/6/24 19:37
* @versioin V1.0
**/
public static String decode(byte[] bytes) {
return new String(Base64.decodeBase64(bytes));
}
}