Java 3DES encryption problem logging

Source: Internet
Author: User
Tags decrypt


3DES encryption has different encryption mode and fill mode, this online a lot of said, as long as the encryption mode and the filling mode to ensure that the decryption is consistent with the first for the key generation, there are 2 ways in Java: 1. First, with ECB mode and no fill mode
//EncryptionPublicStaticByte[] DES3ENCODEECB (Byte[] Key,Byte[] data)ThrowsException {Key Deskey =Null; Secretkeyspec spec =New Secretkeyspec (Key, "Desede"); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede"); Deskey =Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/ecb/nopadding"); Cipher.init (Cipher.encrypt_mode, Deskey);byte[] BOut =Cipher.dofinal (data);ReturnBOut; }//DecryptPublicStaticByte[] DES3DECODEECB (byte[] key, byte[] data) throws Exception {key Deskey = null; Secretkeyspec spec = new Secretkeyspec (Key, "Desede"); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede"); Deskey = Keyfactory.generatesecret ( SPEC); Cipher Cipher = cipher.getinstance ("Desede" + "/ecb/nopadding"); Cipher.init (Cipher.decrypt_mode, Deskey); byte[] BOut = cipher.dofinal (data); return BOut;}               

2. The second, also using the ECB mode and the non-fill mode
//Encrypt     Public Static byte[] Encrypt (byte[] targettobyte, String algorithm,string mode, string key)throwsException {Cipher Cipher=cipher.getinstance (algorithm);        Cipher.init (Cipher.encrypt_mode, GetKey (Key, MODE)); byte[] result =cipher.dofinal (Targettobyte); //System.out.println ("base64:" +encryptbybase64 (Result));        returnresult; }//decryption     Public Static byte[] Decrypt (byte[] targettobyte, String algorithm,string mode, string key)throwsException {Cipher Cipher=cipher.getinstance (algorithm);        Cipher.init (Cipher.decrypt_mode, GetKey (Key, MODE)); byte[] result =cipher.dofinal (Targettobyte); //System.out.println ("Base:" + new String (result, "UTF-8"));        returnresult; }      Public Statickey GetKey (string key, String algorithm) {Try{keygenerator Generator=keygenerator.getinstance (algorithm); //note here that the key is parsed into a byte array according to the 16-digit number, as needed, such as key.getbytes ()Generator.init (NewSecureRandom (Hexstring2bytes (key))); returnGenerator.generatekey (); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }

   Two ways of difference, the first is to use your given key as the key, when communicating with other clients, the encryption is not the same side of the recommended use when the operation, the second is based on the key you gave to generate a new key, the new key is the real encryption of the key used, So if you use the first encryption out of the ciphertext with the second to decrypt, it is not solved. In addition, Java only provides 3 times times the length of 3DES algorithm, that is, the key must be 24 bytes long, if you want to use twice times the length of 3DES, you need to complete the next 8 bytes (that is, 16 bytes of the first 8 bytes to the last, into 24 bytes). If the supplied key is less than 24 bytes, it will be an error, if more than 24 bytes, will intercept the first 24 bytes as key (pit me for a long time)the 3DES encryption process3DES, divided into 2 kinds, one is double long 3DES, one is three times times the length of 3DES. If it is double 3DES long, the key is 16 bytes long, press the left and right, respectively LK (8 bytes to the left of the key), RK (8 bytes of key on the side). Encrypted content data is 8 bytes. Suppose that the single-length des encryption process is: des (data, Key, dest), where data is encrypted, key is the encryption key, and dest is the result of encryption. The single-length des decryption process is: udes (data, key, dest), where data is decrypted, key is the decryption key, dest is the decrypted result. So, the double-length 3DES encryption method is: DES (DATA, LK, TMP1); Udes (TMP1, RK, TMP2);D es (DATA, LK, DEST);D EST is the final cipher. The specific process is summarized as follows: 1) Use the first 8 bytes of the key, encrypt the data, get the encrypted result tmp1;2) use the key after 8 bytes, the first calculation results TMP1, decrypt, get decrypted results tmp2;3) again using the first 8 bytes of the key, The results of the second calculation are TMP2, encrypted, and the results dest. The dest is the final result. For three times times 3DES, the key length is 24 bytes long. Can be divided into LK (8 bytes to the left of the key), CK (the middle 8 bytes of the key), RK (8 bytes to the left of the key). With twice times the 3DES encryption process is basically the same, just the first calculation, using the key LK; the second calculation, using the key CK; third calculation, using the key LK. The basic process is as follows: DES (DATA, LK, TMP1); Udes (TMP1, CK, TMP2);D es (TMP2, RK, DEST);

Java 3DES encryption problem logging

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.