AES Encryption using Ciphers in JAVA | Creating a Client-Server application

In this article, we will make so changes to the previous Java application that encrypts and decrypts strings using the AES algorithm to make it able to be used by a client and a server.

Let’s get started by making some changes. Instead of having the encryptionCypher as a class variable and since we only need the IV we’ll just put the IV there.

private SecretKey key;
private SecretKey key;
private int KEY_SIZE = 128;
private int T_LEN = 128;
private byte[] IV;

Next step is to adapt the changes, we will only change the encrypt and decrypt methods

public String encrypt(String message) throws Exception {
    byte[] messageInBytes = message.getBytes();
    Cipher encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding");
    encryptionCipher.init(Cipher.ENCRYPT_MODE, key);
    IV = encryptionCipher.getIV();
    byte[] encryptedBytes = encryptionCipher.doFinal(messageInBytes);
    return encode(encryptedBytes);
}


public String decrypt(String encryptedMessage) throws Exception {
    byte[] messageInBytes = decode(encryptedMessage);
    Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding");
    GCMParameterSpec spec = new GCMParameterSpec(T_LEN, IV);
    decryptionCipher.init(Cipher.DECRYPT_MODE, key, spec);
    byte[] decryptedBytes = decryptionCipher.doFinal(messageInBytes);
    return new String(decryptedBytes);
}

Now we can run the application with no problem.

If we want to create a client/server application, we will need to get the key, or shall I say keys: One for the SecretKey and the other for the IV.

To make it easier, we will create a method exportKeys()

public void init() throws Exception{
private void exportKeys(){
    System.err.println("SecretKey : "+encode(key.getEncoded()));
    System.err.println("IV : "+encode(IV));
}

And now what’s left to do is to call it in the main method to export our keys

public static void main(String[] args) {
    try {
        AES aes = new AES();
        aes.init();
        String encryptedMessage = aes.encrypt("TheXCoders_2");
        String decryptedMessage = aes.decrypt(encryptedMessage);

        System.err.println("Encrypted Message : " + encryptedMessage);
        System.err.println("Decrypted Message : " + decryptedMessage);
        aes.exportKeys();
    } catch (Exception ignored) {}
}

We are sure now that our keys will not be going anywhere, so let’s initialize our Ciphers with those exact keys by making a method initFromStrings(String, String)

public void initFromStrings(String secretKey, String IV){
    key = new SecretKeySpec(decode(secretKey),"AES");
    this.IV = decode(IV);
}

Back again to the main method to apply the changes

private Cipher encryptionCipher;

public static void main(String[] args) {
    try {
        AES aes = new AES();
        aes.initFromStrings("CHuO1Fjd8YgJqTyapibFBQ==","e3IYYJC2hxe24/EO");
        String encryptedMessage = aes.encrypt("TheXCoders_2");
        String decryptedMessage = aes.decrypt(encryptedMessage);

        System.err.println("Encrypted Message : " + encryptedMessage);
        System.err.println("Decrypted Message : " + decryptedMessage);
        //aes.exportKeys();
    } catch (Exception ignored) {
    }
}

The result should look like this:

Yay! It works!
Now it’s time to separate the client from the server. We’ll start off by determining who will encrypt and who will decrypt, it’s basically like asking who will send the information and who will receive it.

In the video, I have worked with the Server as the sender (encrypt) and the Client as the receiver (decrypt).

The client will be:

And the Client will be:

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!