简体   繁体   English

将私钥转换为 PEM 格式

[英]Convert private key in PEM format

I have created a self-signed certificate with Java code and added into KeyStore.我已经用 Java 代码创建了一个自签名证书并添加到 KeyStore 中。 Now I want to export Private key and Certificate created, into a file in PEM format.现在我想将创建的私钥和证书导出到 PEM 格式的文件中。 Is it possible to achieve this without any third party library ?是否有可能在没有任何第三方库的情况下实现这一目标? Below is the code I use for creating self-singed certificate.下面是我用于创建自签名证书的代码。

  public void createSelfSignedSSLCertificate() {
    try {            
        final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
        final X500Name x500Name =
            new X500Name(commonName, organizationalUnit, organization, city, state, country);
        keypair.generate(keysize);
        final PrivateKey privKey = keypair.getPrivateKey();
        final X509Certificate[] chain = new X509Certificate[1];
        chain[0] = keypair.getSelfCertificate(x500Name, new Date(), validity * 24 * 60 * 60);
        final String alias = JettySSLConfiguration.SSL_CERTIFICATE_ALIAS;
        keyStore.setKeyEntry(alias, privKey, keyStorePassword.toCharArray(), chain);
    } catch (final Exception e) {
       // Handle Exception
    }       
}

Any suggestion of how to export the key and certificate into file with PEM format will be really helpful.关于如何将密钥和证书导出为 PEM 格式的文件的任何建议都将非常有帮助。

You use Certificate.getEncoded() and Key.getEncoded() to get DER and do the base 64 encoding and header/footer manually, eg using DatatypeConverter.printBase64Binary() or some other way.您使用Certificate.getEncoded()Key.getEncoded()来获取 DER 并手动执行 base 64 编码和页眉/页脚,例如使用DatatypeConverter.printBase64Binary()或其他方式。 Something like:就像是:

certpem = "-----BEGIN CERTIFICATE-----\n" +
          DatatypeConverter.printBase64Binary(chain[0].getEncoded())) +
          "\n-----END CERTIFICATE-----\n";
keypem  = "-----BEGIN RSA PRIVATE KEY-----\n" +
          DatatypeConverter.printBase64Binary(privKey.getEncoded())) +
          "\n-----END RSA PRIVATE KEY-----\n";

Thanks Daniel Roethlisberger, for your reply.感谢 Daniel Roethlisberger 的回复。 I got great help from your reply..我从你的回复中得到了很大的帮助..

Implements in Java as below在Java中实现如下

String encodedString = "-----BEGIN PRIVATE KEY-----\n";
            encodedString = encodedString+Base64.getEncoder().encodeToString(Enrollment2.getKey().getEncoded())+"\n";
            encodedString = encodedString+"-----END PRIVATE KEY-----\n";

On Android, you can use the following Kotlin extension function:在 Android 上,您可以使用以下 Kotlin 扩展函数:

import android.util.Base64
import java.security.PublicKey

fun PublicKey.toPemString(): String {
    val publicKeyBase64: String = Base64.encodeToString(this.encoded, Base64.NO_WRAP)
    return publicKeyBase64.chunked(64).joinToString(
        separator = "\n",
        prefix = "-----BEGIN PUBLIC KEY-----\n",
        postfix = "\n-----END PUBLIC KEY-----\n"
    )
}

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

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