数据库加密

替换hibernate.xml中

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

<bean id="propertyConfigurer" class="com.loan.common.utils.PropertyEncrypt">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

 

 

package com.loan.common.utils;

import java.io.FileReader;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.loan.common.encryption.Base64;
import com.qcash.rsa.server.DecryptionRSASecret;


/**
 * Purpose: Spring-Hibernate Config xml database.properties content edcrypt
 * @author Hermanwang
 * @since  JDK 1.8
 * @date   2018年7月30日
 * @MaintenancePersonnel Hermanwang
 */
public class PropertyEncrypt extends PropertyPlaceholderConfigurer {
    /**
     * 系统日志记录对象
     */
    private static final Log LOGGER = LogFactory.getLog(PropertyEncrypt.class);
    /**
     * 驱动
     */
    private String JDBC_DRIVER = "jdbc.driver";
    /**
     * Web 数据库URL
     */
    private String JDBC_URL = "jdbc.url";
    /**
     * Web 数据库用户名称
     */
    private String JDBC_USER = "jdbc.user";
    /**
     * Web 数据库用户密码
     */
    private String JDBC_PASSWORD = "jdbc.password";

    /**
     * (non-Javadoc)
     * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#processProperties(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.Properties)
     */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {
        FileReader fReader = null;
        try {
            fReader = new FileReader(PropertiesManager.getProValueByKey(PropertiesManager.ENCRYPTION_PATH));
            Map<String, String> maps = new HashMap<String, String>();
            maps.put(JDBC_DRIVER, props.getProperty(JDBC_DRIVER));
            maps.put(JDBC_URL, props.getProperty(JDBC_URL));
            maps.put(JDBC_USER, props.getProperty(JDBC_USER));
            maps.put(JDBC_PASSWORD, props.getProperty(JDBC_PASSWORD));
            // 解密
            maps = DBdecrypt.decryptMap(fReader, maps);
            // Driver
            props.setProperty(JDBC_DRIVER, maps.get(JDBC_DRIVER));
            // web url
            props.setProperty(JDBC_URL, maps.get(JDBC_URL));
            // web username
            props.setProperty(JDBC_USER, maps.get(JDBC_USER));
            // web password
            props.setProperty(JDBC_PASSWORD, maps.get(JDBC_PASSWORD));
            super.processProperties(beanFactoryToProcess, props);
        } catch (Exception e) {
            LOGGER.error(this.getClass().getName() + ".processProperties() is error --> " + e);
        } finally {
            try {
                fReader.close();
            } catch (IOException ioe) {
                LOGGER.error(this.getClass().getName() + ".processProperties() try catch finally is error --> " + ioe);
            }
        }
    }
}

 

 

package com.loan.common.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Iterator;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import sun.misc.BASE64Decoder;
/**
 * Purpose: 数据库解密
 * @author Hermanwang
 * @since  JDK 1.8
 * @date   2018年11月21日
 * @MaintenancePersonnel Hermanwang
 */
public class DBdecrypt {
    
    private static Cipher cipher;

    static {
        try {
            cipher = Cipher.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }
    /**
     * 得到私钥
     * 
     * @param key
     *            密钥字符串(经过base64编码)
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String key) throws Exception {
        byte[] keyBytes;
        keyBytes = (new BASE64Decoder()).decodeBuffer(key);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
    /**
     * Purpose:批量解密
     * @author Hermanwang
     * @param fr
     * @param maps
     * @return
     * @return Map<String,String>
     */
    public static Map<String, String> decryptMap(FileReader fr, Map<String, String> maps) {
        try {
            BufferedReader br = new BufferedReader(fr);
            String privateKeyString = "";
            String str;
            while ((str = br.readLine()) != null) {
                privateKeyString += str;
            }
            br.close();
            fr.close();
            cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));
            Iterator it = maps.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                maps.put(entry.getKey().toString(), new String(cipher.doFinal((new BASE64Decoder()).decodeBuffer(entry.getValue().toString()))));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return maps;
    }
}


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SDS(SQL Data Security)是一款专门用于SQL数据库文件加密的保密软件。通过加密和访问控制技术,可控制SQL数据库文件无法拷贝,禁止导出,备份加密,远程销毁,脱离环境无法打开等等。 SDS分为管理程序和控制程序,需要在安装SQL Server数据库的机器上安装控制程序和管理程序,可在局域网内安装管理程序对SDS软件进行远程操控,安装后,所有数据库文件将处于加密状态,即使将文件拷贝出去也无法使用。同时,通过SQL企业管理器备份出来的数据也是加密的,离开本机环境将无法使用,SDS可禁止SQL组件导出数据。 SDS与数据库应用程序及数据库大小无关,后台实时监控数据的写入和读取,不会影响应用程序正常调用数据。 SDS的特点如下: 1.安装,维护简单。一键式安装,配套安装使用教程,专业的售后维护团队。 2.后台运行,实时监控数据库的读写。 3.数据库数据拷贝离开本机环境无法使用。本机环境内则不受影响。 4.与应用程序无关,不影响应用程序使用。ERP、PDM、等管理系统可以正常调用数据库文件,和正常操作一样。 5.与数据库大小无关。 6.通过设置,可本地或管理端临时禁用数据库。黑客来袭或者紧急情况可以通过本软件提供的禁用数据库功能使数据库文件无法打开,即使在本机环境依然无法正常打开使用,之后可恢复正常状态。 7.可设定云端验证。当服务器被盗时,可以是服务器电脑在本公司环境外部无法正常开机,卸载硬盘更换电脑依然无法正常开机,即数据库文件不会被外人获取。 SDS目前支持SQL2000、SQL2005、SQL2008、SQL2012。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值