加密連接數據庫明文密碼

我的其他筆記有記錄過加密算法,這裏不一一闡述,用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>

最近在整理一些資源工具,放在網站分享 http://tools.maqway.com
歡迎關注公衆號:麻雀唯伊 , 不定時更新資源文章,生活優惠,或許有你想看的

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章