天天看點

SpringBoot配置檔案密碼等重要資訊加密

随機密碼生成線上工具:

https://suijimimashengcheng.51240.com/

思路:配置檔案使用加密資訊,然後加載資料庫密碼時重新加載連接配接配置實作解密連接配接資料庫即可。

/**
 * ClassName: TokenCleanTimer
 * 資料源 配置檔案 賬号、密碼加密 類
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class AutoConfigDataSource {
	@Resource
	DataSourceProperties properties;
	//	秘鑰:随機生成的128位字元串
	private final static String PWD = "76Y7kHp3OFR2Twwzgo";
	@Bean
	public DataSource  dataSource() throws Exception {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(properties.getUrl());		 		
		dataSource.setUsername(properties.getUsername());
//		擷取配置中的加密密碼,解密之後置入dataSource,使用預設秘鑰	
		dataSource.setPassword(SymmetricEncoder.AESDncode(PWD,properties.getPassword()));
		dataSource.setDriverClassName(properties.getDriverClassName());
		return dataSource;
	}
}
           

加密工具類:

/**
 * 
 *AES對稱加密和解密  , 項目啟動類調用了此方法解密   
 * @author: LT
 * @date:   2019年2月18日 上午10:51:16
 */
public class SystemEncoder {
	/*
	 * 加密 1.構造密鑰生成器 2.根據ecnodeRules規則初始化密鑰生成器 3.産生密鑰 4.建立和初始化密碼器 5.内容加密 6.傳回字元串
	 */
	public static String AESEncode(String encodeRules, String content) {
		try {
			// 1.構造密鑰生成器,指定為AES算法,不區分大小寫
			KeyGenerator keygen = KeyGenerator.getInstance("AES");
			// 2.根據ecnodeRules規則初始化密鑰生成器
			// 生成一個128位的随機源,根據傳入的位元組數組
			keygen.init(128, new SecureRandom(encodeRules.getBytes()));
			// 3.産生原始對稱密鑰
			SecretKey original_key = keygen.generateKey();
			// 4.獲得原始對稱密鑰的位元組數組
			byte[] raw = original_key.getEncoded();
			// 5.根據位元組數組生成AES密鑰
			SecretKey key = new SecretKeySpec(raw, "AES");
			// 6.根據指定算法AES自成密碼器
			Cipher cipher = Cipher.getInstance("AES");
			// 7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二個參數為使用的KEY
			cipher.init(Cipher.ENCRYPT_MODE, key);
			// 8.擷取加密内容的位元組數組(這裡要設定為utf-8)不然内容中如果有中文和英文混合中文就會解密為亂碼
			byte[] byte_encode = content.getBytes("utf-8");
			// 9.根據密碼器的初始化方式--加密:将資料加密
			byte[] byte_AES = cipher.doFinal(byte_encode);
			// 10.将加密後的資料轉換為字元串
			// 這裡用Base64Encoder中會找不到包
			// 解決辦法:
			// 在項目的Build path中先移除JRE System Library,再添加庫JRE System
			// Library,重新編譯後就一切正常了。
			String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
			// 11.将字元串傳回
			return AES_encode;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		// 如果有錯就返加nulll
		return null;
	}

	/*
	 * 解密 解密過程: 1.同加密1-4步 2.将加密後的字元串反紡成byte[]數組 3.将加密内容解密
	 */
	public static String AESDncode(String encodeRules, String content) {
		try {
			// 1.構造密鑰生成器,指定為AES算法,不區分大小寫
			KeyGenerator keygen = KeyGenerator.getInstance("AES");
			// 2.根據ecnodeRules規則初始化密鑰生成器
			// 生成一個128位的随機源,根據傳入的位元組數組
			keygen.init(128, new SecureRandom(encodeRules.getBytes()));
			// 3.産生原始對稱密鑰
			SecretKey original_key = keygen.generateKey();
			// 4.獲得原始對稱密鑰的位元組數組
			byte[] raw = original_key.getEncoded();
			// 5.根據位元組數組生成AES密鑰
			SecretKey key = new SecretKeySpec(raw, "AES");
			// 6.根據指定算法AES自成密碼器
			Cipher cipher = Cipher.getInstance("AES");
			// 7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二個參數為使用的KEY
			cipher.init(Cipher.DECRYPT_MODE, key);
			// 8.将加密并編碼後的内容解碼成位元組數組
			byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
			/*
			 * 解密
			 */
			byte[] byte_decode = cipher.doFinal(byte_content);
			String AES_decode = new String(byte_decode, "utf-8");
			return AES_decode;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}

		// 如果有錯就返加nulll
		return null;
	}
           

繼續閱讀