A tool class that is commonly used. It can be used directly without in-depth research.
Desutil. Java
Package lsy; import Java. security. key; import Java. security. securerandom; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. secretkey; import sun. misc. base64decoder; import sun. misc. base64encoder; public class desutil {/*** @ Param ARGs */public static void main (string [] ARGs) {// The following is the test system of the encryption method algorithm = "AES. out. println (desutil. getinstance ("lushuaiyin "). getencodestring (" Hello "); // output ldewgazkmwheyfjbz56ylw ==// decrypt the above ciphertext: system. out. println (desutil. getinstance ("lushuaiyin "). getdecodestring ("ldewgazkmwheyfjbz56ylw ="); // output Hello // change the key test system. out. println (desutil. getinstance ("suolong "). getencodestring ("hello"); // output/rlowoj + fr3kdmcdjenatg = system. out. println (desutil. getinstance ("suolong "). getdecodestring ("/rlowoj + fr3kdmcdjenatg ="); // output Hello // If an incorrect key is used for decryption, it will: system. Out. println (desutil. getinstance ("suolong "). getdecodestring ("ldewgazkmwheyfjbz56ylw =");} private secretkey key = NULL; // key // defines the encryption algorithm, which can be des, desede, blowfish, AES // different encryption methods may result in different Private Static string algorithm = "AES"; Private Static desutil = NULL; Public desutil () {} public static desutil getinstance (string strkey) {desutil = new desutil (); desutil. createkey (strkey); Return desutil;}/*** algorithm * @ P Aram strkey */Public void createkey (string strkey) {try {keygenerator kg = keygenerator. getinstance (desutil. algorithm); byte [] bt = strkey. getbytes ("UTF-8"); securerandom sr = new securerandom (BT); kg. init (SR); this. setkey (kg. generatekey ();} catch (exception e) {}}/*** encryption method, returns the ciphertext * cipher password * @ Param datastr */Public String getencodestring (string datastr) {byte [] miwen = NULL; // ciphertext byte [] mingwen = NULL; // plaintext cipher CIP Her; string result = ""; // ciphertext string try {mingwen = datastr. getbytes ("UTF-8"); cipher = cipher. getinstance (desutil. algorithm); cipher. init (cipher. encrypt_mode, this. getkey (); miwen = cipher. dofinal (mingwen); base64encoder base64en = new base64encoder (); Result = base64en. encodebuffer (miwen); // or you can use the following method to obtain the ciphertext. The results are different and can be decrypted normally. // result = byte2hex (miwen ); // The ciphertext result is similar to 2C: 37: B0: 18: 06: 64: 99: 61: de: 60: 58: C1: Cf: 9e: B2: 97} catch (exception E) {e. printstacktrace ();} return result;}/*** decryption method, returns the plaintext * @ Param codestr * @ return */Public String getdecodestring (string codestr) {base64decoder base64de = new base64decoder (); byte [] miwen = NULL; byte [] mingwen = NULL; string resultdata = ""; // The returned plaintext cipher; try {miwen = base64de. decodebuffer (codestr); cipher = cipher. getinstance (desutil. algorithm); cipher. init (cipher. decrypt_mode, this. getkey (); mingwen = CIP Her. dofinal (miwen); resultdata = new string (mingwen, "UTF-8");} catch (exception e) {return "The key is incorrect or causes an exception for other reasons and cannot be decrypted! ";}Return resultdata;} // convert the string to Public String byte2hex (byte [] B) {string HS =" "; string stmp = ""; for (INT n = 0; n <B. length; n ++) {stmp = (Java. lang. integer. tohexstring (B [N] & 0xff); If (stmp. length () = 1) HS = HS + "0" + stmp; elsehs = HS + stmp; If (n <B. length-1) HS = HS + ":";} return HS. touppercase ();} public secretkey getkey () {return key;} public void setkey (secretkey key) {This. key = key;} public static string getalgorithm () {return algorithm;} public static void setalgorithm (string algorithm) {algorithm = algorithm ;}}
Print:
Ldewgazkmwheyfjbz56ylw =
Hello
/Rlowoj + fr3kdmcdjenatg =
Hello
The key is incorrect or cannot be decrypted due to exceptions due to other reasons!