JOSEException:无法创建 AES/GCM/NoPadding 密码:非法密钥大小
Posted
技术标签:
【中文标题】JOSEException:无法创建 AES/GCM/NoPadding 密码:非法密钥大小【英文标题】:JOSEException: Couldn't create AES/GCM/NoPadding cipher: Illegal key size 【发布时间】:2019-02-20 00:11:10 【问题描述】:我正在尝试使用 Nimbus JOSE + JWT 从我的私钥中解密 JWE 数据。
但我遇到了错误:JOSEException: 无法创建 AES/GCM/NoPadding 密码:非法密钥大小
谁能帮我解决这个问题。我正在使用 Java 1.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/xd5JastlBHyD78RAakQKBgQDkHAzo1fowRI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2g4iY8OZas2MHhZiToZGgewWxq3Yc7KpvRzTM-edwmfeh-UqGVdeZbc68xrh5X3gdkCkH0dSPdZA9eA8HVlQu1M2TOO90Rs7HBi1HxlMsHQZvPpJ4fM21MgyzNPq19O5MwQMN0xfl1tB7zvLBLGOT-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());
获取RSA私钥的代码
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;
Maven 依赖
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>6.0</version>
</dependency>
【问题讨论】:
我可以通过更新 Java 8:JCE Unlimited Strength Jurisdiction Policy Files 来完成这项工作。有没有办法在不接触我的 JDK/JRE 的情况下完成这项工作。可能使用不同的库而不是 Nimbus? 如果您使用 Oracle Java 8u161 或更高版本,它现在内置了无限策略,无需更改;如果您的平台可用,任何版本的 OpenJDK 都一样。否则,您可以通过更改库以使用 BouncyCastle 的“轻量级”API(不仅仅是 Bouncy provider)来避免 JCA 策略限制,或者编写一个这样做的替代品,但其中任何一个都会相当多工作的。 PS:标签 nimbus 是完全不同的东西,对你的 Q 来说是错误的。 【参考方案1】:添加 EncryptionMethod.A256GCM 为我创建了以下异常, 原因:com.nimbusds.jose.JOSEException:无法创建 AES/GCM/NoPadding 密码:非法密钥大小
解决方案是-如果jdk8u162或以上java版本可以正常工作。如果使用较低的 jdk 版本,则下载最新的策略文件[jce_policy-8.zip] 并将其替换为 %JAVA_HOME%/jre/lib/security 路径。[local_policy.jar,US_export_policy.jar]
【讨论】:
我遇到了JDK 1.8.0_101
的问题,这解决了这个问题,我只需要替换jdk
和jre
路径中的两个jar 文件。以上是关于JOSEException:无法创建 AES/GCM/NoPadding 密码:非法密钥大小的主要内容,如果未能解决你的问题,请参考以下文章