Share a very good string encryption and decryption program.
You can specify different keys to perform different encryption operations on the same string to enhance the encryption performance.
The Java code is as follows:
Package COM. APP; import Java. security. key; import Java. security. security; import javax. crypto. cipher;/*** DES encryption and decryption tool, which can encrypt and decrypt strings. */Public class test4 {/*** default constructor. Use the default key */Public test4 () throws exception {This (strdefaultkey );} /*** specify the Key Construction Method * @ Param strkey specifies the key * @ throws exception */Public test4 (string strkey) throws exception {// security. addprovider (new COM. sun. crypto. provider. sunjce (); Key key = getkey (strkey. getbytes (); encryptcipher = cipher. getinstance ("des"); encryptcipher. init (cipher. encrypt_mode, key); decryptcipher = cipher. getinstance ("des"); decryptcipher. init (cipher. decrypt_mode, key);}/** default string key value */Private Static string strdefaultkey = "national";/** encryption tool */private cipher encryptcipher = NULL; /** decryption tool */private cipher decryptcipher = NULL;/*** converts the byte array to a string that represents the hexadecimal value, for example: byte [] {8, 18} to: 0813, and public static byte [] * hexstr2bytearr (string strin) reversible conversion process * @ Param arrb the byte array to be converted * @ return the 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 ;} // if the number is less than 0f, add 0if (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 the string to be converted * @ return the converted byte array */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 ;} /*** encrypted byte array ** @ Param arrb the byte array to be encrypted * @ return the encrypted byte array */Public byte [] encrypt (byte [] arrb) throws exception {return encryptcipher. dofinal (arrb);}/*** encrypted string * @ Param strin the string to be encrypted * @ return encrypted string */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 */Public byte [] decrypt (byte [] arrb) throws exception {return decryptcipher. dofinal (arrb);}/*** decryption string * @ Param strin the string to be decrypted * @ return the decrypted string */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 */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;} public static void main (string [] ARGs) {try {string test1 = "987654321 "; test4 DES1 = new test4 (); // use the default key system. out. println ("character before encryption:" + test1); system. out. println ("encrypted character:" + des1.encrypt (test1); system. out. println ("decrypted character:" + des1.decrypt (des1.encrypt (test1); string Test2 = "123456789"; test4 des2 = new test4 ("leeme32nz "); // custom key system. out. println ("character before encryption:" + Test2); system. out. println ("encrypted character:" + des2.encrypt (Test2); system. out. println ("decrypted character:" + des2.decrypt (des2.encrypt (Test2);} catch (exception e) {e. printstacktrace ();}}}
Running result:
Character before encryption: 987654321 character after encryption: 5579cb6ffef7b473e6b92e28c43dccd4 decrypted character: 987654321 character before encryption: 123456789 encrypted character: decrypted character: 123456789
[Note] Reprinted from http://www.blogjava.net/vwpolo/archive/2009/12/05/304874.html