简体   繁体   English

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

[英]Convert RSA Private Key to DER Format in Java

I have a .pem file that contains the private key in this format:我有一个 .pem 文件,其中包含这种格式的私钥:

-----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 手动转换它,我会使用这个命令:

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.但是,我想使用 java 以编程方式执行此操作,但我不知道如何。 Any help is much appreciated任何帮助深表感谢

The OpenSSL statement converts the PEM encoded private key in PKCS#1 format into a DER encoded key in PKCS#8 format. OpenSSL 语句将 PKCS#1 格式的 PEM 编码私钥转换为 PKCS#8 格式的 DER 编码密钥。

In Java, importing the PEM encoded PKCS#1 private key can be done with eg BouncyCastle's PEMParser and JcaPEMKeyConverter (using the bcprov und bcpkix jars).在 Java 中,导入 PEM 编码的 PKCS#1 私钥可以通过BouncyCastle 的PEMParserJcaPEMKeyConverter (使用 bcprov 和 bcpkix jars)来完成。 The export can be accomplished with PrivateKey#getEncoded() which returns the DER encoded PKCS#8 private key:可以使用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);
}

You can run the task in a console in the background.您可以在后台的控制台中运行该任务。 Therefor you can use a ProcessBuilder .因此,您可以使用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如果您需要更多信息,请随时询问或访问此网站: Process Builder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM