You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jose4j解密JWE遇'RSA1_5'算法不被允许问题求助

使用jose4j解密JWE时遇到RSA1_5算法不被允许的错误

错误信息

Exception in thread "main" org.jose4j.lang.InvalidAlgorithmException: 'RSA1_5' is not a permitted algorithm.
    at org.jose4j.jwa.AlgorithmConstraints.checkConstraint(AlgorithmConstraints.java:80)
    at org.jose4j.jwe.JsonWebEncryption.getKeyManagementModeAlgorithm(JsonWebEncryption.java:172)
    at org.jose4j.jwe.JsonWebEncryption.getKeyManagementModeAlgorithm(JsonWebEncryption.java:159)
    at org.jose4j.jwe.JsonWebEncryption.decrypt(JsonWebEncryption.java:244)
    at org.jose4j.jwe.JsonWebEncryption.getPlaintextBytes(JsonWebEncryption.java:96)
    at org.jose4j.jwe.JsonWebEncryption.getPlaintextString(JsonWebEncryption.java:89)

实现代码

String input = "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBMV81In0.oGr_d4RGBPZ_e_YHRQft7Y1ZNtSuN38SI0_atkT7PH0qzk108UjHd0qTxcBKOeKwo2q5dbYePog57r5OWQQaDfTFBdsUNebm7jzq8MEJRzOgeE7IQKI7kasOLl3Hh-NkqSIF2k2BZJs0HMjVrXB4PMVBSnNPDNwDTmxVUncjWL0JE4y-KdWs2bXLPqkNjsjf9tWYmTyVLMeKRqnPaHaE9mpn5Lkx8QICPggtvB_jA-B_-ZvHjZmQCslIvxucgO6iJg-9E5d3_NLkA425webh4G6V91B2t1He6UO3jv2Yh2wWvpkNCgM-xDqyYk8mvxBpkfqihISZOPJRdk6VOEnX4A.l9QqgShp_69MKm-YQhM0Fw.VhfTxavzPmZOFKdiUZaxnQhXcPGZeuqC7rX5eWWGUMwkOMR_sdZjKAESAN2kbfj_55ELO_Y8cN1MWAUHFFD6VUPpwictOw_KoUZylT5KRxN0_seltNdeE2jYK1NlHjfdpe9WZ-TFPQOYLV3oX3r-utx2uMq0B1zPLkihfmyupWP49CzI5O4hD5Hstjwb0h79x702wJEMFsvZ8BJawTUkJHVohmadQvc7fikd6J3WpDeC8PdZPQOkLuXPb5azShvKwGPycIVsURo07VJRTiL3FpNDmw_MQKFoJDro9uWSfrbZV96gcRHaj-Tm-xXQuHUehim0TVG_zlLGJDPHDQBAzJX5Tdlzs65gK98ma5gaaQFXRXcnETgVKqOrazN3uFvaX198j4NJwIsDfQL2QXHpGdWbunfBQA5EwMllT-kOWDa3I-6EiD2drYOTYyXHRbXwBwbOkXSZCC_IRCsSwKeE7KkFtpLwlyxs1Sut3YLbapVRXT43OCmU4wrVK48sZ4SzMUcjrMqAixZ_EvZr6gVHCj9q7Wq2b2yKcVnn4P21Kq2y5mD6POoDYz5_FSGj7jHwlp8FAOg-DxyOmADWdOdSJ5YCR0nVfqg8WdlH_xqBWCS2DptNiRlC9Q5CY-ECqgUqiZmfiu61F1OlG_1QQvWQtxWfywRm7mLgEz0dkCI2fBskbCCSUGH07jvfYKtq2dBJAYAni3Ga6-pwm7jhsWl2kYXxuksBSFSlUWnOPSQsroVtbY0TyJov8F6KlJfqplRYPuKG25MWXbfXF_J53Civ3J_fGtFMRVQLwmS9MPLccWLHm50YhxIV1u6iyXGRg6ANdtoeqmN3TAUCj96R4s0LTQkzjUG0gt1XiPeL26ipbGqDryBkSHcsC4JUz5kQPNiC.T88i20YLQyoaCqG1mKrB4w0";
String keyStorePW = "password";
String keyStore = "PFX.pfx";
String partnerCert = "partnerCert.crt";

KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyStore), keyStorePW.toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey privKey = (PrivateKey)ks.getKey(alias, keyStorePW.toCharArray());
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
FileInputStream fis = new FileInputStream(new File(partnerCert));
X509Certificate signVerifyCert = (X509Certificate) certificateFactory.generateCertificate(fis);
PublicKey pubKey = signVerifyCert.getPublicKey();

JsonWebEncryption receiverJwe = new JsonWebEncryption();

receiverJwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.RSA1_5);
receiverJwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);

receiverJwe.setCompactSerialization(input);
receiverJwe.setKey(privKey);

String decrypted = receiverJwe.getPlaintextString();
JsonWebSignature jwsignature = new JsonWebSignature();
jwsignature.setKey(pubKey);
jwsignature.setCompactSerialization(decrypted);
String decryptedJson = receiverJwe.getPayload();

System.out.println("Decrypted: \n"+decryptedJson);

问题

  • jose4j库是否不支持RSA1_5算法?
  • 若支持该算法,有无解决此问题的办法?
  • 若不支持,有哪些可替代的Java加密库?

解答

1. jose4j是否支持RSA1_5?

jose4j完全支持RSA1_5算法,错误提示并非因为库不支持,而是默认的算法约束(AlgorithmConstraints)禁止使用该算法。RSA1_5属于旧版密钥管理算法,存在潜在安全风险,因此jose4j默认会限制它的使用。

2. 解决办法

需要显式配置算法约束,允许RSA1_5的使用。修改代码如下:

JsonWebEncryption receiverJwe = new JsonWebEncryption();

// 添加这行,设置允许RSA1_5算法
receiverJwe.setAlgorithmConstraints(AlgorithmConstraints.ALLOW_ONLY_RSA1_5);

receiverJwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.RSA1_5);
receiverJwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);

receiverJwe.setCompactSerialization(input);
receiverJwe.setKey(privKey);

如果需要同时支持多个算法,也可以用AlgorithmConstraints的自定义规则,比如:

AlgorithmConstraints constraints = new AlgorithmConstraints(
    AlgorithmConstraints.ConstraintType.PERMIT,
    KeyManagementAlgorithmIdentifiers.RSA1_5,
    KeyManagementAlgorithmIdentifiers.RSA_OAEP
);
receiverJwe.setAlgorithmConstraints(constraints);

另外注意:不需要手动调用setAlgorithmHeaderValue,因为JWE的紧凑序列化字符串中已经包含了alg头信息,jose4j会自动解析。

3. 替代Java加密库

如果因安全策略等原因不想使用jose4j,可选择以下成熟的Java JOSE/JWT库:

  • Nimbus JOSE + JWT:功能全面,支持所有主流JOSE算法,文档详细,社区活跃。
  • Auth0 Java JWT:轻量易用,专注于JWT的生成与解析,API简洁。
  • Apache Santuario:Apache旗下的安全库,支持XML加密和JOSE规范,适合企业级应用。

内容的提问来源于stack exchange,提问作者juniorDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 08:15:15