Spring 数据库配置文件进行解密操作,和网易大佬的技术面谈
} catch (Exception e) {
// log.error("加密错误,错误信息:", e);
throw new RuntimeException("加密错误,错误信息:", e);
}
return encryptedData;
}
/**
数据解密,算法(DES)
@param cryptData
@return 解密后的数据
*/
public static String decryptBasedDes(String cryptData) {
String decryptedData = null;
try {
// DES 算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(DES_KEY);
// 创建一个密匙工厂,然后用它把 DESKeySpec 转换成一个 SecretKey 对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 把字符串解码为字节数组,并解密
decryptedData = new String(cipher.doFinal(new Base64().decode(cryptData)));
} catch (Exception e) {
// log.error("解密错误,错误信息:", e);
throw new RuntimeException("解密错误,错误信息:", e);
}
return decryptedData;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="root";
String str2="1qaz2wsx@";
System.out.println(encryptBasedDes("root"));
System.out.println(encryptBasedDes("123456"));
System.out.println(decryptBasedDes("sxxYbV0F3Ek="));
System.out.println(decryptBasedDes("
OPmUtIQulsbfYjzGTMo08w=="));
// DES 数据加密
// String s1=encryptBasedDes(str);
// System.out.println(s1);
// DES 数据解密
// String s2=decryptBasedDes(s1);
// System.err.println(s2);
}
}
Spring 配置文件解密 加载配置信息
<bean
class="com.v.common.jurisdiction.SecretPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/sqlservice_datasource.properties</value>
</list>
</property>
</bean>
java 解密文件
package com.v.common.jurisdiction;
import java.util.Properties;
import com.v.common.password.DwDes;
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;
/**
类 : <code>SecretPropertyPlaceholderConfigurer.java</code><br/>
描述: <code>数据库文件进行解密操作</code>
@author 作者: {TinVie}
@version 版本: {V1.0 ,2018 年 7 月 14 日 下午 3:39:51}
@since JDK 版本: {jdk 1.8}
*/
public class SecretPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
public static final String JDBC_DATASOURCE_USERNAME_KEY = "jdbc.username";
public static final String JDBC_DATASOURCE_PASSWORD_KEY = "jdbc.password";
public static final String JDBC_DATASOURCE_URL_KEY = "jdbc.url";
public static final String JDBC_DATASOURCE_DRIVERCLASSNAME_KEY = "jdbc.driverClassName";
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
// 解密 账号
String username = props.getProperty(JDBC_DATASOURCE_USERNAME_KEY);
if (username != null) {
评论