Search code examples
javaandroidfirebaseandroid-studioone-time-password

android.security.KeyStoreException: Signature/MAC verification failed


I have getting the error when i try to register to the application. when i enter my phone number and register it goes to otp verification but did not get the otp. But it give me an error saying problem to get safety net token:7. Please help me through it. It is working before but now getting error.

I have add sha-1 and sha-256 as well in firebase setting. I have added the google-service.jsons file also.

Error:

javax.crypto.AEADBadTagException
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:51 
7)
    at javax.crypto.Cipher.doFinal(Cipher.java:2113)
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzd(com.google.firebase:firebase-auth@@20.0.4:7)
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzb(com.google.firebase:firebase-auth@@20.0.4:1)
    at com.google.android.gms.internal.firebase-auth-api.zzau.zzi(com.google.firebase:firebase-auth@@20.0.4:5)
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzi(com.google.firebase:firebase-auth@@20.0.4:1)
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzd(com.google.firebase:firebase-auth@@20.0.4:2)
    at com.google.firebase.auth.internal.zzk.<init>(com.google.firebase:firebase-auth@@20.0.4:7)
    at com.google.firebase.auth.internal.zzk.zza(com.google.firebase:firebase-auth@@20.0.4:3)
    at com.google.firebase.auth.internal.RecaptchaActivity.zzd(com.google.firebase:firebase-auth@@20.0.4:9)
    at com.google.android.gms.internal.firebase-auth-api.zztw.<init>(com.google.firebase:firebase-auth@@20.0.4:13)
    at com.google.firebase.auth.internal.RecaptchaActivity.onResume(com.google.firebase:firebase-auth@@20.0.4:43)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1465)
    at android.app.Activity.performResume(Activity.java:8223)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4779)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4832)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:190)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:105)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
 Caused by: android.security.KeyStoreException: Signature/MAC verification failed
    at android.security.KeyStore.getKeyStoreException(KeyStore.java:1316)
    at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
    at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$BufferAllOutputUntilDoFinalStreamer.doFinal(AndroidKeyStoreAuthenticatedAESCipherSpi.java:373)
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
    at javax.crypto.Cipher.doFinal(Cipher.java:2113) 
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzd(com.google.firebase:firebase-auth@@20.0.4:7) 
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzb(com.google.firebase:firebase-auth@@20.0.4:1) 
    at com.google.android.gms.internal.firebase-auth-api.zzau.zzi(com.google.firebase:firebase-auth@@20.0.4:5) 
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzi(com.google.firebase:firebase-auth@@20.0.4:1) 
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzd(com.google.firebase:firebase-auth@@20.0.4:2) 
    at com.google.firebase.auth.internal.zzk.<init>(com.google.firebase:firebase-auth@@20.0.4:7) 
    at com.google.firebase.auth.internal.zzk.zza(com.google.firebase:firebase-auth@@20.0.4:3) 
    at com.google.firebase.auth.internal.RecaptchaActivity.zzd(com.google.firebase:firebase-auth@@20.0.4:9) 
    at com.google.android.gms.internal.firebase-auth-api.zztw.<init>(com.google.firebase:firebase-auth@@20.0.4:13) 
    at com.google.firebase.auth.internal.RecaptchaActivity.onResume(com.google.firebase:firebase-auth@@20.0.4:43) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1465) 
    at android.app.Activity.performResume(Activity.java:8223) 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4779) 
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4832) 
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:190) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:105) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:213) 
    at android.app.ActivityThread.main(ActivityThread.java:8178) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
