AES 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       :  DcAESUtil
 * @author     :  ncc
 * create time : 2017-12-19 10:13:17 am
 * @version    :  1.0  
 * description : Advanced Encryption Standard (AES) in AES cryptography, also known as Advanced Encryption Standard
 * Rijndael encryption method is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES,
 * Has been analyzed by multiple parties and used worldwide. After a five-year selection process, the Advanced Encryption Standard was established by the American National Standards and
 * The Institute of Technology (NIST) published FIPS PUB 197 on November 26, 2001, and became a valid standard on May 26, 2002.
 * In 2006, Advanced Encryption Standard had become one of the most popular algorithms in symmetric key encryption.
 * This algorithm was designed by Belgian cryptographers Joan Daemen and Vincent Rijmen, combining the names of the two authors,
 * Named after Rijndael, the selection process for submitting advanced encryption standards. (Rijdael is pronounced close to "Rhinedoll".)
 * @see        :                        
 * ************************************************/   
public class DcAESUtil {

	// 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;

	public DcAESUtil() 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("AES");
		// generate key
		deskey = keygen.generateKey();
		// Generate a Cipher object and specify the DES algorithm it supports
		c = Cipher.getInstance("AES");
	}

	/* ********************************************
	 * 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 {
		DcAESUtil de = new DcAESUtil ();
		String msg = "Welcome to the home of the grass!";
		byte[] encontent = de.Encrytor(msg);
		byte[] decontent = de.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=326484623&siteId=291194637