Overview:
For security reasons, data transmission is often encrypted and encoded during network transmission, which involves the following types:
1. MD5 encryption. the encryption algorithm is one-way encryption, that is, encrypted data cannot be restored through decryption. The related classes are included in the Java. Security. messagedigest package.
2. 3-des encryption. the encryption algorithm is reversible. The decryption party can decrypt the data by using the secret key agreed with the encryption party. The related classes are included in the javax. crypto. * package.
3. base64 encoding is the most common encoding method used to transmit 8-bit bytes of code. The related classes are in Sun. Misc. base64decoder and sun. Misc. base64encoder.
4. urlencoder encoding is a character encoding that ensures that transmitted parameters are composed of compliant texts. The related classes are in the java.net. urlencoder package.
Details:
1. Perform MD5 encryption, byte [] <br/>/** <br/> * MD5 encryption <br/> * @ Param string original spkey <br/> * @ return byte [] specify the MD5 byte [] <br/> */</P> <p> private byte [] MD5 (string strsrc) <br/>{< br/> byte [] returnbyte = NULL; <br/> try <br/>{< br/> messagedigest MD5 = messagedigest. getinstance ("MD5"); <br/> returnbyte = md5.digest (strsrc. getbytes ("GBK"); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/ >}< Br/> return returnbyte; <br/>}</P> <p> 2. Obtain the password of 3-des. <br/>/** <br/> * obtain the password of 3-des. <br/> * As needed, for example, if the key is 24 bytes and MD5 is encrypted to 16 bytes, therefore, add 8 bytes of 0 <br/> * @ Param string original spkey <br/> * @ return byte [] specifies the MD5 byte [] After encryption. <br/> */</P> <p> private byte [] getenkey (string spkey) <br/> {<br/> byte [] shard ey = NULL; <br/> try <br/> {<br/> byte [] ey1 = MD5 (spkey ); <br/> cipher ey = new byte [24]; <br/> int I = 0; <br/> wh Ile (I <deskey1.length & I <24) {<br/> your ey [I] = deskey1 [I]; <br/> I ++; <br/>}< br/> if (I <24) {<br/> same ey [I] = 0; <br/> I ++; <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return another ey; <br/>}< br/> 3. 3-DES encryption <br/>/** <br/> * 3-des encryption <br/> * @ Param byte [] SRC 3-des-encrypted byte [] <br/> * @ Param byte [] enkey 3-des encryption key <br/> * @ return byte [] 3-des-encrypted byte [] <br /> */</P> <p> Public byte [] encrypt (byte [] SRC, byte [] enkey) <br/>{< br/> byte [] encrypteddata = NULL; <br/> try <br/>{< br/> desedekeyspec DKS = new desedekeyspec (enkey ); <br/> secretkeyfactory keyfactory = secretkeyfactory. getinstance ("desede"); <br/> secretkey key = keyfactory. generatesecret (DKS); <br/> cipher = cipher. getinstance ("desede"); <br/> cipher. init (cipher. encrypt_mode, key); <Br/> encrypteddata = cipher. dofinal (SRC); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> return encrypteddata; <br/>}< br/> 4. encode the string using base64 <br/>/** <br/> * encode the string using base64 <br/> * @ Param byte [] characters to be encoded by Src <br/> * @ return string the encoded string <br/> */</P> <p> public String getbase64encode (byte [] SRC) <br/>{< br/> string requestvalue = ""; <br/> try {<br/> Ba Se64encoder base64en = new base64encoder (); <br/> requestvalue = base64en. encode (SRC); <br/> // system. out. println (requestvalue); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p> 5. You can remove the newline character of the string as needed. <br/>/** <br/> * remove the newline character of the string. <br/> * When base64 encoded 3-des data, the resulting string contains line breaks. You can remove <br/> */</P> <p> private string filter (string Str) as needed) <br/>{< br/> stri Ng output = NULL; <br/> stringbuffer sb = new stringbuffer (); <br/> for (INT I = 0; I <Str. length (); I ++) <br/>{< br/> int ASC = Str. charat (I); <br/> If (ASC! = 10 & ASC! = 13) <br/> Sb. append (Str. subsequence (I, I + 1); <br/>}< br/> output = new string (SB); <br/> return output; <br/>}</P> <p> 6. urldecoder the string. encode (strencoding) encoding <br/>/** <br/> * urldecoder the string. encode (strencoding) encoding <br/> * @ Param string SRC: the string to be encoded <br/> * @ return string: The encoded string <br/> */< /P> <p> Public String geturlencode (string SRC) <br/>{< br/> string requestvalue = ""; <br/> try {</P> <p> requestvalue = urlencoder. encode (SRC); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p> 7. urldecoder the string. decode (strencoding) decoding <br/>/** <br/> * urldecoder the string. decode (strencoding) decoded <br/> * @ Param string SRC the string to be decoded <br/> * @ return string the decoded string <br/> */< /P> <p> Public String geturldecoderdecode (string SRC) <br/> {<Br/> string requestvalue = ""; <br/> try {</P> <p> requestvalue = urldecoder. decode (SRC); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p> 8. Perform 3-des decryption (the key is equivalent to the Encrypted Key) <br/>/** <br/> * perform 3-des decryption (the key is equivalent to the Encrypted Key ). <Br/> * @ Param byte [] SRC 3-des decryption byte [] <br/> * @ Param string spkey allocated spkey <br/> * @ return string 3-des decryption <br/> */<br/> Public String decrypt (byte [] debase64, string spkey) <br/>{< br/> string strde = NULL; <br/> cipher = NULL; <br/> try <br/> {<br/> cipher = cipher. getinstance ("desede"); <br/> byte [] Key = getenkey (spkey); <br/> desedekeyspec DKS = new desedekeyspec (key); <br/> Secretkeyfactory keyfactory = secretkeyfactory. getinstance ("desede"); <br/> secretkey skey = keyfactory. generatesecret (DKS); <br/> cipher. init (cipher. decrypt_mode, skey); <br/> byte ciphertext [] = cipher. dofinal (debase64); <br/> strde = new string (ciphertext, "UTF-16LE"); <br/>}< br/> catch (exception ex) <br/>{< br/> strde = ""; <br/> ex. printstacktrace (); <br/>}< br/> return strde; </P> <p> after the above steps You can complete MD5 encryption. 3-DES encryption, base64 encoding transmission, base64 decoding, and 3-des decryption can obtain the original text. <Br/> the full text of the program is as follows: <br/> package COM. neusoft. test. util. crypt; </P> <p> Import Java. io. ioexception; <br/> Import Java. io. unsupportedencodingexception; <br/> Import java.net. urldecoder; <br/> Import java.net. urlencoder; <br/> Import Java. security. messagedigest; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. calendar; </P> <p> Import javax. crypto. cipher; <br/> Import javax. crypto. secretkey; <br/> Import Javax. crypto. secretkeyfactory; <br/> Import javax. crypto. spec. desedekeyspec; </P> <p> import sun. misc. base64decoder; <br/> import sun. misc. base64encoder; </P> <p>/** <br/> * <p> title: test encryption and decryption </P> <br/> * <p> description: encryption and decryption </P> <br/> * <p> date: 2005-08-11 </P> <br/> * <p> copyright: Copyright (c) 2005 Neusoft </P> <br/> * <p> company: neusoft </P> <br/> * @ author mengk <B R/> * @ Version 1.00 <br/> * <p> ---------------------------------------------------------- </P> <br/> * <p> modification history </P> <br/> * <p> reason for modifying the sequence number date modifier </P> <br/> * <p> 1 </P> <br/> */</P> <p> public class endecrypt {</P> <p>/** <br/> * MD5 encryption <br/> * @ Param string original spkey <br/> * @ return byte [] specifies the byte [] <br/> */</P> <p> private byte [] MD5 (string strsrc) after the encryption method is MD5) <br/> {<br/> byte [] returnbyte = NUL L; <br/> try <br/> {<br/> messagedigest MD5 = messagedigest. getinstance ("MD5"); <br/> returnbyte = md5.digest (strsrc. getbytes ("GBK"); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> return returnbyte; <br/>}</P> <p>/** <br/> * obtain the password of 3-des. <br/> * According to the interface specification, the password key is 24 bytes, and MD5 is encrypted to 16 bytes, therefore, add 8 bytes of 0 <br/> * @ Param string original spkey <br/> * @ return byte [] specifies the MD5 byte [] After encryption. <br/> */</ P> <p> private byte [] getenkey (string spkey) <br/>{< br/> byte [] encryption ey = NULL; <br/> try <br/> {<br/> byte [] deskey1 = MD5 (spkey); <br/> cipher ey = new byte [24]; <br/> int I = 0; <br/> while (I <deskey1.length & I <24) {<br/> specified ey [I] = deskey1 [I]; <br/> I ++; <br/>}< br/> if (I <24) {<br/> Other ey [I] = 0; <br/> I ++; <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return Administrative ey; <br/>}</P> <p>/** <br/> * 3-des encryption <br/> * @ Param byte [] the byte for which SRC implements 3-des encryption [] <br/> * @ Param byte [] enkey 3-des encryption key <br/> * @ return byte [] 3-des encrypted byte [] <br/> */</ p> <p> Public byte [] encrypt (byte [] SRC, byte [] enkey) <br/>{< br/> byte [] encrypteddata = NULL; <br/> try <br/> {<br/> desedekeyspec DKS = new desedekeyspec (enkey); <br/> secretkeyfactory keyfactory = secretkeyfactory. getinsta NCE ("desede"); <br/> secretkey key = keyfactory. generatesecret (DKS); <br/> cipher = cipher. getinstance ("desede"); <br/> cipher. init (cipher. encrypt_mode, key); <br/> encrypteddata = cipher. dofinal (SRC); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> return encrypteddata; <br/>}</P> <p>/** <br/> * encode the string in base64 format. <br/> * @ Param byte [] character <br/> * <br /> * @ Return string the encoded string <br/> */</P> <p> Public String getbase64encode (byte [] SRC) <br/>{< br/> string requestvalue = ""; <br/> try {<br/> base64encoder base64en = new base64encoder (); <br/> requestvalue = base64en. encode (SRC); <br/> // system. out. println (requestvalue); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p>/** <br/> * remove characters String line break symbol <br/> * When base64 encoding 3-des data, the resulting string contains a line break symbol <br/> *, must be removed, otherwise, the uni-wise platform fails to parse the ticket root. <br/> * "SP Verification Failed" is displayed ". During the development process, I was helpless because of this problem. <br/> * a friend told me that I could ask China Unicom to ask for encrypted text, then compare it with the self-generated string. <br/> * This is a good debugging method. I finally found that the only difference between the string I generated is that there is a line break. <Br/> * I used the C # language to write the ticket root request program. I did not find this problem. <Br/> */</P> <p> private string filter (string Str) <br/>{< br/> string output = NULL; <br/> stringbuffer sb = new stringbuffer (); <br/> for (INT I = 0; I <Str. length (); I ++) <br/>{< br/> int ASC = Str. charat (I); <br/> If (ASC! = 10 & ASC! = 13) <br/> Sb. append (Str. subsequence (I, I + 1); <br/>}< br/> output = new string (SB); <br/> return output; <br/>}</P> <p>/** <br/> * urldecoder the string. encode (strencoding) encoding <br/> * @ Param string SRC: the string to be encoded <br/> * @ return string: The encoded string <br/> */< /P> <p> Public String geturlencode (string SRC) <br/>{< br/> string requestvalue = ""; <br/> try {</P> <p> requestvalue = urlencoder. encode (SRC); <Br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p>/** <br/> * 3-des encryption <br/> * @ Param string SRC: string to be encrypted by 3-des <br/> * @ Param string spkey allocation spkey <br/> * @ return string 3-des encrypted string <br/> */</P> <p> Public String get3desencrypt (string SRC, string spkey) <br/>{< br/> string requestvalue = ""; <br/> try {</P> <p> // obtain the password of 3-des. <br/> B Yte [] enkey = getenkey (spkey ); <br/> // the content to be encrypted with 3-des is in/"UTF-16LE/" byte <br/> byte [] src2 = SRC. getbytes ("UTF-16LE"); <br/> // bytes of content encrypted with 3-des <br/> byte [] encrypteddata = encrypt (src2, enkey ); </P> <p> // base64 encoded content after 3-des encryption <br/> string base64string = getbase64encode (encrypteddata ); <br/> // After linefeed is removed by base64 encoding <br/> string base64encrypt = filter (base64string ); </P> <p> // escape the HTML control code in base64 encoding <br/> Requestvalue = geturlencode (base64encrypt); <br/> // system. out. println (requestvalue); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p>/** <br/> * urldecoder the string. decode (strencoding) decoded <br/> * @ Param string SRC the string to be decoded <br/> * @ return string the decoded string <br/> */< /P> <p> Public String geturldecoderdecode (string SRC) <br/> {<br/> String requestvalue = ""; <br/> try {</P> <p> requestvalue = urldecoder. decode (SRC); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return requestvalue; <br/>}</P> <p>/** <br/> * perform 3-des decryption (the key is equivalent to the Encrypted Key ). <Br/> * @ Param byte [] SRC 3-des decryption byte [] <br/> * @ Param string spkey allocated spkey <br/> * @ return string 3-des decryption <br/> */<br/> Public String decrypt (byte [] debase64, string spkey) <br/>{< br/> string strde = NULL; <br/> cipher = NULL; <br/> try <br/> {<br/> cipher = cipher. getinstance ("desede"); <br/> byte [] Key = getenkey (spkey); <br/> desedekeyspec DKS = new desedekeyspec (key); <br/> Secretkeyfactory keyfactory = secretkeyfactory. getinstance ("desede"); <br/> secretkey skey = keyfactory. generatesecret (DKS); <br/> cipher. init (cipher. decrypt_mode, skey); <br/> byte ciphertext [] = cipher. dofinal (debase64); <br/> strde = new string (ciphertext, "UTF-16LE"); <br/>}< br/> catch (exception ex) <br/>{< br/> strde = ""; <br/> ex. printstacktrace (); <br/>}< br/> return strde; <br/>}</P> <P>/** <br/> * 3-des decryption <br/> * @ Param string SRC: string to be decrypted by 3-des <br/> * @ Param string spkey allocated by spkey <br/> * @ return string 3-des encrypted string <br/> */</P> <p> Public String get3desdecrypt (string SRC, string spkey) <br/>{< br/> string requestvalue = ""; <br/> try {</P> <p> // obtain the password of 3-des </P> <p> // urldecoder. decodetml control code escape Process <br/> string urlvalue = geturldecoderdecode (SRC); </P> <p> // base the content encrypted with 3-des 64 encoding </P> <p> base64decoder base64decode = new base64decoder (); <br/> byte [] base64dvalue = base64decode. decodebuffer (urlvalue ); </P> <p> // the content to be 3-DES encrypted in/"UTF-16LE/" Get byte </P> <p> requestvalue = decrypt (base64dvalue, spkey ); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return requestvalue; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> endecrypt test = new endec Rypt (); <br/> string oldstring = "toxin"; </P> <p> string spkey = "1234"; <br/> system. out. println ("1. The allocated spkey is: "+ spkey); <br/> system. Out. println (" 2. Content: "+ oldstring); <br/> string revalue = test. get3desencrypt (oldstring, spkey); <br/> revalue = revalue. trim (). intern (); <br/> system. out. println ("content encrypted with 3-des:" + revalue); <br/> string revalue2 = test. get3desdecrypt (revalue, spkey); <br/> system. out. println ("content decrypted by 3-des:" + revalue2); <br/>}< br/>}