Simple example sharing of Java implementation of AES encryption algorithm

  • 2020-05-07 19:49:16
  • OfStack

Advanced encryption standard (Advanced Encryption Standard, AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the United States federal government. This standard, which replaces the original DES, has been analyzed by many parties and is widely used around the world.
Most AES calculations are performed in a particular finite field.
The AES encryption process operates on a 4 by 4 byte matrix, also known as the state (state), whose initial value is one plaintext block (the size of one element in the matrix is one Byte in the plaintext block). (since Rijndael encryption method supports a larger block, the number of matrix rows can be increased as the case may be.) when encrypting, each round of AES encryption cycle (except the last round) contains four steps:

AddRoundKey - each byte in the matrix performs XOR operations with the secondary secret key (round key); Each subkey is generated by the key generation scheme. SubBytes - replaces each byte with its corresponding byte in the form of a lookup table using a nonlinear substitution function. ShiftRows - moves each column in the matrix in a circular fashion. MixColumns - operations to fully mix the individual straight lines of the matrix. This step USES linear transformations to mix the four bytes of each column.

The last encryption loop omits the MixColumns step and replaces it with another AddRoundKey.

Java basic implementation:


package com.stone.security; 
 
import java.util.Arrays; 
 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
 
/** 
 * AES  algorithm   Symmetric encryption, the advanced encryption standard in cryptography  2005 Year becomes effective standard  
 */ 
public class AES { 
 static Cipher cipher; 
 static final String KEY_ALGORITHM = "AES"; 
 static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding"; 
 static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding"; 
/** 
 * AES/CBC/NoPadding  requirements  
 *  The key must be 16 A; Initialization vector (IV)  It must be 16 position  
 *  The length of the content to be encrypted must be 16 If not 16 A multiple of, the following exception will occur:  
 * javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes 
 * 
 *  Because of the fixed number of bits, so for the encrypted data has Chinese ,  Incomplete encryption and decryption  
 * 
 *  Can be   To see that the length of the original data is 16 The integer n If the length of the original data is equal to 16*n , use the NoPadding Is the length of the encrypted data 16*n .  
 *  Otherwise encrypt data length   Degree is equal to the 16*(n+1) . In less than 16 If the length of the original data is equal to 16*n+m[ Among them m Less than 16] .  
 *  In addition to NoPadding Anything other than fill   Where, the length of encrypted data is equal to 16*(n+1). 
 */ 
 static final String CIPHER_ALGORITHM_CBC_NoPadding = "AES/CBC/NoPadding"; 
 
 static SecretKey secretKey; 
  
 public static void main(String[] args) throws Exception { 
 method1("a*jal)k32J8czx Every country is its width "); 
 method2("a*jal)k32J8czx Every country is its width "); 
 method3("a*jal)k32J8czx Every country is its width "); 
  
 method4("123456781234 � for wide country ");// length = 16 
 method4("12345678abcdefgh");// length = 16 
  
 } 
 
 /** 
 *  use AES  algorithm   Encryption, default mode  AES/ECB 
 */ 
 static void method1(String str) throws Exception { 
 cipher = Cipher.getInstance(KEY_ALGORITHM); 
 //KeyGenerator  generate aes Algorithm is key  
 secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey(); 
 System.out.println(" The length of the key is: " + secretKey.getEncoded().length); 
  
 cipher.init(Cipher.ENCRYPT_MODE, secretKey);// Initialize using encryption mode   The key  
 byte[] encrypt = cipher.doFinal(str.getBytes()); // Encrypt or decrypt data, or end it, as a one-part operation 1 Multiple part operations.  
  
 System.out.println("method1- Encryption: " + Arrays.toString(encrypt)); 
 cipher.init(Cipher.DECRYPT_MODE, secretKey);// Class using decryption mode   The key  
 byte[] decrypt = cipher.doFinal(encrypt); 
 System.out.println("method1- After decryption: " + new String(decrypt)); 
  
 } 
 
 /** 
 *  use AES  algorithm   Encryption, default mode  AES/ECB/PKCS5Padding 
 */ 
 static void method2(String str) throws Exception { 
 cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB); 
 //KeyGenerator  generate aes Algorithm is key  
 secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey(); 
 System.out.println(" The length of the key is: " + secretKey.getEncoded().length); 
  
 cipher.init(Cipher.ENCRYPT_MODE, secretKey);// Initialize using encryption mode   The key  
 byte[] encrypt = cipher.doFinal(str.getBytes()); // Encrypt or decrypt data, or end it, as a one-part operation 1 Multiple part operations.  
  
 System.out.println("method2- Encryption: " + Arrays.toString(encrypt)); 
 cipher.init(Cipher.DECRYPT_MODE, secretKey);// Class using decryption mode   The key  
 byte[] decrypt = cipher.doFinal(encrypt); 
 System.out.println("method2- After decryption: " + new String(decrypt)); 
  
 } 
 
 static byte[] getIV() { 
 String iv = "1234567812345678"; //IV length: must be 16 bytes long 
 return iv.getBytes(); 
 } 
 
 /** 
 *  use AES  algorithm   Encryption, default mode  AES/CBC/PKCS5Padding 
 */ 
 static void method3(String str) throws Exception { 
 cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC); 
 //KeyGenerator  generate aes Algorithm is key  
 secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey(); 
 System.out.println(" The length of the key is: " + secretKey.getEncoded().length); 
  
 cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));// Initialize using encryption mode   The key  
 byte[] encrypt = cipher.doFinal(str.getBytes()); // Encrypt or decrypt data, or end it, as a one-part operation 1 Multiple part operations.  
  
 System.out.println("method3- Encryption: " + Arrays.toString(encrypt)); 
 cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));// Class using decryption mode   The key  
 byte[] decrypt = cipher.doFinal(encrypt); 
 System.out.println("method3- After decryption: " + new String(decrypt)); 
  
 } 
 
 /** 
 *  use AES  algorithm   Encryption, default mode  AES/CBC/NoPadding  See above for this mode Data limitation of  
 */ 
 static void method4(String str) throws Exception { 
 cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC_NoPadding); 
 //KeyGenerator  generate aes Algorithm is key  
 secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey(); 
 System.out.println(" The length of the key is: " + secretKey.getEncoded().length); 
  
 cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));// Initialize using encryption mode   The key  
 byte[] encrypt = cipher.doFinal(str.getBytes(), 0, str.length()); // Encrypt or decrypt data, or end it, as a one-part operation 1 Multiple part operations.  
  
 System.out.println("method4- Encryption: " + Arrays.toString(encrypt)); 
 cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));// Class using decryption mode   The key  
 byte[] decrypt = cipher.doFinal(encrypt); 
  
 System.out.println("method4- After decryption: " + new String(decrypt)); 
  
 } 
 
} 


Related articles: