是否有任何 java API 可以从“DER”转换证书?格式为 pkcs#12 格式

发布于 2024-10-06 01:39:27 字数 98 浏览 0 评论 0原文

我需要将证书从 DER 格式转换为 pkcs#12 格式。我知道我可以使用 openssl 命令来做到这一点。我正在寻找可以完成这项工作的java API/类。 任何帮助将不胜感激。

I need to convert certificate from DER format to pkcs#12 format. I know I can do this using openssl command. I am looking for java API / Class which can do this job.
Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好久不见√ 2024-10-13 01:39:27

您可以尝试将 PKCS#12 容器作为密钥库加载:

java.security.KeyStore ks = java.security.KeyStore.getInstance("PKCS12");
ks.load(new java.io.FileInputStream("yourStore.p12"), "yourPassword".toCharArray());

加载后,您可以枚举容器内的元素:

for(Enumeration enum = ks.aliases(); enum.hasMoreElements(); ) {
    String alias = (String) enum.nextElement();
    System.out.println("@:" + alias);
    if (ks.isKeyEntry(alias)) {
        System.out.println("return PrivateKey");
        PrivateKey pk = (PrivateKey) ks.getKey(alias, password);
        // ...
    }
}

You can try to load the PKCS#12 container as a keystore:

java.security.KeyStore ks = java.security.KeyStore.getInstance("PKCS12");
ks.load(new java.io.FileInputStream("yourStore.p12"), "yourPassword".toCharArray());

Once loaded, you can enumerate the elements inside the container:

for(Enumeration enum = ks.aliases(); enum.hasMoreElements(); ) {
    String alias = (String) enum.nextElement();
    System.out.println("@:" + alias);
    if (ks.isKeyEntry(alias)) {
        System.out.println("return PrivateKey");
        PrivateKey pk = (PrivateKey) ks.getKey(alias, password);
        // ...
    }
}
暖阳 2024-10-13 01:39:27

据我了解,PEM 只是 DER 内容的 Base64 编码字符串,具有适当的页眉和页脚行。例如,要转换为 Base64,您可以使用 javax.mail.internet.MimeUtility。

As I understand it, PEM is simply the Base64 encoded string of the DER content with the appropriate header and footer lines. To convert to Base64 you can use javax.mail.internet.MimeUtility for example.

染柒℉ 2024-10-13 01:39:27

这可以将 pkcs 从 PEM 转换

openssl pkcs12 -export -in pem-certificate-and-key-file -out pkcs-12-certificate and-key-file

,所以让我们首先将 DER 转换为 PEM

openssl dsa -inform PEM|DER -outform DER|PEM -in pem-file|der-file -out der-file|pem-file

This can convert pkcs from PEM

openssl pkcs12 -export -in pem-certificate-and-key-file -out pkcs-12-certificate and-key-file

So lets convert your DER to PEM first

openssl dsa -inform PEM|DER -outform DER|PEM -in pem-file|der-file -out der-file|pem-file
回忆追雨的时光 2024-10-13 01:39:27

我从 Portecle 中获益匪浅。

如果您必须以编程方式执行此操作,则您需要的大部分内容都在 Java 的 KeyStore 类中。至于用户友好性,它对朋友的选择非常挑剔。打开商店、添加商店、保存商店。如果您需要证书链,那就会更复杂一些。

至于从 DER 编码检索证书,请参阅 X509Certificate javadoc。特别是对CertificateFactory 的引用。

I've gotten a lot of mileage out of Portecle.

If you must do it programmatically, much of what you need is in the KeyStore class in Java. As for user friendliness, well, it's pretty selective about its friends. Open a store, add to it, save it. If you need cert chains, that'll be a bit more complicated.

As for retrieving the cert from the DER encoding, see the X509Certificate javadoc. Especially the references to CertificateFactory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文