Skip to content

Commit

Permalink
配置文件中的密码使用工具类DESUtil.java加密
Browse files Browse the repository at this point in the history
  • Loading branch information
shuzheng committed Feb 4, 2017
1 parent 950799b commit beb85a8
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 206 deletions.
4 changes: 2 additions & 2 deletions zheng-cms/zheng-cms-dao/src/main/resources/jdbc.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
master.jdbc.driver=com.mysql.jdbc.Driver
master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
master.jdbc.username=root
master.jdbc.password=123456
master.jdbc.password=RwfC38sMIAc=

slave.jdbc.driver=com.mysql.jdbc.Driver
slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
slave.jdbc.username=root
slave.jdbc.password=123456
slave.jdbc.password=RwfC38sMIAc=
14 changes: 7 additions & 7 deletions zheng-cms/zheng-cms-dao/src/main/resources/redis.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ip=127.0.0.1
port=6379
password=
max_active=500
max_idle=5
max_wait=10000
timeout=10000
master.redis.ip=127.0.0.1
master.redis.port=6379
master.redis.password=z4SkijYyEBc=
master.redis.max_active=500
master.redis.max_idle=5
master.redis.max_wait=10000
master.redis.timeout=10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.zheng.common.plugin;

import com.zheng.common.util.DESUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
* 支持加密配置文件插件
* Created by ZhangShuzheng on 2017/2/4.
*/
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

private String[] propertyNames = {
"master.jdbc.password", "slave.jdbc.password", "master.redis.password"
};

/**
* 解密指定propertyName的加密属性值
* @param propertyName
* @param propertyValue
* @return
*/
@Override
protected String convertProperty(String propertyName, String propertyValue) {
for (String p : propertyNames) {
if (p.equalsIgnoreCase(propertyName)) {
return DESUtil.getDecryptString(propertyValue);
}
}
return super.convertProperty(propertyName, propertyValue);
}

}
83 changes: 83 additions & 0 deletions zheng-common/src/main/java/com/zheng/common/util/DESUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.zheng.common.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
* DES加密算法工具类
* Created by ZhangShuzheng on 2017/2/4.
*/
public class DESUtil {

private static Key key;
private static String KEY_STR = "zheng"; // 密钥
private static String CHARSETNAME = "UTF-8"; // 编码
private static String ALGORITHM = "DES"; // 加密类型

static {
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* 对str进行DES加密
* @param str
* @return
*/
public static String getEncryptString(String str) {
BASE64Encoder base64encoder = new BASE64Encoder();
try {
byte[] bytes = str.getBytes(CHARSETNAME);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return base64encoder.encode(doFinal);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* 对str进行DES解密
* @param str
* @return
*/
public static String getDecryptString(String str) {
BASE64Decoder base64decoder = new BASE64Decoder();
try {
byte[] bytes = base64decoder.decodeBuffer(str);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return new String(doFinal, CHARSETNAME);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
String[] keys = {
"", "123456"
};
System.out.println("key | encryptString | decryptString");
for (String key : keys) {
System.out.print(key + " | ");
String encryptString = getEncryptString(key);
System.out.print(encryptString + " | ");
String decryptString = getDecryptString(encryptString);
System.out.println(decryptString);
}
}

}
Loading

0 comments on commit beb85a8

Please sign in to comment.