android - 无法解析 nimbus-jose-jwt 库中的符号“加密”
问题描述
我正在尝试使用“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();
}
我一直试图解决这个问题几个小时,但还没有成功。请提出解决方案。
解决方案
代码中有两个错误:
首先,一个错字:必须删除第 3 行中的右括号。
其次,
KeyGenerator
在生成CEK时必须进行如下初始化:keyGenerator.init(EncryptionMethod.A128CBC_HS256.cekBitLength());
通过这些更改,代码可以在我的机器上运行(API28/P)。
推荐阅读
- javascript - 为什么在 jQuery 单击事件触发我的 es6 类函数之一后我的其他类函数和变量变得未定义
- linux - ln(create hardlink) 命令在 AIX 上是原子的吗?
- angular - 动态表单的动态函数
- excel - Excel:如何从工作表 B 中获取价格,其中 product_id 与工作表 A 中的 product_id 相同
- if-statement - Excel:带条件的列公式
- c# - getKeyDown 按键不再起作用,以前起作用。调试也没有输出
- swift - 使用 NSPersistentCloudKitContainer 时记录唯一性?
- postgresql - 在 Postgres 中循环存储过程中的数据
- google-drive-api - 我在哪里可以看到更多的 api 配额价格?
- python - Python中的条件滚动总和