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

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

相关推荐
l12586515 分钟前
# RAG上线评估指标体系:六大核心指标与压测实战全解析
数据库·人工智能·python·mysql·langchain·milvus
一只鹿鹿鹿39 分钟前
数据资产管理解决方案(Word文件)
大数据·数据库·安全·web安全·系统安全
2401_8949155342 分钟前
Geo 优化源码部署实战:环境配置、参数调优完整指南
运维·服务器·数据库·tcp/ip·安全
FfHUCisI1 小时前
Golang database/sql 标准库基础
数据库·sql·golang
zhanghaha13141 小时前
Python进阶教程:13_math 模块 —— 新手完全指南
数据库·python·机器学习
__zRainy__1 小时前
Node系列 · 数据库:单表查询
数据库·后端·mysql·node.js
xcLeigh1 小时前
聊聊数据库迁移工具怎么从单机走向“云+端+服务”,KDMS架构拆解
数据库·架构·数据库迁移·kes·kdms·架构拆解
St_rive2 小时前
selenium cookie的处理
数据库·selenium·测试工具
辻弋2012 小时前
一键优化电源计划、游戏模式、独显强制、禁用后台录制——DeltaForceBooster专治三角洲行动卡顿掉帧,所有改动可一键还原
服务器·数据库·windows·游戏·电脑·php
Wang's Blog3 小时前
PostgreSQL笔记51: 基于 pgvector 构建企业级智能问答系统
数据库·笔记·postgresql