A Java configuration file encryption and decryption utility class share

  • 2020-04-01 03:16:57
  • OfStack

Common such as: database user password, SMS platform user password, the system between the calibration of the fixed password.
This tool class refers to the implementation of section 5.3 of Spring.3. X enterprise application development practice.
The complete code and comment information is as follows:


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 {
 //The key
 private static Key key;
 //The KEY of seeds
 private static String KEY_STR = "encrypt@cncounter.com";
 //constant
 public static final String UTF_8 = "UTF-8";
 public static final String DES = "DES";

 //Static initialization
 static{
  try {
   //The KEY generator
   KeyGenerator generator = KeyGenerator.getInstance(DES);
   //Initialization, secure random operator
   generator.init(new SecureRandom( KEY_STR.getBytes(UTF_8) ));
   //Generate the key
   key = generator.generateKey();
   generator = null;
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 
 public static String encode(String source){
  try {
   //Gets the byte array based on the encoding format
   byte[] sourceBytes = source.getBytes(UTF_8);
   //DES encryption mode
   Cipher cipher = Cipher.getInstance(DES);
   cipher.init(Cipher.ENCRYPT_MODE, key);
   //The encrypted byte array
   byte[] encryptSourceBytes = cipher.doFinal(sourceBytes);
   //Base64 encoder
   BASE64Encoder base64Encoder = new BASE64Encoder();
   return base64Encoder.encode(encryptSourceBytes);
  } catch (Exception e) {
   //Throw is also a return path
   throw new RuntimeException(e);
  }
 }

 /**
  *  This utility class  encode()  Method to decode the encrypted string / decryption 
  * @param encrypted  An encrypted string , The cipher 
  * @return  Plaintext string 
  */
 public static String decode(String encrypted){
  //Base64 decoder
  BASE64Decoder base64Decoder = new BASE64Decoder();
  try {
   //Start with the base64 decoding
   byte[] cryptedBytes = base64Decoder.decodeBuffer(encrypted);
   //DES decryption mode
   Cipher cipher = Cipher.getInstance(DES);
   cipher.init(Cipher.DECRYPT_MODE, key);
   //The decoded byte array
   byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
   //Converts a byte array into a string in the given encoding format
   return new String(decryptStrBytes, UTF_8);
  } catch (Exception e) {
   //This form is really good for working with utility classes
   throw new RuntimeException(e);
  }
 }
 //Unit testing
 public static void main(String[] args) {
  //A string that needs to be encrypted
  String email = "renfufei@qq.com";
  //encryption
  String encrypted = DESUtils.encode(email);
  //decryption
  String decrypted = DESUtils.decode(encrypted);
  //Output result;
  System.out.println("email: " + email);
  System.out.println("encrypted: " + encrypted);
  System.out.println("decrypted: " + decrypted);
  System.out.println("email.equals(decrypted): " + email.equals(decrypted));
 }
}


Related articles: