Java encryption and decryption technology series 3DES

Source: Internet
Author: User
Tags decrypt



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.
<span style= "Font-family:comic Sans ms;font-size:12px;" >package Com.sica.des;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;import java.io.bytearrayoutputstream;import java.security.security;/** * Created by Xiang.li on 2015/3/19. * TripleDES (3DES) Decryption Tool class */public classes TripleDES {private static final String algorithm = "Desede";//define encryption algorithm, available DES    , desede,blowfish private static final String hexstring= "0123456789ABCDEF"; /** * * @param keybyte encryption key, length 24 bytes * @param src byte array (constructs a key based on the given byte array. * @return */public static byte[] Encryptmode (byte[] keybyte, byte[] src) {try {///according to the given word            Section arrays and algorithms construct a key secretkey Deskey = new Secretkeyspec (keybyte, algorithm);            Encryption Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.encrypt_mode, Deskey);        return c1.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; }/** * * @param keybyte key * @param src need to decrypt data * @return */public static byte[] Decrypt Mode (byte[] keybyte, byte[] src) {try {//generate key Secretkey Deskey = new Secretkeyspec (keybyte            , algorithm);            Decrypt Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.decrypt_mode, Deskey);        return c1.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 converted to 16 * @param str * @return */PUblic static string encode (String str) {//Gets the byte array byte[] Bytes=str.getbytes () based on the default encoding;        StringBuilder sb=new StringBuilder (bytes.length*2); Splits each byte in a byte array into a 2-bit 16-in integer for (int i=0;i<bytes.length;i++) {sb.append (Hexstring.charat (Bytes[i]            &AMP;0XF0) (>>4));        Sb.append (Hexstring.charat ((bytes[i]&0x0f) >>0));    } return sb.tostring (); }/** * * @param bytes * @return * Decodes 16 binary digits into a string for all characters (including Chinese) */public static string decode (St        Ring bytes) {bytearrayoutputstream baos=new Bytearrayoutputstream (Bytes.length ()/2); Assemble each 2-bit 16 integer into a byte for (int i=0;i<bytes.length (); i+=2) Baos.write ((Hexstring.indexof (Bytes.charat (i))        <<4 |hexstring.indexof (Bytes.charat (i+1)));    return new String (Baos.tobytearray ());        }//Convert to hexadecimal string public static string Byte2hex (byte[] b) {string hs = "";        String stmp = ""; for (int n = 0; n < b.length;            n++) {stmp = (java.lang.Integer.toHexString (b[n] & 0XFF));            if (stmp.length () = = 1) HS = HS + "0" + stmp;            else HS = hs + stmp;        if (n < b.length-1) HS = HS + "";    } return Hs.touppercase (); } public static void Main (string[] args) {//Add new security algorithm, if use JCE to add it in//here AddProvider method is to add a new cryptographic algorithm provider (personal understanding        No good answers found, supplement) Security.addprovider (New Com.sun.crypto.provider.SunJCE ()); byte array (used to generate the key) final byte[] Keybytes = {0x11, 0x22, 0x4f, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x 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:" + new String (srcbytes)); }}</span>


Conclusion
Because 3DES is on the basis of DES development, therefore, the basic principle of 3DES and des difference is no two, but also on the basis of a number of improvements, understand these, and then use 3DES, it is really easy, although the implementation of sun you do not understand, but probably the principle of clear, and then understand Sun's source code, its own implementation is still very easy.

Java encryption and decryption technology series 3DES

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.