Import Java. security. key; import Java. security. security; import javax. crypto. cipher;/***** des Reversible Encryption Algorithm version 1.0 */public class desencrypt {Private Static string strdefaultkey = "wuguowei"; private cipher encryptcipher = NULL; private cipher decryptcipher = NULL; public static void main (string [] ARGs) throws exception {desencrypt currtest = new desencrypt (); string spassword = "trsadmin"; string sencrypt = currtest. encrypt (spassword); system. out. println ("encryption:" + sencrypt); string sdeencrypt = currtest. decrypt (sencrypt); system. out. println ("decrypted:" + sdeencrypt);}/*** convert the byte array to a string that represents the hexadecimal value, for example, byte [] {8, 18}: 0813, and public static byte [] * hexstr2bytearr (string strin) reversible conversion process ** @ Param arrb * byte array to be converted * @ return converted string * @ throws exception * this method does not handle any exceptions, all exceptions are thrown */public static string bytearr2hexstr (byte [] arrb) throws exception {int ilen = arrb. length; // each byte can be expressed with two characters. Therefore, the string length is twice the length of the array. stringbuffer sb = new stringbuffer (ilen * 2); For (INT I = 0; I <ilen; I ++) {int inttmp = arrb [I]; // convert a negative number to a positive number while (inttmp <0) {inttmp = inttmp + 256 ;} // The number smaller than 0f must be filled with 0 if (inttmp <16) {sb. append ("0");} sb. append (integer. tostring (inttmp, 16);} return sb. tostring ();}/*** converts a hexadecimal string into a byte array and a public static string bytearr2hexstr (byte [] arrb) * reversible conversion process ** @ Param strin * string to be converted * @ return the converted byte array * @ throws exception * this method does not handle any exceptions, all exceptions throw * @ author <a href = "mailto: leo841001@163.com"> liguoqing </a> */public static byte [] hexstr2bytearr (string strin) throws exception {byte [] arrb = strin. getbytes (); int ilen = arrb. length; // two characters indicate one byte. Therefore, the length of the byte array is the string length divided by 2 byte [] arrout = new byte [ilen/2]; for (INT I = 0; I <ilen; I = I + 2) {string strtmp = new string (arrb, I, 2); arrout [I/2] = (byte) integer. parseint (strtmp, 16);} return arrout;}/*** default constructor. Use the default key ** @ throws exception */Public desencrypt () throws exception {This (strdefakey key);}/*** specify the key construction method ** @ Param strkey * specify the key * @ throws exception */Public desencrypt (string strkey) throws exception {try {// Add the current algorithm as the system worker to find your algorithm security. addprovider (new COM. sun. crypto. provider. sunjce ();} catch (exception e) {// ignore} key = getkey (strkey. getbytes (); encryptcipher = cipher. getinstance ("des"); encryptcipher. init (cipher. encrypt_mode, key); decryptcipher = cipher. getinstance ("des"); decryptcipher. init (cipher. decrypt_mode, key );} /***** encrypted byte array ** @ Param arrb * byte array to be encrypted * @ return encrypted byte array * @ throws exception */Public byte [] encrypt (byte [] arrb) throws exception {return encryptcipher. dofinal (arrb );} /***** encrypted string ** @ Param strin * string to be encrypted * @ return encrypted string * @ throws exception */Public String encrypt (string strin) throws exception {return bytearr2hexstr (encrypt (strin. getbytes ()));} /*** decrypt the byte array ** @ Param arrb * the byte array to be decrypted * @ return the decrypted byte array * @ throws exception */Public byte [] decrypt (byte [] arrb) throws exception {return decryptcipher. dofinal (arrb );} /*** decrypt the string ** @ Param strin * the string to be decrypted * @ return the decrypted string * @ throws exception */Public String decrypt (string strin) throws exception {return new string (decrypt (hexstr2bytearr (strin);}/*** generate a key from a specified string, if the length of the byte array required by the key is 8 or less than 8 bits, add 0 to the end, if the value exceeds 8 bits, only the first 8 bits ** @ Param arrbtmp * are used to form the byte array of the string * @ return generated key * @ throws Java. lang. exception */private key getkey (byte [] arrbtmp) throws exception {// create an empty 8-byte array (default value: 0) byte [] arrb = new byte [8]; // convert the original byte array to an 8-bit for (INT I = 0; I <arrbtmp. length & I <arrb. length; I ++) {arrb [I] = arrbtmp [I] ;}// generate the key = new javax. crypto. spec. secretkeyspec (arrb, "des"); Return key ;}}