当前位置: 首页 > 知识库问答 >
问题:

JoseException:无法创建AES/GCM/Nopadding密码:非法密钥大小

闾丘文昌
2023-03-14

我正在尝试使用Nimbus JOSE+JWT从我的私钥解密JWE数据。

有人能帮我解决这个问题吗。我使用的是Java1.8

我的私钥

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDGBRdsiDqKPGyH
gOpzxmSU2EQkm+zYZLvlPlwkwyfFWLndFLZ3saxJS+LIixsFhunrrUT9ZZ0x+bB6
MV55o70z4ABOJRFNWx1wbMGqdiC0Fyfpwad3iYpRVjZO+5etHA9JEoaTPoFxv+kt
d8kVAL9P5I7/Pi6g1R+B2t2lsaE2bMSwtZqgs55gb7fsCR3Z4nQi7BddYR7MZ2lA
MWf7h7Dkm6uRlGhl2RvtmYa6dXFnK3RhIpdQOUT3quyhweMGspowC/tYSG+BNhy1
WukbwhIP5vTAvv1WbHTg+WaUUV+pP0TjPQcY73clHxNpI5zrNqDmwD2rogNfePKR
UI63yBUfAgMBAAECggEAGy/7xVT25J/jLr+OcRLeIGmJAZW+8P7zpUfoksuQnFHQ
QwBjBRAJ3Y5jtrESprGdUFRb0oavDHuBtWUt2XmXspWgtRn1xC8sXZExDdxmJRPA
0SFbgtgJe51gm3uDmarullPK0lCUqS92Ll3x58ZQfgGdeIHrGP3p84Q/Rk6bGcOb
cPhDYWSOYKm4i2DPM01bnZG2z4BcrWSseOmeWUxqZcMlGz9GAyepUU/EoqRIHxw/
2Y+TGus1JSy5DdhPE0HAEWKZH729ZdoyikOZCMxApQglUkRwkwhtXzVAemm6OSoy
3BEWvSEJh/F82tFrmquUoe/xd5JastlBHyD78RAakQKBgQDkHAzo1fowRI19tk7V
CPn0zMdF/UTRghtLywc/4xnw1Nd13m+orArOdVzPlQokLVNL81dIVKXnId0Hw/kX
8CRyRYz8tkL81spc39DfalZW7QI7Fschfq1Htgkxd/QEjBlIaqjkOjGSbX9xYjYU
1Db8PuGoG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zvLBLGOT-wQzvDeCF51tQC6hs4Tha3tnrR4wni840YM0t9SCgvbyI3UVFE6JSD4S8w5YgsaDI8valG1KHrtIiIbeQErBn1Hj-J3hnfkHlcILO4JK4nWxHwuGdgYrGeyvz0p7YbVLTC-frwCFBflJlU_6HbjnG7-vMv07rViG0gQZGRw27PXAyYB6lyhXbScoX-GbL5k7oFWmsJG-xKIF9H4HPSQ1KzMTJxHNtzutM0XeB19YJt9hly4-2LPI9qgkupKG20eZbdP5n0Xyr1IOXVVNUMa3wquNmDRgPevUkIXJKdF5pjjetVFFJT4nuADKSRFKfo5ABYK1pVTBgwN1t4a3jhW6fkUejN9IosotVcbuDtQT468RGs04T5lAdFLrNs7-85kHeAbyx38e6lm_CvRnDUt_bTaA_j50L60dI3vuypWLwh5qOiyteysZvlN0uGKzrFV9NvjHnR3tcX1CTQIguZzZkNxBHtCL2Yn9zufoJ4h_QEUUNkCsTj1g74WxfrK16M_MRI5VjV8sS5PSRXdklsHQcfUqJQKjSeFZM0kXSazVLGiGaBOV3UYYI-Q2v9EBNFgSYdrGA3qLaaNfBtVQzl42skvvnsybp0UDCkLYPmBA1OrSbZKkaCHqtXMq126jF4zje78cjdIF7ive_wRcqCC7HgtgWepD_LBVWjVqwRmGHJoWFIjROc0d0_2FxAHyxHoFTr1EOWlPIRx6C-lvSvkhBlq8VyZleOgZy1Q5j-3-C8JITcyedv11j5XH-wKCNDDlogPyk_N7JvYYEVHw.aLJfWNBdLWNz1GbTtKNGvg
public static void main(String[] args) throws Exception {
   String jweString = "above JWE data";
   JWEObject jwe = JWEObject.parse(jweString);
   jwe.decrypt(new RSADecrypter(rsaPrivateKey));
   System.out.println("Decrypted : " + jwe.getPayload().toString());
}
private static RSAPrivateKey getPrivateKeyFromString(String key) throws GeneralSecurityException {
    String privateKeyPEM = key;
    privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
    privateKeyPEM = privateKeyPEM.replace("-----END PRIVATE KEY-----", "");
    byte[] encoded = Base64.decodeBase64(privateKeyPEM);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
    RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
    return privKey;
}
<dependency>
    <groupId>com.nimbusds</groupId>
    <artifactId>nimbus-jose-jwt</artifactId>
    <version>6.0</version>
</dependency>

共有1个答案

濮阳国兴
2023-03-14

添加EncryptionMethod.a256GCM为我创建了以下异常,原因是:com.nimbusds.jose.joseException:无法创建aes/gcm/nopadding cipher:非法密钥大小

解决方案是--如果jdk8u162或以上的java版本可以按原样工作。如果使用较低的jdk版本,则下载最新的策略文件[jce_policy-8.zip]并将其替换到%java_home%/jre/lib/security path中。[local_policy.jar,us_export_policy.jar]

 类似资料:
  • 也许你能帮我。非常感谢!

  • 节点模块: Java类:主要方法现在只是用于测试,稍后将被删除。

  • 问题内容: 我的测试可以在开发的MacBook Pro上很好地运行,但是无法在持续集成的TeamCity服务器中运行。 错误如下: java.security.InvalidKeyException: Illegal key size at javax.crypto.Cipher.a(DashoA13..) at javax.crypto.Cipher.init(DashoA13 ..) at j

  • 我已经使用OpenSSL AES-256-GCM加密了一个文件。由于aes-256-gcm不受命令行支持,我已经安装了LibreSSL,我可以使用下面的命令加密文件的数据。 openssl ENC-AES-256-GCM-K 616161616161616161616161616161616161616161616161616161616161616161-IV 768A5C31A97D5FE9-

  • 我目前正在用java编写一个加密消息传递服务,我使用的是bouncycastle PGP库。我编写了一个生成密钥对的测试程序,并对消息进行加密/解密。这已经工作了一段时间,但它最近在解密阶段停止了,给了我一个InvalidKeyException。 我做了一些研究,下载了JCE.jar文件,并将它们导入到我的项目中(通过Eclipse Project->Properties->add extern