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

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

相关推荐
java_cj5 分钟前
MySQL 8.0 新特性深度解析:降序索引、Doublewrite Buffer 与 redo log 无锁优化
数据库·mysql
网管NO.111 分钟前
多表联查入门|INNER JOIN 内连接,关联查询基础(实操案例)
数据库·sql
devilnumber13 分钟前
MySQL 索引失效 20 例
数据库·mysql
念恒1230621 分钟前
MySQL事务(上)
数据库·mysql
devilnumber24 分钟前
MySQL 执行计划(EXPLAIN)背诵版
数据库·mysql
念恒1230628 分钟前
MySQL视图
数据库·mysql
我叫张小白。1 小时前
基于Redis的缓存架构与一致性保障体系
数据库·redis·缓存·架构
Omics Pro1 小时前
基因泰克:检测级虚拟细胞基准!大语言模型+智能体
大数据·数据库·人工智能·机器学习·语言模型·自然语言处理·r语言
Quincy_Freak1 小时前
工具分享|基于 SQLiteGo 的国产系统离线数据处理方案
大数据·数据库·数据分析·arm·国产系统·银河麒麟·aarch64
我是一颗柠檬1 小时前
【Redis】数据类型详解Day2(2026年)
数据库·redis·后端·缓存