数据库配置文件密码加解密

1.1、加密工具类

java 复制代码
package as.g.common.tools;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

public class MyEncDecTools {
    private static Key key;
    private static String KEY_STR = "mykey";

    static {
        try {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(KEY_STR.getBytes());
            generator.init(secureRandom);
            key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //对字符串进行加密,返回BASE64的加密字符串
    public  String getEncryptString(String str){
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try{
            byte[] strBytes = str.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return base64Encoder.encode(encryptStrBytes);
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //对BASE64加密字符串进行解密
    public static String getDecryptString(String str){
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try{
            byte[] strBytes = base64Decoder.decodeBuffer(str);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return new String(encryptStrBytes,"UTF-8");
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

1.2、数据库配置文件

1.2.1、properties

复制代码
#gDB.properties

1.2.2、xml

复制代码
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@192.168.1.1:1521:gdev</value>
		</property>
		<property name="username">
			<value>guser</value>
		</property>
		<property name="password">
			<value>gpassw0rd</value>
		</property>
	</bean>

1.3、解密工具类

1.3.1、重写org.apache.commons.dbcp.BasicDataSource

as.g.common.tools.MyBasicDataSource.java

1.3.2、重写org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

as.g.common.tools.MyPropertyPlaceholderConfigurer.java

相关推荐
djjjx.4 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
CoderYanger9 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
MSTcheng.10 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
Yeats_Liao11 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
鹤落晴春11 小时前
MySQL日志梳理
运维·数据库·mysql
五彩小白12 小时前
Redis的过期删除和内存淘汰机制
数据库·redis·缓存
学长毕设12 小时前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
apcipot_rain12 小时前
C++转Java日记(14)
数据库
龙腾AI白云16 小时前
数字孪生与计算机视觉:让机器看懂现场
大数据·数据库·机器学习·知识图谱·pygame
千寻技术帮17 小时前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue