Brief introduction
Recently developed a project, using the 3DES encryption algorithm, encrypt the socket server and client communication messages, because the encryption key is 32 bytes, the result is a toss, and now recorded to share!
1, Des3encryptutils.java
PackageDes3;ImportJavax.crypto.Cipher;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESedeKeySpec;/*** 3DES Encryption Tool class *@authorQiaozhenwu*/ Public classDes3encryptutils {/**secret key*/ PrivateSecretkey SecureKey; /*** Function: The key in the algorithm needs to be encrypted by a byte array of keys *@paramthe byte array of the key secret key*/ Public voidSetkey (byte[] key) { Try{desedekeyspec DKs=NewDesedekeyspec (key); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("Desede"); SecureKey=Keyfactory.generatesecret (DKS); } Catch(Exception e) {System.out.println (E.getmessage ()); } } /*** Function: Encrypt the plaintext *@paramByteS byte array corresponding to plaintext *@returnbyte array corresponding to ciphertext*/ Public byte[] Get3desenccode (byte[] ByteS) { byte[] Bytefina =NULL; Cipher Cipher; Try{cipher= Cipher.getinstance ("desede/ecb/nopadding");//algorithm/grouping mode/fill modeCipher.init (Cipher.encrypt_mode, SecureKey); Bytefina=cipher.dofinal (ByteS); } Catch(Exception e) {System.out.println (E.getmessage ()); } finally{cipher=NULL; } returnBytefina; } /*** Function: Decryption of ciphertext *@parambyted byte array corresponding to ciphertext *@returnbyte array corresponding to clear text*/ Public byte[] Get3desdescode (byte[] byted) {Cipher Cipher; byte[] Bytefina =NULL; Try{cipher= Cipher.getinstance ("desede/ecb/nopadding"); Cipher.init (Cipher.decrypt_mode, SecureKey); Bytefina=cipher.dofinal (byted); } Catch(Exception e) {System.out.println (E.getmessage ()); } finally{cipher=NULL; } returnBytefina; }}
2, Hexutils.java
PackageDes3;/*** Hex Helper class *@authorJacky*/ Public classHexutils {/**converting Data*/ Private Static Final Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; /*** ToString (controls the length of the conversion of byte[] to the corresponding hexadecimal string representation)*/ Public StaticString toString (byte[] BA,intOffsetintlength) { Char[] buf =New Char[Length * 2]; intj = 0; intK; for(inti = offset; I < offset + length; i++) {k=Ba[i]; Buf[j+ +] = hexdigits[(k >>> 4) & 0x0F]; Buf[j+ +] = hexdigits[k & 0x0F]; } return NewString (BUF); } /*** Function: Convert byte[] to the corresponding hexadecimal string *@paramba byte array *@returnhexadecimal string*/ Public StaticString toString (byte[] ba) { returnToString (BA, 0, ba.length); } /*** Function: Convert hexadecimal string to byte array *@paramhex hexadecimal String *@returnbyte array*/ Public Static byte[] fromstring (String hex) {intLen =hex.length (); byte[] buf =New byte[(len + 1)/2]; inti = 0; intj = 0; if(len% 2) = = 1) {buf[j++] = (byte) Fromdigit (Hex.charat (i++)); } while(I <Len) {Buf[j++] = (byte) ((Fromdigit (Hex.charat (i++) << 4) | Fromdigit (Hex.charat (i++))); } returnbuf; } /*** Fromdigit (converts hexadecimal char to decimal int value)*/ Public Static intFromdigit (Charch) { if(Ch >= ' 0 ' && ch <= ' 9 ') { returnCH-' 0 '; } if(Ch >= ' A ' && ch <= ' F ') { returnCH-' A ' + 10; } if(Ch >= ' a ' && ch <= ' F ') { returnCH-' A ' + 10; } Throw NewIllegalArgumentException ("Invalid hex digit" + ch + "'"); }}
3, Des3utils.java
PackageDes3;/*** 3DES decryption main class, plus decryption calls internal method *@authorQiaozhenwu **/ Public classDes3utils {/*** Dec: (decryption). * @paramKey Key *@paramcontent redaction 16-bit *@returnreturn Result: String*/ Public Staticstring decryption (string key, string content) {Des3encryptutils des=Newdes3encryptutils (); String Enkey= "";//final decryption secret key 48 bitString encontent = "";//Decrypt Content if(Key.length () <= 32) {Enkey= (key + key). substring (0, 48); }Else if(Key.length () >= 48) {Enkey= key.substring (0, 48); } if(content.length () = = 16) {encontent=content; }Else{ if(Content.length () > 16){ Throw NewRuntimeException ("The Encrypt content length more than 16"); }Else if(Content.length () < 16){ Throw NewRuntimeException ("The Encrypt content length less than 16"); }} des.setkey (Enkey.getbytes ()); byte[] Get3desdescode =Des.get3desdescode (hexutils.fromstring (encontent)); returnhexutils.tostring (Get3desdescode). Trim (); } /*** Dec: (encrypted). * @paramKey Key *@paramcontent plaintext contents are 16-bit hexadecimal strings *@returnreturn Result: String*/ Public Staticstring encryption (string key, string content) {Des3encryptutils des=Newdes3encryptutils (); String Enkey= "";//final encryption key 48 bitString encontent = "";//Encrypt content if(Key.length () <= 32) {Enkey= (key + key). substring (0, 48); }Else if(Key.length () >= 48) {Enkey= key.substring (0, 48); } if(content.length () = = 16) {encontent=content; }Else{ if(Content.length () > 16){ Throw NewRuntimeException ("The Encrypt content length more than 16"); }Else if(Content.length () < 16){ Throw NewRuntimeException ("The Encrypt content length less than 16"); }} des.setkey (Enkey.getbytes ()); byte[] bye =Des.get3desenccode (hexutils.fromstring (encontent)); returnhexutils.tostring (bye). Trim (); } Public Static voidMain (string[] args) {String str= Encryption ("12345678123456781234567812345678", "06111111FFFFFFFF"); System.out.println ("Encrypted ciphertext is =====" +str); }}
3DES Encryption Algorithm 32 bytes