Android tutorial

How to used 256 AES Encryption and Decryption on Android

AES Encryption and Decryption on Android
243views

How to perform AES Encryption and Decryption on Android. First things first, we need to know what AES256 is. AES, Advanced Encryption Standard is an symmetric block cipher that is used by the U.S. government to protect classified information around the globe to protect information used for service.

What exactly is AES (Advanced encryption standard) encryption?

It is possible that you have heard about advanced encryption standard or AES, but you may not be aware of your”what is AES? “what does AES mean?” Here are four points you should be aware of regarding AES:

  • What’s the reason behind its title? AES is a variation that belongs to the Rijndael family of symmetric block ciphers that is a mix of two Belgian cryptographers’ names: Joan Daemen and Vincent Rijmen. (It’s kind of similar to those combinations of celebrity names, like “Brangelina” or “Kimye”).
  • Where is AES utilized?  Android AES encryption is used extensively in many ways, such as processing security via wireless, wireless SSL/TLS encryption of files. In reality, your web browser may have used AES to secure your connection to this site.
  • What makes AES well-known? AES has proved to be extremely efficient and efficient. It, when used with the right key, creates minimal to no costs for any system in which it is employed. In the end, AES is a fast and secure method of encryption, which is popular with government agencies and businesses around the world.
  • Is AES secure? With a 256-bit encryption key, AES is extremely secure, virtually invulnerable. (More on that in less than a minutes.)

What is the most efficient algorithm to use for aes encryption?

It is the Advanced Encryption Standard (AES) is the most trusted algorithm that is utilized in government agencies like the United States government, as and other companies. Although it is extremely effective in the 128-bit format, AES also uses 192and 256-bit keys to meet extremely complex encryption purposes.Dec

Advanced AES Encryption Standard is built out of three block ciphers:

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

All symmetric encryption algorithms use the same encryption key for both encryption and decryption. This means that in the sense that both the sender and the receiver have to possess one key. Each decrypts and encrypts 128 bits of data, in chunks employing cryptographic keys of 128 or 192- bits.

Plain text that we convert is first transformed to Cipher and then outputs to Cipher chunks of text, such as 128, 192, and 248.

It’s time to codes:

We will need to set up KeyGenerator as well as SecretKey for us to create the own Key.

KeyGenerator keyGenerator
SecretKey secretKey;

Make sure that a parameter is not triggered by using the getInstance method and set the KeyGenerator with a keysize of at 256. Include them in the catch method to avoid crashes.

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

In the present, I’m making use of IV(Initialization Vector) in my code. This is an optional feature of AES Encryption however it is a better option to utilize. Make use of it in conjunction with the SecureRandom class.

Byte[ IV=new byte 16 SecureRandom random;

Initialize the IV following declaration of it global.

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

AES 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;
}

AES 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;
}

What is the best way to call both methods:

I’ve utilized an EditText in the text field for users to input whatever string that is encryption. Two buttons are available to obtain results. One is for Encrypting, and another to decrypt. Also, we used two TextViews for display of results. values(results). One TextView is used to display encrypted value , and the second one displays decrypted value.

After putting these all in, together, you will observe the results of how encryption and decryption are accomplished using AES 256 encryption.

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();
       }
   }
});

Keep in mind this IV(Initialization Vector) is an undefined number that is used only together with a secret key to secure data. In order to generate the IV, we employ SecureRandom. SecureRandom class.

In the vector of Initialization we create an IvParameterSpec which is mandatory in the creation of the cipher encryption and decryption.

 

AES encryption algorithm full Class Souce.

import android.util.Base64;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESCrypt
{
    private static final String ALGORITHM = "AES";
    private static final String KEY = "1Hbfh667adfDEJ78";




    public static String encrypt(String value) throws Exception
    {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte [] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8"));
        String encryptedValue64 = Base64.encodeToString(encryptedByteValue, Base64.DEFAULT);
        return encryptedValue64;

    }

    public static String decrypt(String value) throws Exception
    {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT);
        byte [] decryptedByteValue = cipher.doFinal(decryptedValue64);
        String decryptedValue = new String(decryptedByteValue,"utf-8");
        return decryptedValue;

    }

    private static Key generateKey() throws Exception
    {
        Key key = new SecretKeySpec(AESCrypt.KEY.getBytes(),AESCrypt.ALGORITHM);
        return key;
    }
}


Read More Tutorial