在java中将PKCS#8私钥转换为PEM

发布于 2024-09-16 00:54:03 字数 853 浏览 10 评论 0原文

大家好,我正在尝试将我在 java 程序中生成的 PKCS#8 私钥转换为 PEM 编码文件。

Security.addProvider(new BouncyCastleProvider());
SecureRandom rand = new SecureRandom();
JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA();        
keyPairGen.initialize(2048, rand);
KeyPair keyPair = keyPairGen.generateKeyPair();

PEMWriter privatepemWriter = new PEMWriter(new FileWriter(new File(dir + "private.key")));
privatepemWriter.writeObject(keyPair.getPrivate());

运行程序后,我拥有两种格式的私钥和公钥(代码在工作时未显示)。然后,我使用此 openssl 命令将 private.key 转换回 pem 格式的文件。

openssl pkcs8 -nocrypt -inform DER -in private.key -out private2.pem

当我比较 private.pem 和 private2.pem 时,它们是不同的,显然当我尝试使用 private.pem 时,它说它不是有效文件。

为了正确将此私钥转换为我需要的 PEM 格式,我缺少哪一步?我无法在程序中使用 OpenSSL,否则我只需添加该函数调用即可。我可以访问这个程序中的 BouncyCastle 库,所以也许它有一个我忽略的解决方案。

Hello everyone I'm trying to convert a PKCS#8 private key that I generate in my java program to a PEM encoded file.

Security.addProvider(new BouncyCastleProvider());
SecureRandom rand = new SecureRandom();
JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA();        
keyPairGen.initialize(2048, rand);
KeyPair keyPair = keyPairGen.generateKeyPair();

PEMWriter privatepemWriter = new PEMWriter(new FileWriter(new File(dir + "private.key")));
privatepemWriter.writeObject(keyPair.getPrivate());

After running the program I have the private key in both formats and a public key(the code isn't shown as it works). I then use this openssl command to conver the private.key back to a pem formated file.

openssl pkcs8 -nocrypt -inform DER -in private.key -out private2.pem

When I compare private.pem and private2.pem they are different and obviously when I try to use private.pem it says it's not a valid file.

What step am I missing in order to properly convert this private key into the PEM format that I need? I can't use OpenSSL from within my program, otherwise I would simply add that function call. I have access to BouncyCastle libs in this program, so maybe it has a solution I'm overlooking.

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

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

发布评论

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

评论(3

抽个烟儿 2024-09-23 00:54:03

您可以在 Bouncycastle 中使用 PEMWriter 类。

You can use the PEMWriter class in Bouncycastle.

狂之美人 2024-09-23 00:54:03

OpenSSL 使用它自己的格式这一事实确实是唯一让这一点具有挑战性的事情。值得庆幸的是,充气城堡 PEMWriter 使这一切变得简单,但界面没有很好的文档记录。我通过搜索邮件列表找到了一些代码。我对其进行了如下调整:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair(); 
StringWriter stringWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(stringWriter);  
pemWriter.writeObject( keyPair.getPrivate());
pemWriter.close();
privateKeyString = stringWriter.toString();

The fact that OpenSSL uses it's own format is really the only thing that makes this challenging. Thankfully the bouncy castle PEMWriter makes this easy, but the interface isn't very well documented. I found some code by searching through the mailing list. I've adapted it below:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair(); 
StringWriter stringWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(stringWriter);  
pemWriter.writeObject( keyPair.getPrivate());
pemWriter.close();
privateKeyString = stringWriter.toString();
空气里的味道 2024-09-23 00:54:03

使用页眉:

-----BEGIN PRIVATE KEY-----

... 和页脚:

-----END PRIVATE KEY-----

请注意,“RSA”被省略 - Java 代码对私钥使用 PKCS #8 编码,并且该编码包括算法。

您显示的 openssl 命令会将 DER 形式的标准 PKCS #8 密钥转换为 PEM 形式的专有 OpenSSL 密钥。要保留 PKCS #8 格式,但从 DER 转换为 PEM,请添加 -topk8 选项。然后 OpenSSL 输出应该与您的 Java 代码生成的内容相匹配。

如果您需要生成 OpenSSL 密钥,而不是 PKCS #8,这是可能的,但您必须使用 BouncyCastle ASN.1 库创建自己的 OpenSSL 结构并对其进行编码。请澄清这是否是您所需要的。

Use the header:

-----BEGIN PRIVATE KEY-----

… and the footer:

-----END PRIVATE KEY-----

Note that the "RSA" is left out—The Java code is using PKCS #8 encoding for the private key, and that encoding includes the algorithm.

The openssl command that you show is converting a standard PKCS #8 key in DER form to a proprietary OpenSSL key in PEM form. To keep the PKCS #8 format, but convert from DER to PEM, add the -topk8 option. Then the OpenSSL output should match what your Java code is producing.

If you need to produce the OpenSSL key, instead of PKCS #8, it's possible, but you'll have to create your own OpenSSL structure with the BouncyCastle ASN.1 library and encode that. Please clarify if that's what you need.

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