jdbc.properties 文件
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://localhost:3306
#username=
#password=
driver=D0E1022D91B6143BB48A3A4EFF10C89706FE0521E63A0E30
url=9BA61C333D389C6D72A11A520D6B0D98B5F27DD7862B97AD1E5D6F1D97DFC780C2FCE5AB875FC8539C5852EF64BCA501135E694B17BB7891F9F3978591262C403E7002DD6456B242260687E468A8305A09D2EAF3239223A7A3466C3CB57B4FAB26C671113177859FEA1B5D10CEFF1DD7AC5F37368BCA0E2F7078868018E81B43
username=0387C44D09743D74
password=89E7CDE9F8A56468
spring 配置
在applicationContext.xml中加入
<!-- 对JDBC配置进行解密 -->
<!-- 这里的class是解密的类 -->
<bean class="com.*.*.util.JDBCEncrypt"
p:location="classpath:jdbc.properties" p:fileEncoding="utf-8" />
<!-- 引入jdbc配置文件 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${driver}" p:url="${url}"
p:username="${username}" p:password="${password}" />
<bean class="com.*.*.util.JDBCEncrypt"
p:location="classpath:jdbc.properties" p:fileEncoding="utf-8" />
<!-- 引入jdbc配置文件 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${driver}" p:url="${url}"
p:username="${username}" p:password="${password}" />
JDBCEncrypt类
package com.*.*.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 JDBCEncrypt extends PropertyPlaceholderConfigurer {
private static final String key = "0002002000020002";
protected void processProperties(
ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
Des des = new Des();
String username = props.getProperty("username");
if (username != null) {
props.setProperty("username",
des.Decrypt(username, des.hex2byte(key)));
}
String password = props.getProperty("password");
if (password != null) {
props.setProperty("password",
des.Decrypt(password, des.hex2byte(key)));
}
String url = props.getProperty("url");
if (url != null) {
props.setProperty("url",
des.Decrypt(url, des.hex2byte(key)));
}
String driverClassName = props.getProperty("driver");
if (driverClassName != null) {
props.setProperty("driver",
des.Decrypt(driverClassName, des.hex2byte(key)));
}
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}
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 JDBCEncrypt extends PropertyPlaceholderConfigurer {
private static final String key = "0002002000020002";
protected void processProperties(
ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
Des des = new Des();
String username = props.getProperty("username");
if (username != null) {
props.setProperty("username",
des.Decrypt(username, des.hex2byte(key)));
}
String password = props.getProperty("password");
if (password != null) {
props.setProperty("password",
des.Decrypt(password, des.hex2byte(key)));
}
String url = props.getProperty("url");
if (url != null) {
props.setProperty("url",
des.Decrypt(url, des.hex2byte(key)));
}
String driverClassName = props.getProperty("driver");
if (driverClassName != null) {
props.setProperty("driver",
des.Decrypt(driverClassName, des.hex2byte(key)));
}
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}
Des类 (加密和解密的类)
package com.*.*.util;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Des {
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 = "123456";// 要加密的信息
String strKey = "0002002000020002";
String s3 = Encrypt(str, hex2byte(strKey));// 加密
String s4 = Decrypt(s3, hex2byte(strKey));// 解密
System.out.println("加密后的信息 " + s3);
System.out.println("解密后的信息 " + s4);
}
}
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Des {
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 = "123456";// 要加密的信息
String strKey = "0002002000020002";
String s3 = Encrypt(str, hex2byte(strKey));// 加密
String s4 = Decrypt(s3, hex2byte(strKey));// 解密
System.out.println("加密后的信息 " + s3);
System.out.println("解密后的信息 " + s4);
}
}