I am trying to generate an encrypted JWT in Android using 'nimbus-jose-jwt' library. But when I call the method 'encrypt()
' of library, I get the error 'Cannot resolve symbol 'encrypt'
, even though the source code of library has the method 'encrypt()' for the object I specified.
This is my code:
public class EncryptedJWTGenerator {
public EncryptedJWTGenerator() throws NoSuchAlgorithmException, JOSEException {
}
JWEAlgorithm alg = JWEAlgorithm.RSA_OAEP_256;
EncryptionMethod enc = EncryptionMethod.A128CBC_HS256;
KeyPairGenerator rsaGen = KeyPairGenerator.getInstance("RSA");
//rsaGen.initialize(2048);
KeyPair rsaKeyPair = rsaGen.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey)rsaKeyPair.getPublic();
// Generate the preset Content Encryption (CEK) key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecretKey cek = keyGenerator.generateKey();
// Encrypt the JWE with the RSA public key + specified AES CEK
JWEObject jweObject = new JWEObject(new JWEHeader(alg, enc), new Payload("Hello, world!"));
jweObject.encrypt(new RSAEncrypter(rsaPublicKey, cek)); //**ERROR IN THIS LINE**
String jweString = jweObject.serialize();
}
I have been trying to resolve this for hours, but not successful yet. Please suggest a solution.
There are two bugs in the code:
First, a typo: the closing bracket in the 3rd line must be removed.
Secondly, the KeyGenerator
must be initialized as follows when generating the CEK:
keyGenerator.init(EncryptionMethod.A128CBC_HS256.cekBitLength());
With these changes the code works on my machine (API28/P).