spring加密properties文件

一、這裏有一個jdbc.properties文件

jdbc.driverClass=oracle.jdbc.driver.OracleDriver
#jdbc.jdbcUrl = jdbc:oracle:thin:@192.168.100.15:1521:orcl
jdbc.jdbcUrl = jdbc:oracle:thin:@192.168.100.58:1521:firstdb
jdbc.user = wzj
#jdbc.password = wzj
#jia mi 3DES--下面是密文
jdbc.password = cRNm8QLUSKk=
jdbc.miniPoolSize = 1
jdbc.maxPoolSize = 20
jdbc.initialPoolSize = 1
jdbc.maxIdleTime = 25000
jdbc.acquireIncrement = 1
jdbc.checkoutTimeout=3000
#jdbc.checkoutTimeout=30000

 

二、繼承spring的PropertyPlaceholderConfigurer類,並進行修改

<P>&nbsp;</P><PRE class=html name="code">package com.ie.wzj.samples.account.util;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class JdbcEncryptFromPropertiesFile extends
  PropertyPlaceholderConfigurer {
 @Override
 protected void processProperties(
   ConfigurableListableBeanFactory beanFactory, Properties props)
   throws BeansException {
  String password = props.getProperty("jdbc.password");
  if (password != null) {//**解密過程**
   ThreeDes des = new ThreeDes(); // 實例化一個對像
   des.getKey(ThreeDes.generateKeyStr); // 生成密匙 (同一個key)
   // 解密jdbc.password屬性值,重新明文賦值
   String strDes = des.getDesString(password);// 密文解密成明文
   props.setProperty("jdbc.password", strDes);//賦值
  }
  super.processProperties(beanFactory, props);//調用父方法
 }
}
</PRE>
<P><BR>
&nbsp;</P>
<PRE></PRE>
<P>三、關於ThreeDes類:</P>
<PRE class=html name="code">package com.ie.wzj.samples.account.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
//有時候也使用apache組織的base64類:org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
/**
 *
 * DES對稱加密算法
 *
 * @author wzj
 *
 * @see 使用3DES加密與解密,可對byte[],String類型進行加密與解密 密文可使用String,byte[]存儲.
 *
 * @see 對稱加密算法就是能將數據加解密。加密的時候使用密鑰對數據進行加密,解密的時候使用同樣的密鑰對數據進行解密
 * @see DES是美國國家標準研究所提出的算法。由於加解密的數據安全性和密鑰長度成正比,故DES的56位密鑰已經形成安全隱患
 * @see 後來針對DES算法進行了改進,有了三重DES算法(也稱DESede或Triple-DES)。全名是TDEA:Triple Data
 * Encryption Algorithm
 * @see DESede針對DES算法的密鑰長度較短以及迭代次數偏少問題做了相應改進,提高了安全強度
 * @see 不過DESede算法處理速度較慢,密鑰計算時間較長,加密效率不高
 */
public class ThreeDes {
 private Key key; // 密鑰
 public static String generateKeyStr="abcdefghigklmnopqrstuvwxyz123456";
 private String encryptAlgorithm="DES";
 /**
  * 根據參數生成KEY
  *
  * @param strKey
  * 密鑰字符串
  */
 public void getKey(String strKey) {
  try {
   KeyGenerator _generator = KeyGenerator.getInstance(encryptAlgorithm);
   _generator.init(new SecureRandom(strKey.getBytes()));
   this.key = _generator.generateKey();
   _generator = null;
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * 加密String明文輸入,String密文輸出
  *
  * @param strMing
  * String明文
  * @return String密文
  */
 public String getEncString(String strMing) {
  byte[] byteMi = null;
  byte[] byteMing = null;
  String strMi = "";
  BASE64Encoder base64en = new BASE64Encoder();
  try {
   byteMing = strMing.getBytes("UTF8");
   byteMi = this.getEncCode(byteMing);
   strMi = base64en.encode(byteMi);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   base64en = null;
   byteMing = null;
   byteMi = null;
  }
  return strMi;
 }
 /**
  * 解密 以String密文輸入,String明文輸出
  *
  * @param strMi
  * String密文
  * @return String明文
  */
 public String getDesString(String strMi) {
  BASE64Decoder base64De = new BASE64Decoder();
  byte[] byteMing = null;
  byte[] byteMi = null;
  String strMing = "";
  try {
   byteMi = base64De.decodeBuffer(strMi);
   byteMing = this.getDesCode(byteMi);
   strMing = new String(byteMing, "UTF8");
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   base64De = null;
   byteMing = null;
   byteMi = null;
  }
  return strMing;
 }
 /**
  * 爲getEncString方法提供服務
  *
  * 加密以byte[]明文輸入,byte[]密文輸出
  *
  * @param byteS
  * byte[]明文
  * @return byte[]密文
  */
 private byte[] getEncCode(byte[] byteS) {
  byte[] byteFina = null;
  Cipher cipher;
  try {
   cipher = Cipher.getInstance(encryptAlgorithm);
   cipher.init(Cipher.ENCRYPT_MODE, key);
   byteFina = cipher.doFinal(byteS);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cipher = null;
  }
  return byteFina;
 }
 /**
  * 爲getDesString方法提供服務
  *
  * 解密以byte[]密文輸入,以byte[]明文輸出
  *
  * @param byteD
  * byte[]密文
  * @return byte[]明文
  */
 private byte[] getDesCode(byte[] byteD) {
  Cipher cipher;
  byte[] byteFina = null;
  try {
   cipher = Cipher.getInstance(encryptAlgorithm);
   cipher.init(Cipher.DECRYPT_MODE, key);
   byteFina = cipher.doFinal(byteD);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cipher = null;
  }
  return byteFina;
 }
 public static void main(String[] args) {
  String encryStr="teststr";
  ThreeDes des = new ThreeDes(); // 實例化一個對像
  des.getKey(generateKeyStr);
  String strEnc = des.getEncString(encryStr);// 加密字符串,返回String的密文
  System.out.println(strEnc);
  String strDes = des.getDesString(strEnc);// 把String 類型的密文解密
  System.out.println(strDes);
   
 }
}
</PRE>
<P>四、總結</P>
<P>1、可以針對properties文件所有屬性進行加密</P>
<P>2、關於BASE64Encoder加密的使用,亦可以使用apache的base64類</P>
<P>3、後續需要研究具體的算法DES DESede以及CSA</P>
<P>&nbsp;</P>
<P><BR>
<BR>
&nbsp;</P>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章