Android-MAS-SDK, KeyUtilsSymmetric.decrypt RuntimeException

49
January 10, 2019, at 4:00 PM

I have Android app with CA-MAS library.

implementation 'com.ca:mas-foundation:1.8.00'

implementation 'com.ca:mas-storage:1.8.00'

But its give runtime exception. while decrypt data its generating the exception and its very difficult to find out the exact cause. Does anyone have the same issue?

Code

public static byte[] decrypt(byte[] encryptedData, SecretKey secretKey, String key) {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_GCM_NO_PADDING);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        if (DEBUG) Log.e(TAG, "Error while getting an cipher instance", e);
        throw new RuntimeException("Error while getting an cipher instance", e);
    }
    int ivlength = IV_LENGTH;
    int macLength;
    try {
        macLength = Mac.getInstance(HMAC_SHA256).getMacLength();
    } catch (NoSuchAlgorithmException e) {
        if (DEBUG) Log.e(TAG, "Error while instantiating MAC", e);
        throw new RuntimeException("Error while instantiating MAC", e);
    }
    int encryptedDataLength = encryptedData.length - ivlength - macLength;
    byte[] macFromMessage = getArraySubset(encryptedData, 0, macLength);
    byte[] iv = getArraySubset(encryptedData, macLength, ivlength);
    encryptedData = getArraySubset(encryptedData, macLength + ivlength, encryptedDataLength);
    byte[] mac = computeMac(key, encryptedData);
    if (!Arrays.equals(mac, macFromMessage)) {
        if (DEBUG) Log.e(TAG, "MAC signature could not be verified");
        throw new RuntimeException("MAC signature could not be verified");
    }
    AlgorithmParameterSpec ivParams;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ivParams = new GCMParameterSpec(128, iv);
    } else {
        ivParams = new IvParameterSpec(iv);
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParams);
        return cipher.doFinal(encryptedData);
    } catch (Exception e) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            checkDeleteKeys(key, e);
        if (DEBUG) Log.i(TAG, "Error while decrypting an cipher instance", e);
        throw new RuntimeException(e.getMessage(), e);
    }
}

Runtime Exception

Caused by javax.crypto.AEADBadTagException
   at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:517)
   at javax.crypto.Cipher.doFinal(Cipher.java:1736)
   at com.ca.mas.core.util.KeyUtilsSymmetric.decrypt(SourceFile:584)
   at com.ca.mas.core.security.DefaultEncryptionProvider.decrypt(SourceFile:73)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:102)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:92)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:227)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:218)
   at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
   at android.os.AsyncTask$2.call(AsyncTask.java:345)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
   at java.lang.Thread.run(Thread.java:784)

Caused by java.lang.RuntimeException
   at com.ca.mas.core.util.KeyUtilsSymmetric.decrypt(SourceFile:589)
   at com.ca.mas.core.security.DefaultEncryptionProvider.decrypt(SourceFile:73)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:102)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:92)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:227)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:218)
   at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
   at android.os.AsyncTask$2.call(AsyncTask.java:345)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
   at java.lang.Thread.run(Thread.java:784)
READ ALSO
When i go back return android spinner duplicate data accure

When i go back return android spinner duplicate data accure

How to solved When i go back return android spinner duplicate data accrue , this is below my code

48
[Android][RecyclerView] Callback when row becomes visible

[Android][RecyclerView] Callback when row becomes visible

I am using Recyclerview consider it have 100 item and I want a callback when 21st item is visible on screen

24
How to parse json array inside json object? [duplicate]

How to parse json array inside json object? [duplicate]

This question already has an answer here:

27
BLE advertisement with different data packets in android

BLE advertisement with different data packets in android

I am new in Bluetooth low energy advertisementBLE supports dynamic advertisement from API level 28

34