3DES encryption and decryption

package com.dc;

import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;  
import java.security.Security;  
  
import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.KeyGenerator;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.SecretKey;  

/* ******************** Class description ********************
 * class       :  DcDES3Util
 * @author     :  ncc
 * create time : 2017-12-19 10:01:53 am
 * @version    :  1.0  
 * description : 3DES, also known as Triple DES, is a mode of DES encryption algorithm, which uses three 56-bit keys to 3DES
 * Data is encrypted three times. Data Encryption Standard (DES) is a long-established encryption standard in the United States,
 * It uses symmetric key cryptography and was standardized by the ANSI organization as ANSI X.3.92 in 1981.
 * DES uses a 56-bit key and a cipher block method, and in the cipher block method, the text is divided into 64-bit sized
 * The text block is then encrypted. 3DES is more secure than the original DES. 3DES (i.e. Triple DES) is
 * DES to AES transition encryption algorithm (in 1999, NIST designated 3-DES as the transition encryption standard),
 * is a more secure variant of DES. It takes DES as the basic module, and designs the block encryption algorithm by combining the grouping method.
 * Its specific implementation is as follows:
 * Let Ek() and Dk() represent the encryption and decryption process of the DES algorithm, K represents the key used by the DES algorithm, P represents the plaintext, C represents the ciphertext,
 * In this way, the 3DES encryption process is: C=Ek3(Dk2(Ek1(P)))
 * The 3DES decryption process is: P=Dk1((EK2(Dk3(C)))
 * @see        :                        
 * ************************************************/   
public class DcDES3Util {  
  
    // KeyGenerator provides the function of a symmetric key generator and supports various algorithms  
    private KeyGenerator keygen;  
    // SecretKey is responsible for storing the symmetric key  
    private SecretKey deskey;  
    // Cipher is responsible for completing the encryption or decryption work  
    private Cipher c;  
    // This byte array is responsible for storing the encrypted result  
    private byte[] cipherByte;  
  
    /**
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     */
    public DcDES3Util() throws NoSuchAlgorithmException, NoSuchPaddingException {  
        Security.addProvider(new com.sun.crypto.provider.SunJCE());  
        // Instantiate a key generator that supports the DES algorithm (the algorithm name must be named as specified, otherwise an exception will be thrown)  
        keygen = KeyGenerator.getInstance("DESede");  
        // generate key  
        deskey = keygen.generateKey();  
        // Generate a Cipher object and specify the DES algorithm it supports  
        c = Cipher.getInstance("DESede");  
    }  
  
    /* ********************************************
     * method name   : Encrytor
     * description : encrypt the string
     * @return       : byte[]
     * @param : @param str
     * @param        : @return
     * @param        : @throws InvalidKeyException
     * @param        : @throws IllegalBlockSizeException
     * @param        : @throws BadPaddingException
     * modified      : ncc ,  2017-12-19
     * @see          :
     * ********************************************/      
    public byte[] Encrytor(String str) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        // Initialize the Cipher object according to the key, ENCRYPT_MODE represents the encryption mode  
        c.init(Cipher.ENCRYPT_MODE, deskey);  
        byte[] src = str.getBytes();  
        // Encrypt, save the result into cipherByte  
        cipherByte = c.doFinal(src);  
        return cipherByte;  
    }  
  
    /* ********************************************
     * method name   : Decryptor
     * description : decrypt the string
     * @return       : byte[]
     * @param        : @param buff
     * @param        : @return
     * @param        : @throws InvalidKeyException
     * @param        : @throws IllegalBlockSizeException
     * @param        : @throws BadPaddingException
     * modified      : ncc ,  2017-12-19
     * @see          :
     * ********************************************/      
    public byte[] Decryptor(byte[] buff) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        // Initialize the Cipher object according to the key, DECRYPT_MODE represents the encryption mode  
        c.init(Cipher.DECRYPT_MODE, deskey);  
        cipherByte = c.doFinal(buff);  
        return cipherByte;  
    }  
  
    /**
     * @param args
     * @throws NoSuchPaddingException  
     * @throws NoSuchAlgorithmException  
     * @throws BadPaddingException  
     * @throws IllegalBlockSizeException  
     * @throws InvalidKeyException  
     */  
    public static void main(String[] args) throws Exception {  
    	DcDES3Util des3 = new DcDES3Util();  
        String msg = "Welcome to Decao Home!";  
        byte[] encontent = des3.Encrytor(msg);  
        byte[] decontent = des3.Decryptor(encontent);  
        System.out.println("Plaintext is: " + msg);  
        System.out.println("After encryption:" + new String(encontent));  
        System.out.println("After decryption:" + new String(decontent));  
  
    }  
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326434202&siteId=291194637