一個Java設定檔加密解密工具類分享_java

來源:互聯網
上載者:User

常見的如: 資料庫使用者密碼,簡訊平台使用者密碼,系統間校正的固定密碼等。
本工具類參考了 《Spring.3.x公司專屬應用程式開發實戰》一書 5.3節的實現。
完整代碼與注釋資訊如下:

複製代碼 代碼如下:

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";

 // 靜態初始化
 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 {
   // 根據編碼格式擷取位元組數組
   byte[] sourceBytes = source.getBytes(UTF_8);
   // DES 加密模式
   Cipher cipher = Cipher.getInstance(DES);
   cipher.init(Cipher.ENCRYPT_MODE, key);
   // 加密後的位元組數組
   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);
   // 解碼後的位元組數組
   byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
   // 採用給定編碼格式將位元組數組變成字串
   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);
  // 輸出結果;
  System.out.println("email: " + email);
  System.out.println("encrypted: " + encrypted);
  System.out.println("decrypted: " + decrypted);
  System.out.println("email.equals(decrypted): " + email.equals(decrypted));
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.