一個Java配置文件加密解密工具類分享
常見的如: 數(shù)據(jù)庫用戶密碼,短信平臺用戶密碼,系統(tǒng)間校驗的固定密碼等。
本工具類參考了 《Spring.3.x企業(yè)應(yīng)用開發(fā)實戰(zhàn)》一書 5.3節(jié)的實現(xiàn)。
完整代碼與注釋信息如下:
package com.cncounter.util.comm;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DESUtils {
// 密鑰
private static Key key;
// KEY種子
private static String KEY_STR = "encrypt@cncounter.com";
// 常量
public static final String UTF_8 = "UTF-8";
public static final String DES = "DES";
// 靜態(tài)初始化
static{
try {
// KEY 生成器
KeyGenerator generator = KeyGenerator.getInstance(DES);
// 初始化,安全隨機算子
generator.init(new SecureRandom( KEY_STR.getBytes(UTF_8) ));
// 生成密鑰
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 對源字符串加密,返回 BASE64編碼后的加密字符串
* @param source 源字符串,明文
* @return 密文字符串
*/
public static String encode(String source){
try {
// 根據(jù)編碼格式獲取字節(jié)數(shù)組
byte[] sourceBytes = source.getBytes(UTF_8);
// DES 加密模式
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key);
// 加密后的字節(jié)數(shù)組
byte[] encryptSourceBytes = cipher.doFinal(sourceBytes);
// Base64編碼器
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(encryptSourceBytes);
} catch (Exception e) {
// throw 也算是一種 return 路徑
throw new RuntimeException(e);
}
}
/**
* 對本工具類 encode() 方法加密后的字符串進行解碼/解密
* @param encrypted 被加密過的字符串,即密文
* @return 明文字符串
*/
public static String decode(String encrypted){
// Base64解碼器
BASE64Decoder base64Decoder = new BASE64Decoder();
try {
// 先進行base64解碼
byte[] cryptedBytes = base64Decoder.decodeBuffer(encrypted);
// DES 解密模式
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key);
// 解碼后的字節(jié)數(shù)組
byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
// 采用給定編碼格式將字節(jié)數(shù)組變成字符串
return new String(decryptStrBytes, UTF_8);
} catch (Exception e) {
// 這種形式確實適合處理工具類
throw new RuntimeException(e);
}
}
// 單元測試
public static void main(String[] args) {
// 需要加密的字符串
String email = "renfufei@qq.com";
// 加密
String encrypted = DESUtils.encode(email);
// 解密
String decrypted = DESUtils.decode(encrypted);
// 輸出結(jié)果;
System.out.println("email: " + email);
System.out.println("encrypted: " + encrypted);
System.out.println("decrypted: " + decrypted);
System.out.println("email.equals(decrypted): " + email.equals(decrypted));
}
}
相關(guān)文章
SpringBoot整合SSO(single sign on)單點登錄
這篇文章主要介紹了SpringBoot整合SSO(single sign on)單點登錄,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
深入解析Java的Servlet過濾器的原理及其應(yīng)用
這篇文章主要介紹了深入解析Java的Servlet過濾器的原理及應(yīng)用,Java編寫的Servlet通常是一個與網(wǎng)頁一起作用于瀏覽器客戶端的程序,需要的朋友可以參考下2016-01-01
jackson 如何將實體轉(zhuǎn)json json字符串轉(zhuǎn)實體
這篇文章主要介紹了jackson 實現(xiàn)將實體轉(zhuǎn)json json字符串轉(zhuǎn)實體,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
關(guān)于Java Guava ImmutableMap不可變集合源碼分析
這篇文章主要介紹Java Guava不可變集合ImmutableMap的源碼分析的相關(guān)資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09
如何利用Retrofit+RxJava實現(xiàn)網(wǎng)絡(luò)請求的異常處理
這篇文章主要介紹了如何利用Retrofit+RxJava實現(xiàn)網(wǎng)絡(luò)請求的異常處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
SpringBoot結(jié)合WebSocket實現(xiàn)聊天功能
本文介紹了如何使用SpringBoot和WebSocket實現(xiàn)一個簡單的聊天功能,包括導(dǎo)入依賴、配置類、創(chuàng)建消息實體、指定ServerEndpoint、創(chuàng)建客戶端等步驟,通過具體示例,演示了如何發(fā)送個人消息和群發(fā)消息,實現(xiàn)了基本的聊天功能,適合需要在項目中實現(xiàn)實時通訊功能的開發(fā)者參考2024-11-11

