Common ones include: database user password, SMS platform user password, fixed password for inter-system verification, etc.
This tool class refers to the implementation of Section 5.3 of the book "Spring.3.x Enterprise Application Development Practice".
The complete code and annotation information are as follows:
Copy the code code 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 {
// key
private static Key key;
// KEY seed
private static String KEY_STR = "[email protected]";
// constant
public static final String UTF_8 = "UTF-8";
public static final String DES = "DES";
// static initialization
static{
try {
// KEY generator
KeyGenerator generator = KeyGenerator.getInstance(DES);
//Initialization, secure random operator
generator.init(new SecureRandom( KEY_STR.getBytes(UTF_8) ));
// Generate key
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Encrypt the source string and return the BASE64-encoded encrypted string.
* @param source source string, plain text
* @return ciphertext string
*/
public static String encode(String source){
try {
// Get the byte array according to the encoding format
byte[] sourceBytes = source.getBytes(UTF_8);
// DES encryption mode
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key);
// Encrypted byte array
byte[] encryptSourceBytes = cipher.doFinal(sourceBytes);
// Base64 encoder
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(encryptSourceBytes);
} catch (Exception e) {
// throw can also be regarded as a return path
throw new RuntimeException(e);
}
}
/**
* Decode/decrypt the string encrypted by the encode() method of this tool class
* @param encrypted The encrypted string, that is, the ciphertext
* @return plain text string
*/
public static String decode(String encrypted){
// Base64 decoder
BASE64Decoder base64Decoder = new BASE64Decoder();
try {
//Decode base64 first
byte[] cryptedBytes = base64Decoder.decodeBuffer(encrypted);
// DES decryption mode
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key);
//Decoded byte array
byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
// Convert the byte array into a string using the given encoding format
return new String(decryptStrBytes, UTF_8);
} catch (Exception e) {
// This form is indeed suitable for processing tool classes
throw new RuntimeException(e);
}
}
// unit test
public static void main(String[] args) {
//String that needs to be encrypted
String email = "[email protected]";
//encrypt
String encrypted = DESUtils.encode(email);
// Decrypt
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));
}
}