Advanced Encryption Standard (AES), also known as Rijndael encryption method in cryptography, is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, and has been analyzed by multiple parties and is widely used worldwide.
Most AES calculations are done in a particular finite domain.
The AES encryption process operates on a 4×4 byte matrix, also known as "state", and its initial value is a plaintext block (a element size in the matrix is a Byte in the plaintext block). (Rijndael encryption method supports larger blocks, and its matrix row count can be increased as appropriate) During encryption, each round of AES encryption loop (except the last round) contains 4 steps:
The MixColumns step is omitted in the last encryption loop, and replaced by another AddRoundKey.
Basic Java 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 became an effective standard in 2005*/ 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-bit; Initialization vector (IV) must be 16-bit* The length of the content to be encrypted must be a multiple of 16. If it is not a multiple of 16, the following exception will appear: * javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes * * Since the number of bits is fixed, the encrypted data is in Chinese, and the addition and decryption are incomplete* * You can see that when the original data length is n times the integer of 16, if the original data length is equal to 16*n, the encrypted data length is equal to 16*n when using NoPadding, * In other cases, the encrypted data length is equal to 16*n. In the case of less than 16 integer multiples, if the original data length is equal to 16*n+m[where m is less than 16], * In any way except NoPadding padding, the encrypted data length 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 country is national wide"); method2("a*jal)k32J8czx country is national wide"); method3("a*jal)k32J8czx is the country's sanitation"); method4("123456781234 is the country's sanitation");// length = 16 method4("12345678abcdefgh");// length = 16 } /** * Use the AES algorithm to encrypt, default mode AES/ECB */ static void method1(String str) throws Exception { cipher = Cipher.getInstance(KEY_ALGORITHM); //KeyGenerator generates aes algorithm 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 the key using encryption mode byte[] encrypt = cipher.doFinal(str.getBytes()); //Encrypt or decrypt the data according to a single-part operation, or end a multi-part operation. System.out.println("method1-encrypted: " + Arrays.toString(encrypt)); cipher.init(Cipher.DECRYPT_MODE, secretKey);//Initialize the key using decryption mode byte[] decrypt = cipher.doFinal(encrypt); System.out.println("method1-decrypted: " + new String(decrypt)); } /** * Encrypted using the AES algorithm, default mode AES/ECB/PKCS5Padding */ static void method2(String str) throws Exception { cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB); //KeyGenerator generates aes algorithm 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 the key using encryption mode byte[] encrypt = cipher.doFinal(str.getBytes()); //Encrypt or decrypt data according to a single-part operation, or end a multi-part operation. System.out.println("method2-encrypted: " + Arrays.toString(encrypt)); cipher.init(Cipher.DECRYPT_MODE, secretKey);//Initialize the key using decryption mode byte[] decrypt = cipher.doFinal(encrypt); System.out.println("method2-decrypted: " + new String(decrypt)); } static byte[] getIV() { String iv = "1234567812345678"; //IV length: must be 16 bytes long return iv.getBytes(); } /** * Use the AES algorithm to encrypt, default mode AES/CBC/PKCS5Padding */ static void method3(String str) throws Exception { cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC); //KeyGenerator generates aes algorithm 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 the key using encryption mode byte[] encrypt = cipher.doFinal(str.getBytes()); //Encrypt or decrypt the data according to a single-part operation, or end a multi-part operation. System.out.println("method3-encrypted: " + Arrays.toString(encrypt)); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));//Initialize the key using decryption mode byte[] decrypt = cipher.doFinal(encrypt); System.out.println("method3-decrypted: " + new String(decrypt)); } /** * Encrypted using the AES algorithm, default mode AES/CBC/NoPadding See above for data limitations for this mode*/ static void method4(String str) throws Exception { cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC_NoPadding); //KeyGenerator generates aes algorithm 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 the key using encryption mode byte[] encrypt = cipher.doFinal(str.getBytes(), 0, str.length()); //Encrypt or decrypt data according to a single-part operation, or end a multi-part operation. System.out.println("method4-encrypt: " + Arrays.toString(encrypt)); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));//Initialize the key using decryption mode byte[] decrypt = cipher.doFinal(encrypt); System.out.println("method4-decrypted: " + new String(decrypt)); } }