import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
public class Test7 {
private static Key key;
private static String KEY_STR = "mykey";
static {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//对字符串进行加密,返回BASE64的加密字符串
public String getEncryptString(String str){
BASE64Encoder base64Encoder = new BASE64Encoder();
try
{
byte[] strBytes = str.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64Encoder.encode(encryptStrBytes);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
//对BASE64加密字符串进行解密
public static String getDecryptString(String str){
BASE64Decoder base64Decoder = new BASE64Decoder();
try
{
byte[] strBytes = base64Decoder.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return new String(encryptStrBytes,"UTF-8");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
配置文件加密后引用问题:
配置文件如下:
jdbc-driver=com.mysql.jdbc.Driver
jdbc-url=jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=UTF-8
jdbc-user=Ov4j7fKiCzY=
jdbc-password=Ov4j7fKiCzY=
第一步:
新建类继承PropertyPlaceholderConfigurer类,重写getDecryptString方法
package cn.kgc.tools;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private String[] encryptPropNames = {"jdbc-user", "jdbc-password"};
private static Key key;
private static String KEY_STR="mykey";
static{
try
{
KeyGenerator generator = KeyGenerator.getInstance("DES");
SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
generator=null;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
private boolean isEncryptProp(String propertyName)
{
for (String encryptName : encryptPropNames)
{
if (encryptName.equals(propertyName))
{
return true;
}
}
return false;
}
public static String getDecryptString(String str){
BASE64Decoder base64Decoder = new BASE64Decoder();
try
{
byte[] strBytes = base64Decoder.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return new String(encryptStrBytes,"UTF-8");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
protected String convertProperty(String propertyName, String propertyValue){
//如果在加密属性名单中发现该属性
if (isEncryptProp(propertyName)){
String decryptValue = getDecryptString(propertyValue);
System.out.println(decryptValue);
return decryptValue;
}else {
return propertyValue;
}
}
}
第二步:
引用配置文件:
<bean class="cn.kgc.tools.EncryptPropertyPlaceholderConfigurer" p:location="classpath:/properties.properties"></bean>
代替:
<context:property-placeholder location="classpath:/properties.properties" />
第三步:
数据源配置如下:
<!--dbcp数据源,只对当前项目有用-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc-driver}"/>
<property name="url" value="${jdbc-url}"/>
<property name="username" value="${jdbc-user}"/>
<property name="password" value="${jdbc-password}"/>
</bean>
|
|