将RSA私钥转换为Java中的DER格式

发布于 2025-02-12 01:06:50 字数 436 浏览 0 评论 0原文

我有一个.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 技术交流群。

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

发布评论

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

评论(2

过期以后 2025-02-19 01:06:51

OpenSSL语句将PKCS#1格式中的PEM编码的私钥转换为PKCS#8格式中的DER编码键。

在Java中,可以使用EG bouncycascelle's > pemparser 和jcapemkeyconverter(使用bcprov und und bcpkix jars)。可以使用privateKey#getEncoded()返回DER编码的PKCS#8专用密钥:

import java.io.FileOutputStream;
import java.io.FileReader;
import java.security.KeyPair;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
...
String inputFile = "<path to PKCS#1 PEM key>";
String outputFile = "<path to PKCS#8 DER key>";
try (FileReader fileReader = new FileReader(inputFile);
     PEMParser pemParser = new PEMParser(fileReader);
     FileOutputStream outputStream = new FileOutputStream(outputFile)) {
    // Import PEM encoded PKCS#1 private key
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    KeyPair keyPair = converter.getKeyPair((PEMKeyPair)pemParser.readObject());
    // Export DER encoded PKCS#8 private key
    byte[] privateKey = keyPair.getPrivate().getEncoded();
    outputStream.write(privateKey, 0, privateKey.length);
}

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 and JcaPEMKeyConverter (using the bcprov und bcpkix jars). The export can be accomplished with PrivateKey#getEncoded() which returns the DER encoded PKCS#8 private key:

import java.io.FileOutputStream;
import java.io.FileReader;
import java.security.KeyPair;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
...
String inputFile = "<path to PKCS#1 PEM key>";
String outputFile = "<path to PKCS#8 DER key>";
try (FileReader fileReader = new FileReader(inputFile);
     PEMParser pemParser = new PEMParser(fileReader);
     FileOutputStream outputStream = new FileOutputStream(outputFile)) {
    // Import PEM encoded PKCS#1 private key
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    KeyPair keyPair = converter.getKeyPair((PEMKeyPair)pemParser.readObject());
    // Export DER encoded PKCS#8 private key
    byte[] privateKey = keyPair.getPrivate().getEncoded();
    outputStream.write(privateKey, 0, privateKey.length);
}
枉心 2025-02-19 01:06:51

您可以在背景中的控制台中运行任务。因此,您可以使用ProcessBuilder

//split the command every space and create a process builder
String[] commands = "your command written above...".split(" ");
ProcessBuilder pb = new ProcessBuilder(commands);
// starting the process
Process process = pb.start();

如果您需要读取控制台输出(而不是输出文件),则可以使用输入流:

// for reading the output from stream
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
            process.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

我没有特别测试此代码。如果您需要更多信息,请随时询问或访问此网站:

You can run the task in a console in the background. Therefor you can use a ProcessBuilder.

//split the command every space and create a process builder
String[] commands = "your command written above...".split(" ");
ProcessBuilder pb = new ProcessBuilder(commands);
// starting the process
Process process = pb.start();

If you need to read the console output (instead of an output file) you can use an input stream:

// for reading the output from stream
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
            process.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

I haven't tested this code in particular. If you need more info feel free to ask or visit this website: Process Builder

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