Search code examples
androidencryptionandroid-keystoreaes-gcm

Android : AES Encryption & Decryption using GCM mode in android?


I'm trying to Encrypt & Decrypt the String using AES Algorithm & GCM mode.

My code is able to encrypt the string but i'm not able to decrypt the encoded data.

Steps followed :

  • Create Key()
  • Encrypt(string)
  • Decrypt(encded_data);

My Code :

Create Key

public void createKey() {
    try {
        if (!ks.containsAlias(keyName)) {

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                Log.e("MAinAcvtivity", "Current version is 23(MashMello)");
                //Api level 23
                KeyGenerator generator  = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
                generator.init(
                        new KeyGenParameterSpec.Builder(keyName,
                                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                                .build());
                SecretKey key = generator.generateKey();

            } else {
                Log.e("MAinAcvtivity", "Current version is < 23(MashMello)");

            }

        }else{
            Log.e("MAinAcvtivity", "Key exist");
        }
    } catch (Exception e) {

        Log.e("MAinAcvtivity", "Key didn't generated");
        Log.e("MAinAcvtivity", Log.getStackTraceString(e));
    }
}

Encryption :

public String doEncryption(String data) {
    try {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            ce = Cipher.getInstance("AES/GCM/NoPadding");
            sKey = (SecretKey) ks.getKey(keyName, null);
            ce.init(Cipher.ENCRYPT_MODE, sKey);
        } else {

        }

        encodedData = ce.doFinal(data.getBytes());
        mEncodedData = Base64.encodeToString(encodedData, Base64.DEFAULT);
    } catch (Exception e) {
        Log.e("Main", "RSA Encription Error.!");
        Log.e("MainActivity", "RSA Decryption Error.!", e);
    }
    Log.e("Main", "encripted DATA =" + mEncodedData);
    return mEncodedData;
}

Decryption :

public String doDecryption(String eData) {
    String decryptedText = null;
encodedData = Base64.decode(eData, Base64.DEFAULT);

    try {
        Cipher c;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Log.i("MainActivity","in Decryption version m");

            c = Cipher.getInstance("AES/GCM/NoPadding");
            sKey = (SecretKey) ks.getKey(keyName, null);
            Log.e("MainActivity", "After getting key : " );
            c.init(Cipher.DECRYPT_MODE, sKey);
        } else {

        }

        decodedData = c.doFinal(encodedData);
        decryptedText = new String(decodedData, "UTF-8");
        Log.e("MainActivity", "After decryption : "+ decryptedText);
    } catch (Exception e) {
        Log.e("MainActivity", "RSA Decryption Error.!", e);
    }
    return decryptedText;
}

Error Log :

MAinAcvtivity: Key exist
Main: encripted DATA =KrHmMXhcytb0owDzLaMY2wsQmwY=
MainActivity: in decryption : encoded data =KrHmMXhcytb0owDzLaMY2wsQmwY=
MainActivity: After getting key : 
MainActivity: RSA Decryption Error.!
MainActivity: java.security.InvalidKeyException: IV required when     decrypting. Use IvParameterSpec or AlgorithmParameters to provide it.
MainActivity:     at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$GCM.initAlgorithmSpecificParameters(AndroidKeyStoreAuthenticatedAESCipherSpi.java:79)
MainActivity:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:106)

Solution

  • Bingooo!

    I got the solution.

    In decryption i made this changes :

     GCMParameterSpec spec = new GCMParameterSpec(128,ce.getIV());
     c.init(Cipher.DECRYPT_MODE, sKey,spec);