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

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

相关推荐
满天星83035772 小时前
【MySQL】索引
linux·服务器·数据库·mysql
InfinteJustice2 小时前
mysql如何设计积分系统_mysql流水账与余额对账
jvm·数据库·python
NotFound4862 小时前
Golang怎么实现防重复提交_Golang如何用Token机制防止表单重复提交【技巧】
jvm·数据库·python
2401_865439632 小时前
CSS如何实现图片自动裁剪填充_巧用object-fit属性控制尺寸
jvm·数据库·python
m0_748839492 小时前
HTML函数能否用液态金属散热提升性能_极端散热方案实测【汇总】
jvm·数据库·python
2301_803538952 小时前
mysql添加索引导致插入变慢怎么办_索引优化与异步处理方案
jvm·数据库·python
2301_782659182 小时前
如何防止SQL脏数据写入_利用触发器实现强一致性校验
jvm·数据库·python
2301_817672263 小时前
如何实现元素从底部进入视口时触发 sticky 定位
jvm·数据库·python
InfinteJustice3 小时前
mysql如何排查插件加载失败原因_mysql plugin目录与权限核对
jvm·数据库·python