Implement 3DES with Java

Source: Internet
Author: User

3DES, or Triple DES, is the enhanced version of Des and also a more secure variant of DES. It uses 3 56-bit (168-bit) keys to encrypt the data three times, and compared to DES, the security has been greatly improved.

In fact, 3DES is a transition encryption algorithm. In 1999, NIST designated 3-des as the Encryption standard for DES-to-AES transitions.

3DES with Des as the basic module, the group encryption algorithm is designed by combining grouping method. If the three keys are different, it is essentially equivalent to using a 168-bit key for encryption, which greatly enhances the security of the data.

If the data security requirements are not high, you can make the two keys equal, so that the effective length of the key is 112 bits.

In the Java encryption system, the use of 3DES is very simple, the program structure and the use of DES is the same, but at the initialization of the algorithm name from "DES" to "Desede".

The basic principles of the following programs are the same as p12_01, except that the encryption and decryption processes are written together.

Import java.security.Security;

Import Javax.crypto.Cipher;

Import Javax.crypto.KeyGenerator;

Import Javax.crypto.SecretKey;

public class P12_02 {

public static void Main (string[] args) throws Exception {

Keygenerator provides the function of symmetric key generator to support various algorithms

Keygenerator keygen;

Secretkey is responsible for saving the symmetric key

Secretkey Deskey;

Cipher is responsible for completing the encryption or decryption work

Cipher C;

Security.addprovider (New Com.sun.crypto.provider.SunJCE ());

Instantiate a key generator that supports the 3DES algorithm, with the name of the algorithm Desede

keygen = keygenerator.getinstance ("Desede");

Generate key

Deskey = Keygen.generatekey ();

Generates a Cipher object that specifies that it supports 3DES algorithms

c = cipher.getinstance ("Desede");

String msg = "Guo Kewa _ Secure Programming Technology";

System.out.println ("Clear text is:" + msg);

Initializes the cipher object according to the key, Encrypt_mode represents the encryption mode

C.init (Cipher.encrypt_mode, Deskey);

byte[] src = msg.getbytes ();

Encryption, results saved into enc

byte[] enc = c.dofinal (SRC);

System.out.println ("Ciphertext is:" + new String (ENC));

Initializes the cipher object according to the key, Encrypt_mode represents the encryption mode

C.init (Cipher.decrypt_mode, Deskey);

Decryption, results saved in Dec

byte[] Dec = c.dofinal (ENC);

SYSTEM.OUT.PRINTLN ("Decrypted result is:" + new String (dec));

}

}

Implement 3DES with Java

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.