aes

Hi guys today I’m gonna teach you about AES-Encryption. This encryption method is very useful for protecting your data against any intrusion.Even if intruders may gain access on your data, they still can understand them because they are all encrypted.

I will provide you the source code of a simple application I created with JavaFX, which will help you encrypt/decrypt your data.

So first you are going to need a key and an init vector. Let’s suppose the key and the init vector have the following values :

String key = "Jar12345Jar12345";
String initVector = "RandomInitVector";

 

Then you need 2 methods :

1-the encrypt method

public static String encrypt(String key, String initVector, String value)
  {
    try {
      IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
      SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
      
      Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
      cipher.init(1, skeySpec, iv);
      
      byte[] encrypted = cipher.doFinal(value.getBytes());
      
      return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    
    return null;
  }

 

2-the decrypt method

public static String decrypt(String key, String initVector, String encrypted) {
     try {
       IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
       SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
       
       Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
       cipher.init(2, skeySpec, iv);
       
       byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
       
       return new String(original);
     } catch (Exception ex) {
       Alert a = new Alert(AlertType.ERROR, "Input length must be multiple of 16", 
                        ButtonType.CANCEL);
       a.show();
     }
     
     return null;
   }

 

If you want to know more about AES here is a link : https://thebestvpn.com/advanced-encryption-standard-aes/

There 2 methods are the “key” methods in our program. I wont go into details of how the application is build in JavaFX.Here is a photo of the program.

aes

Below is the source code.

AES Encryption

Enjoy, thank you.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here