- 创建DesUtil类
public class DesUtil {
private static String strDefaultKey = "mykey";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
public String getEncString(String strMing) throws Exception {
return byteArr2HexStr(encrypt(strMing.getBytes("UTF8")));
}
public String getDesString(String strMi) throws Exception {
return new String(decrypt(hexStr2ByteArr(strMi)), "UTF8");
}
public String getDesStr(String strMi) {
String ming = null;
try {
ming = new String(decrypt(hexStr2ByteArr(strMi)), "UTF8");
} catch (Exception e) {
ming = strMi;
}
return ming;
}
private static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
private static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
public DesUtil() throws Exception {
this(strDefaultKey);
}
public DesUtil(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
private byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
private String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
private byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
private Key getKey(byte[] arrBTmp) throws Exception {
byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) throws Exception {
DesUtil des = new DesUtil("自定义key");
try {
String test = "test";
System.out.println("加密前的字符:" + test);
System.out.println("加密后的字符:" + des.encrypt(test));
System.out.println("解密后的字符:" + des.decrypt(des.getEncString(test)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 创建ConvertPwdPropertyConfigurer类
public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer {
private String[] encryptPropNames = { "database.username", "database.password" };
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
DesUtil des;
try {
des = new DesUtil("自定义key");
for (String encryptName : encryptPropNames) {
String name = props.getProperty(encryptName);
if (null != name) {
props.setProperty(encryptName, des.decrypt(name));
}
}
super.processProperties(beanFactoryToProcess, props);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 修改配置文件 jdbc.properties
database.url=jdbc:mysql://*****?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
database.username=d581f01b3d48d27a
database.password=c0b756a69241a8c366b9121c14aa63f508a850df3b26ef05
- 修改spring-content.xml配置文件
将spring-context中的
<context:property-placeholder location="classpath:jdbc.properties" />
替换为
<bean id="propertyConfigurer"
class="com.dal.expand.ConvertPwdPropertyConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>