java 加密jdbc连接
来源:互联网 发布:淘宝主播怎么做 编辑:程序博客网 时间:2024/06/16 01:49
1,准备获取配置文件信息类,以下是jdbc文件有2个数据源的情况下
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
String usernameW = props.getProperty("usernameW");
String usernameR = props.getProperty("usernameR");
if (usernameW != null) {
props.setProperty("usernameW", SecUtil.decrypt(usernameW));
}
if (usernameR != null) {
props.setProperty("usernameR", SecUtil.decrypt(usernameR));
}
String passwordW = props.getProperty("passwordW");
String passwordR = props.getProperty("passwordW");
if (passwordW != null) {
props.setProperty("passwordW", SecUtil.decrypt(passwordW));
}
if (passwordR != null) {
props.setProperty("passwordR", SecUtil.decrypt(passwordR));
}
String urlW = props.getProperty("urlW");
String urlR = props.getProperty("urlR");
if (urlW != null) {
props.setProperty("urlW", SecUtil.decrypt(urlW));
}
if (urlR != null) {
props.setProperty("urlR", SecUtil.decrypt(urlR));
}
String driverClassNameW = props.getProperty("driverW");
String driverClassNameR = props.getProperty("driverR");
if(driverClassNameW != null){
props.setProperty("driverW", SecUtil.decrypt(driverClassNameW));
}
if(driverClassNameR != null){
props.setProperty("driverR", SecUtil.decrypt(driverClassNameR));
}
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
2,准备加密类
private static final Logger logger = Logger.getLogger(SecUtil.class.getName());
/**
* 自定义 KEY
*/
private static byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x50,
0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
public static void main(String[] args) {
BufferedReader reader;
try {
String st = "";
do{
if("".equals(st)) {
System.out.println("AES加密与解密操作:");
System.out.println("\"E\":加密 \t\"D\":解密\t\t\"Q\":退出");
System.out.println("请输入操作代码:");
}
reader = new BufferedReader(new InputStreamReader(System.in));
st = reader.readLine();
if("E".equalsIgnoreCase(st)) {
System.out.println("请输入待加密字符串:");
st = reader.readLine();
if(!"".equals(st.trim())) {
System.out.println("加密前:" + st.trim());
System.out.println("加密后:" + encrypt(st.trim()) + "\n\n");
}
st = "";
}else if("D".equalsIgnoreCase(st)) {
System.out.println("请输入待解密字符串:");
st = reader.readLine();
if(!"".equals(st.trim())) {
System.out.println("解密前:" + st.trim());
System.out.println("解密后:" + decrypt(st.trim()) + "\n\n");
}
st = "";
}
} while(!st.equalsIgnoreCase("Q"));
} catch (Exception e) {
logger.error(e);
}
}
/**
* @Title: encrypt
* @author yunlin.liu
* @Description: 加密
* @param @param value
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String encrypt(String value) {
String s = null;
int mode = Cipher.ENCRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher.doFinal(value.getBytes());
s = String.valueOf(Hex.encodeHex(outBytes));
} catch (Exception e) {
logger.error(e);
}
return s;
}
/**
* @Title: decrypt
* @author yunlin.liu
* @Description: 解密
* @param @param value
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String decrypt(String value) {
String s = null;
int mode = Cipher.DECRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher
.doFinal(Hex.decodeHex(value.toCharArray()));
s = new String(outBytes);
} catch (Exception e) {
logger.error(e);
}
return s;
}
/**
* @Title: initCipher
* @author yunlin.liu
* @Description: 初始化密码
* @param @param mode
* @param @return
* @param @throws NoSuchAlgorithmException
* @param @throws NoSuchPaddingException
* @param @throws InvalidKeyException 设定文件
* @return Cipher 返回类型
* @throws
*/
private static Cipher initCipher(int mode) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Key key = new SecretKeySpec(keybytes, "AES");
cipher.init(mode, key);
return cipher;
}
3,spring配置文件
<bean id="pftpServicePlaceholderConfig"
class="com.wtjr.common.util.EncryptablePropertyPlaceholderConfigurer">
<property name="order" value="1"></property>
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
- java 加密jdbc连接
- 加密jdbc连接以及表敏感字段
- java jdbc 连接Oracle9i
- Java连接数据库技术JDBC
- java jdbc 连接db2
- java odbc-jdbc连接
- java jdbc 连接 数据库
- java JDBC连接MySQL
- Java jdbc连接oracle10g
- Java jdbc 连接数据库
- java JDBC 连接数据库
- [java]JDBC连接MySQL
- Java JDBC 连接Oracle
- java-------连接数据库(jdbc)
- postpresql Jdbc java连接
- java连接数据库(jdbc)
- java jdbc连接mysql
- java 连接 jdbc
- Failed to decode downloaded font 问题解决
- 网易视频云干货分享:2B产品经理如何养成
- sql分页语句
- C# VS2010 windows服务的安装
- python学习之 字符串前'r'的用法
- java 加密jdbc连接
- c++
- 设计HashMap中元素put顺序和输出顺序相同
- Web前端面试指导(四十五):页面渲染原理是什么?
- 《汇编语言》学习(十)call and ret
- 知道这20个正则表达式,能让你少写1,000行代码
- Oracle PLSQL 学习笔记一
- Unity3d 必备的几种优秀的设计模式
- linux内核源码总览之0000--------备分,未整理,文件访问1