数据库配置文件加密">数据库配置文件加密
2017-08-19 艳学网
点击上方“艳学网”,申请加群
最新Java技术,第一时间送达!
前言
使用连接数据库加密的话,都会使用一个配置文件在配置文件中对数据库链接进行读写,通过加密方法把链接写到配置文件,然后读取的时候再使用解密方法。
xml配置">1、xml配置
xml"><!--引入属性文件 -->
<!-- <context:property-placeholder location="classpath:dbconfig.properties" /> -->
<bean id="propertyConfigurer"
class="com.commons.util.EncryptablePropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
2、properties文件
#weixin-text-jiami
#jdbc.url=B7766E9B37CA2C3F5264E5FFC222BFB694BAC950B05FB1BACDD86EE58ADF8F5A76005B0310A595626ADFAE2B3f6933524C133F2CC14CCC028889F25C9DBECE4FEDE92F5267D957387F918EE1BB030FD5B6AA335DAA0433DF700450115E46974A4306785549265266
#jdbc.username=4245Be787DCA3B0D
#jdbc.password=412D17D8Ad47C901D8gg00DF62918A942A
加密工具">3、加密工具
java">package com.commons.util;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class _desUtil {
private static final String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish
//src为被加密的数据缓冲区(源)
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
}
catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
}
catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//keybyte为加密密钥,长度为24字节
//src为加密后的缓冲区
public static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
}
catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//转换成十六进制字符串
public static String byte2hex(byte[] b) {
String hs="";
String stmp="";
for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+"";
}
return hs.toUpperCase();
}
//16 进制 转 2 进制
public static byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
//加密
public static String Encrypt(String str, byte[] key){
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] encrypt = encryptMode(key, str.getBytes());
return byte2hex(encrypt);
}
//加密
public static byte[] EncryptRetByte(byte[] src, byte[] key){
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] encrypt = encryptMode(key, src);
return encrypt;
}
//解密
public static String Decrypt(String str, byte[] key){
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] decrypt = decryptMode(key, hex2byte(str));
return new String(decrypt);
}
public static void main(String arg[]){
String str = "xxoo";
String strKey = "0002000200020002";
String s3 = Encrypt(str, hex2byte(strKey)); //加密
String s4 = Decrypt(s3, hex2byte(strKey)); //解密
System.out.println(s3);
System.out.println(s4);
}
}
4、Configurer
java">package com.commons.util;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static final String key = "0002000200020002";
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
_desUtil des = new _desUtil();
String username = props.getProperty("jdbc.username");
if (username != null) {
props.setProperty("jdbc.username", des.Decrypt(username, des.hex2byte(key)));
}
String password = props.getProperty("jdbc.password");
if (password != null) {
props.setProperty("jdbc.password", des.Decrypt(password, des.hex2byte(key)));
}
String url = props.getProperty("jdbc.url");
if (url != null) {
props.setProperty("jdbc.url", des.Decrypt(url, des.hex2byte(key)));
}
// String driverClassName = props.getProperty("jdbc.driverClassName");
// if(driverClassName != null){
// props.setProperty("jdbc.driverClassName", des.Decrypt(driverClassName, des.hex2byte(key)));
// }
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}
源码下载
提示:如没源码下载,可开通vip权限获取源码,也可人工获取复制以下内容
转发至1个Java相关QQ群 + 分享到自己的QQ空间说说,
然后转发后截图,将截图发至:QQ邮箱490647751@qq.com。
全CSDN的丰(好)色(se)博客,这里有书本看不到的Java技术,电影院禁播的电影,欢迎关注QQ群494808400。