Java encryption and decryption technology series 3DES
- Order
- Background
- Concept
- Principle
- Code implementation
- Conclusion
Order
The previous article is about symmetric encryption algorithm--des, this article intends to continue to speak a little more on the basis of DES, that is, 3 des--triple des.
Background
Why does 3DES appear? In fact, this is not difficult to think of. Because DES is a very simple encryption algorithm, but the key length is relatively short, the computational amount is small, relatively speaking, more easily cracked. Therefore, on the basis of DES, using triple Data encryption algorithm, data encryption, so that the probability of cracking is much smaller.
Concept
3DES, the "Triple des", the Chinese name "Triple Data encryption algorithm", it is equivalent to each data block application three times DES encryption algorithm. Because of the enhancement of computing ability, the key length of the original des password becomes easy to be cracked; 3DES is designed to provide a relatively simple way to avoid similar attacks by increasing the key length of DES, rather than designing a new block cipher algorithm.
Principle
Data is encrypted three times using 3 56-bit keys. 3DES (i.e. Triple des) is an encryption algorithm for DES-to-AES transitions (in 1999, NIST designates 3-des as the encryption standard for transitions).
Its implementation is as follows: Set Ek () and Dk () on behalf of the DES algorithm encryption and decryption process, K for the DES algorithm used by the key, P for the plaintext, C for ciphertext, so:
The 3DES encryption process is:C = Ek3 (Dk2 (EK1 (P)))
The 3DES decryption process is:P = Dk1 (EK2 (DK3 (C)))
Code implementation
3DES code implementation, and DES very similar, in fact, can refer to the previous article--des code implementation, the algorithm definition to Desede can be. However, considering the convenience of reference, here is also posted 3DES plus decryption code implementation, for your reference.
ImportJavax.crypto.Cipher; ImportJavax.crypto.SecretKey; ImportJavax.crypto.spec.SecretKeySpec; ImportJava.io.ByteArrayOutputStream; Importjava.security.Security; /*** Created by Xiang.li on 2015/3/19. * TripleDES (3DES) plus decryption tool class*/ Public classTripleDES {Private Static FinalString algorithm = "Desede";//define cryptographic algorithms, available Des,desede,blowfish Private Static FinalString hexstring= "0123456789ABCDEF"; /** * * @paramKeybyte encryption key with a length of 24 bytes *@paramsrc byte array, which constructs a key based on a given byte array. ) * @return */ Public Static byte[] Encryptmode (byte[] Keybyte,byte[] src) { Try { //constructs a key based on the given byte array and algorithmSecretkey Deskey =NewSecretkeyspec (keybyte, algorithm); //EncryptCipher C1 =cipher.getinstance (algorithm); C1.init (Cipher.encrypt_mode, Deskey); returnc1.dofinal (SRC); } Catch(java.security.NoSuchAlgorithmException E1) {e1.printstacktrace (); } Catch(javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace (); } Catch(Java.lang.Exception e3) {e3.printstacktrace (); } return NULL; } /** * * @paramkeybyte Key *@paramSRC needs to decrypt the data *@return */ Public Static byte[] Decryptmode (byte[] Keybyte,byte[] src) { Try { //Generate keySecretkey Deskey =NewSecretkeyspec (keybyte, algorithm); //decryptionCipher C1 =cipher.getinstance (algorithm); C1.init (Cipher.decrypt_mode, Deskey); returnc1.dofinal (SRC); } Catch(java.security.NoSuchAlgorithmException E1) {e1.printstacktrace (); } Catch(javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace (); } Catch(Java.lang.Exception e3) {e3.printstacktrace (); } return NULL; } /*** string is converted to 16 binary *@paramSTR *@return */ Public Staticstring Encode (String str) {//get byte array by default encoding byte[] bytes=str.getbytes (); StringBuilder SB=NewStringBuilder (bytes.length*2); //splits each byte in a byte array into a 2-bit 16-decimal integer for(inti=0;i<bytes.length;i++) {sb.append (Hexstring.charat (bytes[i)&0XF0) >>4)); Sb.append (Hexstring.charat (bytes[i)&0x0f) >>0)); } returnsb.tostring (); } /** * * @parambytes *@return* Decodes 16 binary digits into a string for all characters (including Chinese)*/ Public Staticstring Decode (string bytes) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (Bytes.length ()/2); //assemble every 2 bits of 16 integers into one byte for(intI=0;i<bytes.length (); i+=2) Baos.write ((Hexstring.indexof (Bytes.charat (i) )<<4 |hexstring.indexof (Bytes.charat (i+1)))); return NewString (Baos.tobytearray ()); } //convert to hexadecimal string Public StaticString Byte2hex (byte[] b) {String HS= ""; String stmp= ""; for(intn = 0; n < b.length; n++) {stmp= (java.lang.Integer.toHexString (b[n) & 0XFF)); if(stmp.length () = = 1) HS= HS + "0" +stmp; ElseHS= HS +stmp; if(N < b.length-1) HS= HS + ""; } returnhs.touppercase (); } Public Static voidMain (string[] args) {//add a new security algorithm, and if you use JCE, add it in.//The AddProvider method here is to add a new cryptographic algorithm provider (personal understanding does not find a good answer, to supplement)Security.addprovider (NewCom.sun.crypto.provider.SunJCE ()); //byte array (used to generate the key) Final byte[] keybytes = {0x11, 0x22, 0x4f, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xe2 }; String szsrc= "This is a 3DES test. Test; System.out.println ("String before encryption:" +szsrc); byte[] encoded =Encryptmode (Keybytes, Szsrc.getbytes ()); System.out.println ("Encrypted string:" +Byte2hex (encoded)); byte[] Srcbytes =Decryptmode (keybytes, encoded); System.out.println ("Decrypted string:" +NewString (srcbytes)); } }
6.3DES of Java Encryption and decryption technology series