将RSA私钥转换为Java中的DER格式
我有一个.pem文件,其中包含这种格式的私钥:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3wVu5KhHVJjc9ri5mWKNDW5xXe08smNeu2GSAdBwEaGBHaWj
...
xqDDtaoYKUvwhuKHboTJMs9CtQyrVNk+TDSdfaEdTEWTNeu2UwaP4QBhA==
-----END RSA PRIVATE KEY-----
如果我想使用openssl手动转换它,我将使用此命令:
openssl pkcs8 -topk8 -inform PEM -outform DER -in secret.pem -nocrypt secret.key
但是,我想使用Java以编程方式进行操作,但我无法弄清楚如何。 任何帮助都非常感谢
I have a .pem file that contains the private key in this format:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3wVu5KhHVJjc9ri5mWKNDW5xXe08smNeu2GSAdBwEaGBHaWj
...
xqDDtaoYKUvwhuKHboTJMs9CtQyrVNk+TDSdfaEdTEWTNeu2UwaP4QBhA==
-----END RSA PRIVATE KEY-----
If I want to convert it manually using OpenSSL I would use this command:
openssl pkcs8 -topk8 -inform PEM -outform DER -in secret.pem -nocrypt secret.key
However, I want to do that programmatically using java but I couldn't figure out how.
Any help is much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OpenSSL语句将PKCS#1格式中的PEM编码的私钥转换为PKCS#8格式中的DER编码键。
在Java中,可以使用EG bouncycascelle's > pemparser 和
jcapemkeyconverter
(使用bcprov und und bcpkix jars)。可以使用privateKey#getEncoded()
返回DER编码的PKCS#8专用密钥:The OpenSSL statement converts the PEM encoded private key in PKCS#1 format into a DER encoded key in PKCS#8 format.
In Java, importing the PEM encoded PKCS#1 private key can be done with e.g. BouncyCastle's
PEMParser
andJcaPEMKeyConverter
(using the bcprov und bcpkix jars). The export can be accomplished withPrivateKey#getEncoded()
which returns the DER encoded PKCS#8 private key:您可以在背景中的控制台中运行任务。因此,您可以使用
ProcessBuilder
。如果您需要读取控制台输出(而不是输出文件),则可以使用输入流:
我没有特别测试此代码。如果您需要更多信息,请随时询问或访问此网站:
You can run the task in a console in the background. Therefor you can use a
ProcessBuilder
.If you need to read the console output (instead of an output file) you can use an input stream:
I haven't tested this code in particular. If you need more info feel free to ask or visit this website: Process Builder