【发布时间】:2021-10-02 07:38:29
【问题描述】:
我正在尝试使用“nimbus-jose-jwt”库在 Android 中生成加密的 JWT。但是当我调用库的方法'encrypt()'时,我得到错误'Cannot resolve symbol 'encrypt',即使库的源代码有我指定的对象的方法'encrypt()'。
这是我的代码:
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();
}
我已经尝试解决这个问题好几个小时了,但还没有成功。请提出解决方案。
【问题讨论】:
-
在删除第三行错误的右括号后尝试(这个错字会在我的机器上产生发布的错误消息)。
-
但是,生成 CEK 时必须将
KeyGenerator初始化为EncryptionMethod.A128CBC_HS256.cekBitLength()。 -
Topaco,抱歉回复晚了。现在我得到:com.nimbusds.jose.JOSEException:ecrypt() 方法调用行中 RSA 块的数据过多。
-
我无法使用发布的代码和我建议的更改 (API28/P) 在我的机器上重现此内容。
-
对不起,现在一切正常。我已经初始化了 KeyPairGenerator 而不是 KeyGenerator。请发布答案,以便我将其标记为正确。
标签: android encryption jwt jwe nimbus-jose-jwt