DES加密数据库账号密码

DES加密数据库账号密码

实现对连接数据库账号密码的加密

DESUtil:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@SuppressWarnings("restriction")
public class DESUtil {

private static Key key;

private static String KEY_ETR="mykey";
private static String CHARSETNAME="utf-8";
private static String ALGORITHM="DES";
private static String OFWAY="SHA1PRNG";
static{
try {
//生成DES算法
KeyGenerator generator=KeyGenerator.getInstance(ALGORITHM);
//运行SHA1安全策略
SecureRandom secureRandom=SecureRandom.getInstance(OFWAY);
//设置秘钥种子
secureRandom.setSeed(KEY_ETR.getBytes());
//初始化基于SHA1秘钥算法对象
generator.init(secureRandom);
//生成秘钥对象
key=generator.generateKey();
generator=null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 加密信息(在main函数中跑出来,放在jdbc连接字段,调用getDecryptString去解析加密字段)
* @param str
* @return
*/
public static String getEncryptString(String str){
// 基于BASE64编码,接收byte[]并转换成String
BASE64Encoder base64Encoder=new BASE64Encoder();
try {
//按照utf-8编码
byte[] bytes = str.getBytes(CHARSETNAME);
//获取加密后的对象
Cipher cipher=Cipher.getInstance(ALGORITHM);
//初始化密码信息
cipher.init(Cipher.ENCRYPT_MODE, key);
//加密
byte[] doFinal = cipher.doFinal(bytes);
//将加密后的字节,encode成string并且返回
return base64Encoder.encode(doFinal);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
/**
* 获取解密后信息
* @param str
* @return
*/
public static String getDecryptString(String str){

BASE64Decoder base64Decoder=new BASE64Decoder();
try{
//将字符串decode成byte[]
byte[] decodeBuffer = base64Decoder.decodeBuffer(str);
//获取解密对象
Cipher cipher=Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
//解密
byte[] doFinal = cipher.doFinal(decodeBuffer);
//返回解密后的信息
return new String(doFinal, CHARSETNAME);
}catch(Exception e){
throw new RuntimeException(e);
} finally {
}
}

public static void main(String args[]){
String encryptString1 = getEncryptString("root");
String encryptString2 = getEncryptString("123456");
System.out.println(encryptString1);
System.out.println(encryptString2);
}

}

EncryptPropertyPlaceholderConfigurer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{

//设置加密的字段数组
private String encryptPropertyNames[]={"jdbc.username","jdbc.password"};
/*
* 获取解密后的数据(转换)
*/
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if(isEncryptProp(propertyName)){
propertyValue=DESUtil.getDecryptString(propertyValue);
return propertyValue;
}else{
return propertyValue;
}
}
/*
* 判断该字段是否已经加密
*/
private boolean isEncryptProp(String propertyName) {
for (String encryptPropertyName : encryptPropertyNames) {
if(encryptPropertyName.equals(propertyName)){
return true;
}
}
return false;
}
}

DES加密数据库账号密码
http://milkyaw.online/2024/03/08/DES加密数据库账号密码/
作者
Milkyaw
发布于
2024年3月8日
许可协议