W/zzds: cannot decrypt keyset: 
javax.crypto.AEADBadTagException
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:517)
    at javax.crypto.Cipher.doFinal(Cipher.java:2113)
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzd(com.google.firebase:firebase-auth@@20.0.4:7)
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzb(com.google.firebase:firebase-auth@@20.0.4:4)
    at com.google.android.gms.internal.firebase-auth-api.zzau.zzi(com.google.firebase:firebase-auth@@20.0.4:5)
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzi(com.google.firebase:firebase-auth@@20.0.4:1)
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzd(com.google.firebase:firebase-auth@@20.0.4:2)
    at com.google.firebase.auth.internal.zzk.<init>(com.google.firebase:firebase-auth@@20.0.4:7)
    at com.google.firebase.auth.internal.zzk.zza(com.google.firebase:firebase-auth@@20.0.4:3)
    at com.google.firebase.auth.internal.RecaptchaActivity.zzd(com.google.firebase:firebase-auth@@20.0.4:9)
    at com.google.android.gms.internal.firebase-auth-api.zztw.<init>(com.google.firebase:firebase-auth@@20.0.4:13)
    at com.google.firebase.auth.internal.RecaptchaActivity.onResume(com.google.firebase:firebase-auth@@20.0.4:43)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1465)
    at android.app.Activity.performResume(Activity.java:8223)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4779)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4832)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:190)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:105)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
 Caused by: android.security.KeyStoreException: Signature/MAC verification failed
    at android.security.KeyStore.getKeyStoreException(KeyStore.java:1316)
    at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
    at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$BufferAllOutputUntilDoFinalStreamer.doFinal(AndroidKeyStoreAuthenticatedAESCipherSpi.java:373)
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
    at javax.crypto.Cipher.doFinal(Cipher.java:2113) 
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzd(com.google.firebase:firebase-auth@@20.0.4:7) 
    at com.google.android.gms.internal.firebase-auth-api.zzdt.zzb(com.google.firebase:firebase-auth@@20.0.4:4) 
    at com.google.android.gms.internal.firebase-auth-api.zzau.zzi(com.google.firebase:firebase-auth@@20.0.4:5) 
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzi(com.google.firebase:firebase-auth@@20.0.4:1) 
    at com.google.android.gms.internal.firebase-auth-api.zzdr.zzd(com.google.firebase:firebase-auth@@20.0.4:2) 
    at com.google.firebase.auth.internal.zzk.<init>(com.google.firebase:firebase-auth@@20.0.4:7) 
    at com.google.firebase.auth.internal.zzk.zza(com.google.firebase:firebase-auth@@20.0.4:3) 
    at com.google.firebase.auth.internal.RecaptchaActivity.zzd(com.google.firebase:firebase-auth@@20.0.4:9) 
    at com.google.android.gms.internal.firebase-auth-api.zztw.<init>(com.google.firebase:firebase-auth@@20.0.4:13) 
    at com.google.firebase.auth.internal.RecaptchaActivity.onResume(com.google.firebase:firebase-auth@@20.0.4:43) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1465) 
    at android.app.Activity.performResume(Activity.java:8223) 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4779) 
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4832) 
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:190) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:105) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:213) 
    at android.app.ActivityThread.main(ActivityThread.java:8178) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
E/FirebearCryptoHelper: Exception encountered during crypto setup:
Protocol message contained an invalid tag (zero).
KeysetManager failed to initialize - unable to get Public key
E/RecaptchaActivity: Could not generate an encryption key for reCAPTCHA - cancelling flow.
W/System: Ignoring header X-Firebase-Locale because its value was null.
E/zzf: Failed to get reCAPTCHA token with error [An internal error has 
occurred. [ Failed to generate/retrieve public encryption key for reCAPTCHA 
flow. ]]- calling backend without app verification

Solution

  • well I have hardly go through it. Finally I understand the error that says mac verification failed. It is because of, we need to enable android device verification. To do so, i have followed following steps.

    1. go to https://console.cloud.google.com/

    2. Type your firebase project name-> select your project-> your project pop up and click

    3. click API and service(left navigation bar)

    4. select library->scroll down and click mobile-> click android device verification->enable api

    5. clean and rebuild your app-> run-> works perfectly.

    I have added dependency below:

    implementation 'androidx.security:security-crypto:1.1.0-alpha02'
    implementation "androidx.browser:browser:1.2.0"
    

    Now its work perfectly fine for me. It may help others also.