I'm trying to implement Play Integrity API in my project. I already implement the logic in an Android app and send the token to my server. I want to decrypt and verify locally because the server don't and won't have internet connection. So I'm following the official guide, but I'm struggling when getting the payload of the JWE. This is what I tried:
String playIntegrityToken = "eyJhbGciOiJBM...";
String base64OfEncodedDecryptionKey = "...";
byte[] decryptionBytes = Base64.decode(base64OfEncodedDecryptionKey);
SecretKey secretKey = new SecretKeySpec(decryptionBytes, 0, decryptionBytes.length, "AES");
JsonWebEncryption jwe = (JsonWebEncryption) JsonWebStructure.fromCompactSerialization(playIntegrityToken);
jwe.setKey(secretKey);
System.out.println(jwe.getPayload());
But I getting the error:
Exception in thread "main" org.jose4j.lang.JoseException: javax.crypto.AEADBadTagException: Tag mismatch!
at org.jose4j.jwe.SimpleAeadCipher.decrypt(SimpleAeadCipher.java:114)
at org.jose4j.jwe.SimpleAeadCipher.decrypt(SimpleAeadCipher.java:101)
at org.jose4j.jwe.AesGcmContentEncryptionAlgorithm.decrypt(AesGcmContentEncryptionAlgorithm.java:79)
at org.jose4j.jwe.JsonWebEncryption.decrypt(JsonWebEncryption.java:249)
at org.jose4j.jwe.JsonWebEncryption.getPlaintextBytes(JsonWebEncryption.java:85)
at org.jose4j.jwe.JsonWebEncryption.getPlaintextString(JsonWebEncryption.java:78)
at org.jose4j.jwe.JsonWebEncryption.getPayload(JsonWebEncryption.java:93)
at Main.main(Main.java:17)
Caused by: javax.crypto.AEADBadTagException: Tag mismatch!
at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:620)
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1116)
at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2168)
at org.jose4j.jwe.SimpleAeadCipher.decrypt(SimpleAeadCipher.java:110)
... 7 more
The library I'm using is Jose4j in its 0.8.0 version.
I also tried changing the library that handle the JWE, for Nimbus JOSE + JWT
, the library that decode the decryption key, the SecretKeySpec
, the length, the algoritm , but nothing works, What am I doing wrong?