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

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

相关推荐
ESDWAN3 分钟前
SD-WAN 是什么?工作原理、适用场景与选型要点
开发语言·网络·数据库·架构·php
m0_639906945 分钟前
MySQL 慢查询优化|explain执行计划详解
数据库·mysql
正儿八经的少年8 分钟前
MyBatis 一对一、一对多 SQL
数据库·sql·mybatis
Elastic 中国社区官方博客14 分钟前
Elastic 在《Forrester Wave™:2026 年第四季度智能运维平台》报告中被评为强劲表现者
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索
Apache IoTDB16 分钟前
从 IoTDB 到 CED-DB:哈尔滨工业大学探索云边端协同时序数据库
数据库·时序数据库·iotdb
闲云野鹤在人间22 分钟前
头秃修炼手册:StorageClass+StatefulSet+ETCD 全套渡劫笔记
linux·数据库·笔记·云原生·kubernetes·etcd
Decay__43 分钟前
03_MySQL 存储引擎与存储管理 —— InnoDB 深入、表空间/ibd、binlog/redo/undo 与双写缓冲
运维·数据库·sql·mysql·架构
瀚高PG实验室1 小时前
瀚高数据库提交数据后,是否需要COMMIT
数据库·postgresql·瀚高数据库
bksczm1 小时前
MySQL程序架构
数据库·mysql·架构
论文复现现场1 小时前
2026 低成本 AI Agent 模型怎么选?DeepSeek V4.1 Flash、Qwen3.8 Flash、GLM-5.3 Flash 对比
数据库·人工智能·qwen·ai agent·glm·deepseek