如何将nimbus-jose-jwt 版本9.x JWK 转换成 X509Certificate,Open JDK 17,Too short at java.base/sun.security.util.DerValue.<init>(DerValue.java:361) 如何解决
时间: 2024-03-02 22:53:49 浏览: 143
nimbus-jose-jwt-3.9.jar中文-英文对照文档.zip
5星 · 资源好评率100%
您可以尝试使用以下代码将JWK转换为X509Certificate:
```java
import com.nimbusds.jose.jwk.RSAKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
public static X509Certificate jwkToX509(RSAKey jwk) throws Exception {
RSAPublicKeySpec spec = new RSAPublicKeySpec(jwk.getModulus(), jwk.getPublicExponent());
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
byte[] derEncoded = publicKey.getEncoded();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(derEncoded));
}
```
如果您在运行时遇到“Too short at java.base/sun.security.util.DerValue.<init>(DerValue.java:361)”的错误,请确保您的JWK中的公钥长度不小于2048位。如果公钥长度小于2048位,则可能会导致此错误。
阅读全文