数据库配置文件加密

数据库配置文件加密

2017-08-19 艳学网

艳学网Q群二维码


点击上方“艳学网”,申请加群
最新Java技术,第一时间送达!

前言

使用连接数据库加密的话,都会使用一个配置文件在配置文件中对数据库链接进行读写,通过加密方法把链接写到配置文件,然后读取的时候再使用解密方法。

1、xml配置

<!--引入属性文件 -->
    <!-- <context:property-placeholder location="classpath:dbconfig.properties" /> -->
    <bean id="propertyConfigurer"  
       class="com.commons.util.EncryptablePropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:dbconfig.properties</value>  
            </list>  
        </property>  
    </bean>

2、properties文件

#weixin-text-jiami
#jdbc.url=B7766E9B37CA2C3F5264E5FFC222BFB694BAC950B05FB1BACDD86EE58ADF8F5A76005B0310A595626ADFAE2B3f6933524C133F2CC14CCC028889F25C9DBECE4FEDE92F5267D957387F918EE1BB030FD5B6AA335DAA0433DF700450115E46974A4306785549265266
#jdbc.username=4245Be787DCA3B0D
#jdbc.password=412D17D8Ad47C901D8gg00DF62918A942A

3、加密工具

package com.commons.util;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class _desUtil {
    private static final String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish 

    //src为被加密的数据缓冲区(源) 
    public static byte[] encryptMode(byte[] keybyte, byte[] src) { 
        try { 
        //生成密钥 
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
            //加密 
            Cipher c1 = Cipher.getInstance(Algorithm); 
            c1.init(Cipher.ENCRYPT_MODE, deskey); 
            return c1.doFinal(src); 
        } 
            catch (java.security.NoSuchAlgorithmException e1) { 
            e1.printStackTrace(); 
        } 
            catch (javax.crypto.NoSuchPaddingException e2) { 
            e2.printStackTrace(); 
        } 
            catch (java.lang.Exception e3) { 
            e3.printStackTrace(); 
        } 
        return null; 
    } 

    //keybyte为加密密钥,长度为24字节 
    //src为加密后的缓冲区 
    public static byte[] decryptMode(byte[] keybyte, byte[] src) { 
        try { 
        //生成密钥 
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
            //解密 
            Cipher c1 = Cipher.getInstance(Algorithm); 
            c1.init(Cipher.DECRYPT_MODE, deskey); 
            return c1.doFinal(src); 
        } 
            catch (java.security.NoSuchAlgorithmException e1) { 
            e1.printStackTrace(); 
        } 
            catch (javax.crypto.NoSuchPaddingException e2) { 
            e2.printStackTrace(); 
        } 
            catch (java.lang.Exception e3) { 
            e3.printStackTrace(); 
        } 
        return null; 
    } 
    //转换成十六进制字符串 
    public static String byte2hex(byte[] b) { 
        String hs=""; 
        String stmp=""; 
        for (int n=0;n<b.length;n++) { 
            stmp=(java.lang.Integer.toHexString(b[n] & 0XFF)); 
            if (stmp.length()==1) hs=hs+"0"+stmp; 
            else hs=hs+stmp; 
            if (n<b.length-1) hs=hs+""; 
        } 
        return hs.toUpperCase(); 
    } 

    //16 进制 转 2 进制
    public static byte[] hex2byte(String hex) throws IllegalArgumentException {     
        if (hex.length() % 2 != 0) {     
            throw new IllegalArgumentException();     
        }     
        char[] arr = hex.toCharArray();     
        byte[] b = new byte[hex.length() / 2];     
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {     
            String swap = "" + arr[i++] + arr[i];     
            int byteint = Integer.parseInt(swap, 16) & 0xFF;     
            b[j] = new Integer(byteint).byteValue();     
        }     
        return b;     
    } 

    private static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    //加密
    public static String Encrypt(String str, byte[] key){
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] encrypt = encryptMode(key, str.getBytes());
        return byte2hex(encrypt);
    }

    //加密
    public static byte[] EncryptRetByte(byte[] src, byte[] key){
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] encrypt = encryptMode(key, src);
        return encrypt;
    }

    //解密
    public static String Decrypt(String str, byte[] key){
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] decrypt = decryptMode(key, hex2byte(str)); 
        return new String(decrypt);
    }

    public static void main(String arg[]){


        String str = "xxoo";
        String strKey = "0002000200020002";
        String s3 = Encrypt(str, hex2byte(strKey)); //加密
        String s4 = Decrypt(s3, hex2byte(strKey)); //解密
        System.out.println(s3);
        System.out.println(s4);


    }
}

4、Configurer

package com.commons.util;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static final String key = "0002000200020002";

    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
        throws BeansException {
            try {
                _desUtil des = new _desUtil();
                String username = props.getProperty("jdbc.username");
                if (username != null) {
                    props.setProperty("jdbc.username", des.Decrypt(username, des.hex2byte(key)));
                }

                String password = props.getProperty("jdbc.password");
                if (password != null) {
                    props.setProperty("jdbc.password", des.Decrypt(password, des.hex2byte(key)));
                }

                String url = props.getProperty("jdbc.url");
                if (url != null) {
                    props.setProperty("jdbc.url", des.Decrypt(url, des.hex2byte(key)));
                }

//              String driverClassName = props.getProperty("jdbc.driverClassName");
//              if(driverClassName != null){
//                  props.setProperty("jdbc.driverClassName", des.Decrypt(driverClassName, des.hex2byte(key)));
//              }

                super.processProperties(beanFactory, props);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BeanInitializationException(e.getMessage());
            }
        }
    }

源码下载

提示:如没源码下载,可开通vip权限获取源码,也可人工获取复制以下内容
转发至1个Java相关QQ群 + 分享到自己的QQ空间说说,
然后转发后截图,将截图发至:QQ邮箱490647751@qq.com。

全优快云的丰(好)色(se)博客,这里有书本看不到的Java技术,电影院禁播的电影,欢迎关注QQ群494808400。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值