前言: 使用若依框架时,被提要求配置文件数据源配置需要加密,不能以明文的形式展示,第一想法就就是德鲁伊,结果要求用户名也不能明文,于是查资料找到一个使用AES加密的方法。
首先添加AES加密工具类:
/**
* @ClassName AESUtils
* @Description AES相关工具类
* @author PHY
* @date 2021年12月17日
* @version V1.0
*/
public class AESUtils {
private final static String SECRET_KEY = "CCTC!@#AES$%2021";
private final static String IV = "CCTC!@#AES$%2021";
/**
* aes 加密
* @param data
* @return
*/
public static String encryptData(String data){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* aes 加密
* @param data
* @return
*/
public static String encryptDataByKey(String data,String key){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(key.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* aes 解密
* @param data 密文
* @return
*/
public static String decryptData(String data){
try {
byte[] encrypted1 =Base64.decodeBase64(data.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString.trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* aes 解密
* @param data 密文
* @return
*/
public static String decryptDataByKey(String data,String key){
try {
byte[] encrypted1 =Base64.decodeBase64(data.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(key.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// public static void main(String[] args) {
// String data = "123456";
// String enStr = AESUtils.encryptData(data);
// System.out.println("加密:"+enStr);
// String deStr = AESUtils.decryptData("D1YrF8XquJC9ox2F5L0JkQ==");
// System.out.println("解密:"+deStr);
// }
}
java
使用AES工具类最下方的main方法,生成数据源用户名和密码加密后的内容,放到配置文件中
然后找到若依框架多数据源配置类DruidConfig
具体路径是:src/main/java/com/ruoyi/framework/config/DruidConfig.java; 找到动态配置数据源dynamicDataSource,获取数据源配置信息,解密后再放回去。
这样就完成了若依框架数据源配置的加密了。
推荐阅读
1、数组拼接方法 2、SpringBoot + Vue 请求加密(采用国密算法) 3、记录一下vue打包成app(Hbuilder X) 4、Idea 2021.2.3破解过程 5、前端面试精选-基础篇