1. Are you ready for the Galaxy S20? Here is everything we know so far!

AEADBadTagException Caused by KeyStoreException: Signature/MAC verification failed

Discussion in 'Android Development' started by LubaArbel, Aug 3, 2020.

  1. LubaArbel

    LubaArbel Lurker
    Thread Starter

    I use AES key with this code to encrypt:

    private EncryptionResult encrypt(SecretKey secretKey, String message) throws Throwable {
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));

    // save in an object encoded with iv
    EncryptionResult result = new EncryptionResult(Base64.encodeToString(cipherText, Base64.NO_WRAP), cipher.getIV());
    return result;
    }


    And I use this code to decrypt:

    private String decrypt(SecretKey secretKey, EncryptionResult encryptionResult) throws Throwable {
    byte[] salt = encryptionResult.getIV();
    GCMParameterSpec gCMParameterSpec = new GCMParameterSpec(128, salt);

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, gCMParameterSpec);
    byte[] decodeBase64 = Base64.decode(encryptionResult.getBase64EncryptedValue(), Base64.NO_WRAP);
    byte[] decryptedValue = cipher.doFinal(decodeBase64);
    return new String(decryptedValue);
    }


    The "message" that is being encrypted is a RSA key size 2048.
    It works on MOST of the popular devices, but the decryption fails on some others like Meizu (M5, M3E), Huawei (P40, Nova 5T), Samsungs (J7, A10), etc... where it crashes do to "AEADBadTagException... Caused by KeyStoreException: Signature/MAC verification failed".
    If I reduce the "message" to 1024, it also works fine (although as far as I know, AES key has no size limitation for encryption).
    I also tried using CipherOutputStream/CipherInputStream, but got the same result.
    All looks good with Base64 encoding/decoding, tags, iv, keyStore... any other components envolved in the process.

    Does anyone can shed some light why some devices do the work, while others fail?
     


    goldensoju likes this.

    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - AEADBadTagException Caused KeyStoreException
  1. zelaine2
    Replies:
    5
    Views:
    381
  2. maurellius
    Replies:
    4
    Views:
    451
  3. jdwheli
    Replies:
    2
    Views:
    784
  4. Cao Van Thanh
    Replies:
    2
    Views:
    717
  5. App Update
    Replies:
    0
    Views:
    396
  6. Silvarro
    Replies:
    5
    Views:
    726
  7. archz2
    Replies:
    4
    Views:
    4,288
  8. Karyn219
    Replies:
    13
    Views:
    792
  9. kayakkielbasa
    Replies:
    3
    Views:
    658
  10. Brizky
    Replies:
    0
    Views:
    540

Share This Page

Loading...