Package httpvisit. action; import javax. crypto. cipher; import javax. crypto. secretkey; import javax. crypto. secretkeyfactory; import javax. crypto. spec. deskeyspec; import javax. crypto. spec. ivparameterspec; import sun. misc. base64decoder; import sun. misc. base64encoder;/*** @ Author: heweipo * @ Version 1.00 ***/public class descoderutil {Private Static byte [] keys = {1,-1, 1,-1, 1,-1, 1,-1}; Private Static s Tring key = "leagsoft"; public static string getkey () {return key;} public static void setkey (string key) {descoderutil. key = key;}/***** <p> * perform MD5 encryption on the password * @ Param source * @ return byte [] * Author: heweipo */public static byte [] getmd5 (byte [] source) {byte TMP [] = NULL; try {Java. security. messagedigest MD = Java. security. messagedigest. getinstance ("MD5"); Md. update (source); TMP = md. dig Est ();} catch (exception e) {e. printstacktrace ();} return TMP ;} /***** <p> * use JDK built-in classes for real encryption operations * @ Param bytes * @ Param password * @ return byte [] * Author: heweipo */Private Static byte [] encryptbyte (byte [] bytes, byte password []) {byte [] bytefina = NULL; try {// initialize the encryption/Decryption tool cipher = cipher. getinstance ("des/CBC/pkcs5padding"); inclueyspec = new inclueyspec (password); secretkeyfa Ctory keyfactory = secretkeyfactory. getinstance ("des"); secretkey = keyfactory. generatesecret (deskeyspec); ivparameterspec IV = new ivparameterspec (KEYS); cipher. init (cipher. encrypt_mode, secretkey, iv); bytefina = cipher. dofinal (bytes);} catch (exception e) {Throw new runtimeexception (E);} return bytefina ;} /***** <p> * use the built-in JDK class for decryption * @ Param bytes * @ Param password * @ return B Yte [] * Author: heweipo */Private Static byte [] decryptbyte (byte [] bytes, byte password []) {byte [] bytefina = NULL; try {// initialize the encryption/Decryption tool cipher = cipher. getinstance ("des/CBC/pkcs5padding"); inclueyspec = new inclueyspec (password); secretkeyfactory keyfactory = secretkeyfactory. getinstance ("des"); secretkey = keyfactory. generatesecret (deskeyspec); ivparameterspec IV = new ivparame Terspec (KEYS); cipher. init (cipher. decrypt_mode, secretkey, iv); bytefina = cipher. dofinal (bytes);} catch (exception e) {Throw new runtimeexception (E);} return bytefina;}/***** <p> * DES encrypted strming, then base64 conversion * @ Param strming * @ Param md5key * @ return string * Author: heweipo */public static string encryptstr (string strming, byte md5key []) {byte [] bytemi = NULL; byte [] byteming = NULL; string s Trmi = ""; try {byteming = strming. getbytes ("UTF-8"); bytemi = encryptbyte (byteming, md5key); base64encoder base64encoder = new base64encoder (); strmi = base64encoder. encode (bytemi);} catch (exception e) {Throw new runtimeexception (E);} finally {byteming = NULL; bytemi = NULL;} return strmi ;} /***** <p> * convert strmi using base64, And Then decrypt the DES. * @ Param strmi * @ Param md5key * @ return string * Author: heweipo */Public static string decryptstr (string strmi, byte md5key []) {byte [] byteming = NULL; string strming = ""; try {base64decoder decoder = new base64decoder (); byteming = decoder. decodebuffer (strmi); byteming = decryptbyte (byteming, md5key); strming = new string (byteming);} catch (exception e) {Throw new runtimeexception (E );} finally {byteming = NULL;} return strming;} public static void main (string [] ar GS) {string [] keys = {"leagsoft", "leagsoftpo", "leagsoftiu", "leagsoftyy", "leagsoftew", "leagsoftmm"}; For (string key: keys) {string data = "I'm learn_more, who are you? "; Try {system. out. println (descoderutil. encryptstr (data, descoderutil. getmd5 (key. getbytes ("UTF-8"); system. out. println ("--------------"); system. out. println (descoderutil. decryptstr (descoderutil. encryptstr (data, descoderutil. getmd5 (key. getbytes ("UTF-8"), descoderutil. getmd5 (key. getbytes ("UTF-8");} catch (exception e) {e. printstacktrace ();}}}}
Des encryption + base64 Conversion