In Java EE configuration files, such as XML or properties files, because some sensitive information does not want to be seen by ordinary people, it can be stored in an encrypted manner and decrypted after the program is read.
Common such as: Database user password, SMS platform user password, system calibration of fixed passwords.
This tool class refers to the "spring.3.x Enterprise application Development Combat" book 5.3 of the implementation.
The complete code and comment information are 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 = "encrypt@cncounter.com";
Constant public static final String utf_8 = "UTF-8";
public static final String DES = "des";
Static initialization static{try {//KEY builder Keygenerator generator = Keygenerator.getinstance (DES);
initialization, secure random operator Generator.init (new SecureRandom (Key_str.getbytes (utf_8));
Generate key = Generator.generatekey ();
Generator = null;
catch (Exception e) {throw new RuntimeException (e); }/** * Encrypts source string, returns BASE64 encoded encrypted String * @param source string, plaintext * @return ciphertext string/public static string encode (S
Tring Source {try {//Get byte array from encoded 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);
The catch (Exception e) {//Throw is also considered a return path throw new RuntimeException (e); /** * Decodes/decrypts strings encrypted by this tool class encode () method * @param encrypted encrypted string, ciphertext * @return plaintext string/public static St
Ring decode (String encrypted) {//Base64 decoder Base64decoder base64decoder = new Base64decoder ();
try {//First base64 decode 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);
Converts a byte array into a string in a given encoding format return new string (Decryptstrbytes, utf_8);
The catch (Exception e) {//This form is indeed appropriate for the processing tool class throw new RuntimeException (e); }//CellTest public static void main (string[] args) {//need to encrypt strings string email = "renfufei@qq.com";
Encrypted String encrypted = desutils.encode (email);
Decrypt String decrypted = Desutils.decode (encrypted);
Output results;
System.out.println ("email:" + email);
SYSTEM.OUT.PRINTLN ("Encrypted:" + encrypted);
System.out.println ("decrypted:" + decrypted);
System.out.println ("Email.equals (decrypted):" + email.equals (decrypted)); }
}