Hibernate配置文件加密解决方案

package com.jyd.tools.db;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;

/**
 * AES加密工具
 */
public class SecUtil {

  private static byte[] keybytes = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46}; 
 
     public static void main(String[] args) throws Exception {
      String e1 = encrypt("username");
         System.out.println(e1);
         String e2 = decrypt(e1);
         System.out.println(e2);
        }
      /** 
         * 加密
         * @param value
         * @return
         */ 
       public static String encrypt(String value) { 
            
            String s=null;    
             int mode = Cipher.ENCRYPT_MODE;   
             try { 
                 Cipher cipher = initCipher(mode); 
      
                byte[] outBytes = cipher.doFinal(value.getBytes()); 
    
                 s = String.valueOf(Hex.encodeHex(outBytes));  //commons-codec-1.3.jar 需要此jar包
             } catch (Exception e) { 
                 e.printStackTrace(); 
            } 
             return s; 
         } 
      
         /**
          * 解密
          * @param value
          * @return
         */ 
         public static String decrypt(String value) { 
      
             String s = null; 
     
             int mode = Cipher.DECRYPT_MODE; 
      
             try { 
                 Cipher cipher = initCipher(mode); 
    
                 byte[] outBytes = cipher.doFinal(Hex.decodeHex(value.toCharArray())); 
      
                s = new String(outBytes); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
             return s; 
        } 
       
       private static Cipher initCipher(int mode) throws Exception{ 
             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
            Key key = new SecretKeySpec(keybytes, "AES"); 
             cipher.init(mode, key); 
            return cipher; 
         }
}

 

//配置加载类

package com.jyd.tools.db;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.connection.DriverManagerConnectionProvider;

public class CustomDriverManagerConnectionProvider extends DriverManagerConnectionProvider {

 public CustomDriverManagerConnectionProvider() {
   super();
 }

 @Override
 public void configure(Properties props) throws HibernateException {
  String newUser = SecUtil.decrypt(props.getProperty("hibernate.connection.user"));
  String newPwd = SecUtil.decrypt(props.getProperty("hibernate.connection.password"));
  props.setProperty("hibernate.connection.user", newUser);
        props.setProperty("hibernate.connection.password", newPwd);
        super.configure(props); 
 }
 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值