无法解析nimbus jose jwt库中的符号“加密”

ac1kyiln  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(374)

我正在尝试使用“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();

}

我已经尝试解决这个问题好几个小时了,但还没有成功。请提出解决办法。

8gsdolmq

8gsdolmq1#

代码中有两个错误:
首先,输入错误:必须删除第3行中的结束括号。
第二,是 KeyGenerator 生成cek时,必须按如下方式初始化:

keyGenerator.init(EncryptionMethod.A128CBC_HS256.cekBitLength());

通过这些更改,代码在我的机器上工作(api28/p)。

相关问题