3

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)
1
  • 2
    ce.getIV() Commented Nov 30, 2015 at 10:07

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Why is 128 the only length?
128 length because, GCM specification states that tLen may only have the values {128, 120, 112, 104, 96}, or {64, 32} for certain applications. developer.android.com/reference/javax/crypto/spec/…
0

This is tagsize calculation code that I used for AES/GCM mode. I don't prefer static usage.

byte[] pinBytes = pin.getBytes();
byte[] bytes = cipher.doFinal(pinBytes);
tagLenght= bytes.length - pin.length;

2 Comments

Where is your cipher.init?
What does pin refer to here? I'm interested in how you calculate tagLength but can't tell from this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.