1. 程式人生 > >加密連線資料庫明文密碼

加密連線資料庫明文密碼

我的其他筆記有記錄過加密演算法,這裡不一一闡述,用DES對MySQL使用者名稱和密碼加密,然後在spring-dao.xml中配置中解密,需要新建一個解密類註冊到xml中。

解密類:

package com.mlr.util;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    // 需要加密的欄位陣列
private String[] encryptPropNames = { "jdbc.username", "jdbc.password" }; /** * 對關鍵的屬性進行轉換 */ @Override protected String convertProperty(String propertyName, String propertyValue) { if (isEncryptProp(propertyName)) { // 對已加密的欄位進行解密工作 String decryptValue = DESUtil.getDecryptString(propertyValue); return
decryptValue; } else { return propertyValue; } } /** * 該屬性是否已加密 * * @param propertyName * @return */ private boolean isEncryptProp(String propertyName) { // 若等於需要加密的field,則進行加密 for (String encryptpropertyName : encryptPropNames) { if
(encryptpropertyName.equals(propertyName)) return true; } return false; } }

加密類:

package com.mlr.util;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DES是一種對稱加密演算法,所謂對稱加密演算法即:加密和解密使用相同金鑰的演算法。
 * 
 * @author xiangze
 *
 */
public class DESUtil {

    private static Key key;
    // 設定金鑰key
    private static String KEY_STR = "myKey";
    private static String CHARSETNAME = "UTF-8";
    private static String ALGORITHM = "DES";

    static {
        try {
            // 生成DES演算法物件
            KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
            // 運用SHA1安全策略
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            // 設定上金鑰種子
            secureRandom.setSeed(KEY_STR.getBytes());
            // 初始化基於SHA1的演算法物件
            generator.init(secureRandom);
            // 生成金鑰物件
            key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 獲取加密後的資訊
     * 
     * @param str
     * @return
     */
    public static String getEncryptString(String str) {
        // 基於BASE64編碼,接收byte[]並轉換成String
        BASE64Encoder base64encoder = new BASE64Encoder();
        try {
            // 按UTF8編碼
            byte[] bytes = str.getBytes(CHARSETNAME);
            // 獲取加密物件
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化密碼資訊
            cipher.init(Cipher.ENCRYPT_MODE, key);
            // 加密
            byte[] doFinal = cipher.doFinal(bytes);
            // byte[]to encode好的String並返回
            return base64encoder.encode(doFinal);
        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        }
    }

    /**
     * 獲取解密之後的資訊
     * 
     * @param str
     * @return
     */
    public static String getDecryptString(String str) {
        // 基於BASE64編碼,接收byte[]並轉換成String
        BASE64Decoder base64decoder = new BASE64Decoder();
        try {
            // 將字串decode成byte[]
            byte[] bytes = base64decoder.decodeBuffer(str);
            // 獲取解密物件
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化解密資訊
            cipher.init(Cipher.DECRYPT_MODE, key);
            // 解密
            byte[] doFinal = cipher.doFinal(bytes);
            // 返回解密之後的資訊
            return new String(doFinal, CHARSETNAME);
        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println(getEncryptString("123456"));
    }

}

用加密類對賬戶和密碼進行加密後,jdbc.properties中配置如下

jdbc.username=WnplV/ietfQ=
jdbc.password=QAHlVoUc49w=

然後spring-dao.xml中改變配置:

 <!-- 1.配置資料庫相關引數properties的屬性:${url} -->
  <!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
  <bean class="com.mlr.util.EncryptPropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc.properties</value>
      </list>
    </property>
    <property name="fileEncoding" value="UTF-8" />
  </bean>