
赵海滨
北京北科博研科技有限公司
序言
随着信息化的发展,加密便成了信息保护的手段。对于那些重要的数据信息(密码)或配置信息(数据连接配置)我们要进行一定程度的加密处理,防止其以明文形式公开给程序。接下来将介绍Spring2.5配置文件加密的方案之一。
方案原理:
通过先加密 *.property属性文件中变量值,再Spring读取并解密属性文件中变量值。从而将解密后正确的配置在程序运行之前完整的加载到内存之中,供程序正常的运行。话不多说,接下来就开始我们方案配置吧。本次演示如何对系统邮件服务器配置信息进行加密。
一、新建属性文件mail.properties
内容如下:
#邮箱配置
mail.host=mail. ***.com
mail.username=abc@***.com
mail.password=YH4pn5nEJsOOkJRKWVTzDg==
#公用设置
mail.send=true
目前,只对 mail.password 属性进行加密处理。
二、配置加密工具类
本人自己编写,适合本人项目。大家也可因自己需求进行修改。
1.加密解密类(EncryptPropertyFile.java),有这个类我们可以生成我们想要的密文
“YH4pn5nEJsOOkJRKWVTzDg==”
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASEDecoder;
import sun.misc.BASEEncoder;
public class EncryptPropertyFile {
private Key key; // 密钥
public static String GENERATE_KEY_STR =
"ABCDEFGHIGKLMNOPQRSTUVWXYZ01234567abcdefghijklmnopqrstuvwxyz";// 密文很重要,
可以设置自己的
private String encryptAlgorithm = "DES";
public void initKey(String strKey) {
try {
KeyGenerator _generator =
KeyGenerator.getInstance(encryptAlgorithm);
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASEEncoder baseen = new BASEEncoder();
try {
byteMing = strMing.getBytes("UTF-8");
byteMi = this.getEncCode(byteMing);
strMi = baseen.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
}
baseen = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
public String getDesString(String strMi) {
BASEDecoder baseDe = new BASEDecoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = baseDe.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
baseDe = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
String encryStr = "bkhrms";//需要加密的明文
EncryptPropertyFile des = new EncryptPropertyFile(); // 实例化一个对像
des.initKey(EncryptPropertyFile.GENERATE_KEY_STR);
String strEnc = des.getEncString(encryStr);// 加密字符串,返回String的密文System.out.println(strEnc);
String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
System.out.println(strDes);
}
}
2.配置加密类(PropertiesFileConfiger.java)
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.BeansException;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
//ZhaoHb2013.07.12
public class PropertiesFileConfiger extends PropertyPlaceholderConfigurer{
private String encryptKey = null;
private Set private EncryptPropertyFile encryptFile = new EncryptPropertyFile(); private Properties props = null; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { if(encryptSet==null){ if(encryptKey!=null){ String[] keys = encryptKey.split(";"); if(keys != null){ encryptSet = new HashSet for(String key:keys){ encryptSet.add(key); } } encryptFile.initKey(EncryptPropertyFile.GENERATE_KEY_STR); } } if(encryptSet!=null){ String pvalue = null,tvalue=null; for(String key:encryptSet){ pvalue = props.getProperty(key); if (pvalue != null) { //解密,并重新设置 tvalue = encryptFile.getDesString(pvalue); props.setProperty(key, tvalue); //System.out.println("KEY:"+key+ } } } this.props = props; super.processProperties(beanFactory, props); } public String getEncryptKey() { return encryptKey; } public void setEncryptKey(String encryptKey) { this.encryptKey = encryptKey; } public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } } 三、Spring配置如下 Configurer Bean配置是关键,encryptKey 代表需要加密处理的属性变量. Spring配置加密方案结束语 赠人玫瑰,手留余香。我认为人类的一切智慧只包含在两个字里面——分享。笔者结合自身多年研发工作经验,加之对Spring的浅显理解完成此文,希望能够对大家有所帮助。
