Create Free Digital Festival Business Images & Video Maker App Create Free Digital Festival Business Images & Video Maker App

First Things First, We should know what exactly the AES256 means

AES, Advanced Encryption Standard is a symmetric block cipher which is chosen by the U.S. government to protect classified information throughout the world to encrypt service data.

What is AES (Advanced Encryption Standard) Encryption?

You may have heard of advanced encryption standard, or AES for short but may not know the answer to the question “what is AES?” Here are four things you need to know about AES:

  • What’s behind the name? AES is a variant of the Rijndael family of symmetric block cipher algorithms, which is a combination of two Belgian cryptographers’ names, Joan Daemen and Vincent Rijmen. (It’s sort of like those combo celebrity names like “Brangelina” and “Kimye”).
  • Where is AES used? Android AES encryption is commonly used in a lot of ways, including wireless security, processor security, file encryption, and SSL/TLS. In fact, your web browser probably used AES to encrypt your connection with this website.
  • Why is AES so popular? AES has proved to be very effective and efficient and, given the correct key, adds little to no-noticeable difference in overhead for any process where it is utilized. Essentially, AES is a fast and highly secure form of encryption that is a favorite of businesses and governments worldwide.
  • How secure is AES? With a 256-bit encryption key, AES is very secure — virtually unbreakable. (More on that in just minute.)

What is the best algorithm for encryption?

The Advanced Encryption Standard (AES) is the trusted standard algorithm used by the United States government, as well as other organizations. Although extremely efficient in the 128-bit form, AES also uses 192- and 256-bit keys for very demanding encryption purposes.Dec

Advanced Encryption Standard is built from three block ciphers :

  • AES-128
  • AES-192
  • AES-256

All symmetric encryption ciphers use the same key for encryption and decryption. Which means in the sense, both the sender and receiver must have the same key. Each encrypts and decrypts have 128 bits data in chunks by using cryptographic keys 128-, 192- or 256- bits.

Plain text which we use to convert are first converted into Cipher and then outputs into Cipher text bits of 128, 192 & 256.

It’s time for code:

We need to initialize KeyGenerator and SecretKey in order to make our own Key.

KeyGenerator keyGenerator;
SecretKey secretKey;

Prevent a parameter with getInstance method and initialize the KeyGenerator with keysize 256. Add them in the try catch method to prevent crashes.

try {
   keyGenerator = KeyGenerator.getInstance("AES");
   keyGenerator.init(256);
   secretKey = keyGenerator.generateKey();
} catch (Exception e) {
   e.printStackTrace();
}

Now I’m using IV(Initialization Vector) in my code. This is an optional part in AES Encryption but a way better to use. Use it with SecureRandom class.

byte[] IV = new byte[16];
SecureRandom random;

Initialize the IV after declaring it globally.

random = new SecureRandom();
random.nextBytes(IV);

Encryption Method:

public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception {
   Cipher cipher = Cipher.getInstance("AES");
   SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
   IvParameterSpec ivSpec = new IvParameterSpec(IV);
   cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
   byte[] cipherText = cipher.doFinal(plaintext);
   return cipherText;
}

Decryption Method:

public static String decrypt(byte[] cipherText, SecretKey key, byte[] IV) {
   try {
       Cipher cipher = Cipher.getInstance("AES");
       SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
       IvParameterSpec ivSpec = new IvParameterSpec(IV);
       cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
       byte[] decryptedText = cipher.doFinal(cipherText);
       return new String(decryptedText);
   } catch (Exception e) {
       e.printStackTrace();
   }
   return null;
}

How to call both the methods:

I’ve used an EditText for the user text field to type whatever the string which needs to be encrypted. Two buttons for getting results respectively. One for Encrypting and other for Decrypting. Also used two TextViews for displaying the values(results). One text is used for displaying encrypted value and the other one for decrypted value.

btn1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       try {
           encrypt = encrypt(et.getText().toString().getBytes(), secretKey, IV);
           String encryptText = new String(encrypt, "UTF-8");
           tv1.setText(encryptText);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
});

btn2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       try {
           String decrypt = decrypt(encrypt, secretKey, IV);
           tv2.setText(decrypt);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
});

After placing these all you can now see the results of how Encrypt and Decrypt are done through AES 256 Encryption.

Remember that IV(Initialization Vector) is an arbitrary number that is only used along with a secret key for data encryption. To generate the IV we use the SecureRandom class.

From the Initialization vector, We create an IvParameterSpec which is always required when creating the cipher during encryption or decryption.

You can see the output in the below example: