AES Encryption using Ciphers in JAVA

In this article, we will make a simple Java application that encrypts and decrypts strings using the AES algorithm.
Let’s get started by making a java class
public class AES{
}
The first step is to initialize some variables. For this step, we only need one (spoiler, there are at least two)
I will also need some final variables
private SecretKey key;
/**
* Possible AES KEY_SIZE values are 128, 192 and 256
* Possible tag length (T_LEN) values are 128, 120, 112, 104 and 96
*/
private final int KEY_SIZE = 128;
private final int T_LEN = 128;
Let’s get into my favorite method : init()
You can use a try-catch and print the stack trace if you want to learn about the possible errors there are, but I will just ignore all the errors for the sake of the simplicity
public void init() throws Exception{
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(KEY_SIZE);
key = generator.generateKey();
}
Now that we have initialized our key, we can add the two encode/decode methods that use Base64 (same as the RSA code)
private String encode(byte[] data){
return Base64.getEncoder().encodeToString(data);
}
private byte[] decode(String data){
return Base64.getDecoder().decode(data);
}
Now, we are ready to roll with the encryption!
public String encrypt(String message) throws Exception{
byte[] messageInBytes = message.getBytes();
Cipher encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding");
encryptionCipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encryptedBytes = encryptionCipher.doFinal(messageInBytes);
return encode(encryptedBytes);
}
Shall we move to the decrypt method? 🤔
Not really because we need something that only the encryption cipher has! HIS IV. So let’s modify our code
private Cipher encryptionCipher;
public String encrypt(String message) throws Exception{
byte[] messageInBytes = message.getBytes();
encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding");
encryptionCipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encryptedBytes = encryptionCipher.doFinal(messageInBytes);
return encode(encryptedBytes);
}
NOTE: You don’t need to put the entire as a class variable, you can just save the IV by using the method .getIV();
on a Cipher object and save it in a byte array.
Now it’s time for the decryption
public String decrypt(String encryptedMessage) throws Exception{
byte[] messageInBytes = decode(encryptedMessage);
Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec spec = new GCMParameterSpec(T_LEN,encryptionCipher.getIV());
decryptionCipher.init(Cipher.DECRYPT_MODE,key,spec);
byte[] decryptedBytes = decryptionCipher.doFinal(messageInBytes);
return new String(decryptedBytes);
}
We’re done! Time to squeeze in the main
public static void main(String[] args) {
try{
AES aes = new AES();
aes.init();
String encryptedMessage = aes.encrypt("TheXCoders");
String decryptedMessage = aes.decrypt(encryptedMessage);
System.err.println("Encrypted Message : "+encryptedMessage);
System.err.println("Decrypted Message : "+decryptedMessage);
}catch (Exception ignored){}
}
And here’s the result:
Encrypted Message : qMOVIjYfr8gwBGnIUHh2YuX48AFnk9TH+U0=
Decrypted Message : TheXCoders
And the final code can be found in my gist:
If you want to watch the video here it isss:
Thank you for taking the time to read! If you want to suggest what I would do next, feel free to comment on the video!