AES Encryption and Decryption using JAVA.

Gagan Jain
4 min readMar 11, 2023

--

AES (Advanced Encryption Standard) is a symmetric-key encryption algorithm widely used for secure communication and data protection. It was selected by the US National Institute of Standards and Technology (NIST) as the standard for encryption in 2001, replacing the older and less secure Data Encryption Standard (DES).

AES operates on fixed-length blocks of data (128 bits), and the encryption and decryption keys are of the same length (128, 192, or 256 bits).

Here are the steps involved in AES encryption and decryption:

AES Encryption:

  1. Key Expansion: AES generates a set of round keys from the main encryption key through a process called key expansion. The key expansion process creates a set of subkeys that will be used in each round of encryption. The number of rounds is determined by the key size: 10 rounds for 128-bit keys, 12 rounds for 192-bit keys, and 14 rounds for 256-bit keys.
  2. Initial Round: AES begins with an initial round, where the input data block is XORed with the first subkey.
  3. Main Rounds: After the initial round, the algorithm performs a series of main rounds. Each main round consists of four operations:
  • SubBytes: a byte substitution using a fixed substitution table called the S-box.
  • ShiftRows: a permutation of the rows of the state array.
  • MixColumns: a linear transformation of the columns of the state array.
  • AddRoundKey: XOR the state array with the subkey.

4. Final Round: The final round of AES is similar to the main rounds, but it omits the MixColumns operation.

5. Output: After the final round, the encrypted data block is obtained by reading the state array in row-major order.

AES Decryption:

  1. Key Expansion: AES decryption uses the same set of round keys as encryption, but in reverse order.
  2. Initial Round: AES decryption begins with an initial round, where the input data block is XORed with the last subkey.
  3. Main Rounds: The decryption main rounds are similar to the encryption main rounds, but they use inverse operations:
  • InvSubBytes: a byte substitution using the inverse of the S-box.
  • InvShiftRows: a permutation of the rows of the state array, but in the opposite direction.
  • InvMixColumns: a linear transformation of the columns of the state array, but using a different matrix than MixColumns.
  • AddRoundKey: XOR the state array with the subkey.

4. Final Round: The final round of decryption is similar to the initial round, where the state array is XORed with the first subkey.

5. Output: After the final round, the decrypted data block is obtained by reading the state array in row-major order.

Encryption and Decryption using shared secret key

Overall, AES encryption and decryption are complex processes that involve several operations and rounds of computation. The strength of AES lies in its ability to provide high levels of security while being computationally efficient on modern hardware.

Java Code: Here is the Java code for AES encryption and decryption using the standard Java Cryptography Extension (JCE) library:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class AES {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";

public static byte[] encrypt(String plainText, String key) throws Exception {
Key secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

return cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
}

public static String decrypt(byte[] cipherText, String key) throws Exception {
Key secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] decryptedBytes = cipher.doFinal(cipherText);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}

To use this code, you can call the encrypt() method to encrypt a plaintext string, and the decrypt() method to decrypt a ciphertext byte array. Here is an example:

String plaintext = "This is a test plaintext string.";
String key = "1234567890123456";

try {
byte[] encryptedBytes = AES.encrypt(plaintext, key);
System.out.println("Encrypted bytes: " + Arrays.toString(encryptedBytes));

String decryptedText = AES.decrypt(encryptedBytes, key);
System.out.println("Decrypted text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}

Note that in this example, we are using the ECB (Electronic Codebook) mode of operation, which is not recommended for secure encryption because it does not provide sufficient security against certain attacks. It is recommended to use a mode like CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode) for better security.

Happy Learning !! :)

--

--