简体   繁体   English

将 .pem 转换为 .crt 和 .key

[英]Convert .pem to .crt and .key

Can anyone tell me the correct way/command to extract/convert the certificate .crt and private key .key files from a .pem file?谁能告诉我从.pem文件中提取/转换证书.crt和私钥.key文件的正确方法/命令吗? I just read they are interchangable, but not how.我只是读到它们是可以互换的,但不知道如何互换。

我能够使用这个将 pem 转换为 crt:

openssl x509 -outform der -in your-cert.pem -out your-cert.crt

Converting Using OpenSSL使用 OpenSSL 进行转换

These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software.这些命令允许您将证书和密钥转换为不同的格式,以使其与特定类型的服务器或软件兼容。

  • Convert a DER file (.crt .cer .der) to PEM将 DER 文件 (.crt .cer .der) 转换为 PEM

     openssl x509 -inform der -in certificate.cer -out certificate.pem
  • Convert a PEM file to DER将 PEM 文件转换为 DER

     openssl x509 -outform der -in certificate.pem -out certificate.der
  • Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM将包含私钥和证书的 PKCS#12 文件 (.pfx .p12) 转换为 PEM

     openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes You can add -nocerts to only output the private key or add -nokeys to only output the certificates.
  • Convert a PEM certificate file and a private key to PKCS#12 (.pfx .p12)将 PEM 证书文件和私钥转换为 PKCS#12 (.pfx .p12)

     openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt
  • Convert PEM to CRT (.CRT file)将 PEM 转换为 CRT(.CRT 文件)

     openssl x509 -outform der -in certificate.pem -out certificate.crt

OpenSSL Convert PEM OpenSSL 转换 PEM

  • Convert PEM to DER将 PEM 转换为 DER

     openssl x509 -outform der -in certificate.pem -out certificate.der
  • Convert PEM to P7B将 PEM 转换为 P7B

     openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer
  • Convert PEM to PFX将 PEM 转换为 PFX

     openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt

OpenSSL Convert DER OpenSSL 转换 DER

  • Convert DER to PEM将 DER 转换为 PEM

     openssl x509 -inform der -in certificate.cer -out certificate.pem

OpenSSL Convert P7B OpenSSL 转换 P7B

  • Convert P7B to PEM将 P7B 转换为 PEM

     openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer
  • Convert P7B to PFX将 P7B 转换为 PFX

     openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer

OpenSSL Convert PFX OpenSSL 转换 PFX

  • Convert PFX to PEM将 PFX 转换为 PEM

     openssl pkcs12 -in certificate.pfx -out certificate.cer -nodes

Generate rsa keys by OpenSSL通过 OpenSSL 生成 rsa 密钥

  • Using OpenSSL on the command line you'd first need to generate a public and private key, you should password protect this file using the -passout argument, there are many different forms that this argument can take so consult the OpenSSL documentation about that.在命令行上使用 OpenSSL,您首先需要生成公钥和私钥,您应该使用 -passout 参数密码保护此文件,此参数可以采用许多不同的形式,因此请参阅 OpenSSL 文档。

     openssl genrsa -out private.pem 1024
  • This creates a key file called private.pem that uses 1024 bits.这将创建一个名为 private.pem 的密钥文件,该文件使用 1024 位。 This file actually have both the private and public keys, so you should extract the public one from this file:这个文件实际上有私钥和公钥,所以你应该从这个文件中提取公钥:

     openssl rsa -in private.pem -out public.pem -outform PEM -pubout or openssl rsa -in private.pem -pubout > public.pem or openssl rsa -in private.pem -pubout -out public.pem

    You'll now have public.pem containing just your public key, you can freely share this with 3rd parties.您现在将拥有仅包含您的公钥的 public.pem,您可以自由地与第 3 方共享它。 You can test it all by just encrypting something yourself using your public key and then decrypting using your private key, first we need a bit of data to encrypt:您可以通过使用您的公钥自己加密一些东西然后使用您的私钥解密来测试这一切,首先我们需要一些数据来加密:

  • Example file :示例文件:

     echo 'too many secrets' > file.txt
  • You now have some data in file.txt, lets encrypt it using OpenSSL and the public key:您现在在 file.txt 中有一些数据,让我们使用 OpenSSL 和公钥对其进行加密:

     openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.ssl
  • This creates an encrypted version of file.txt calling it file.ssl, if you look at this file it's just binary junk, nothing very useful to anyone.这会创建一个名为 file.ssl 的 file.txt 的加密版本,如果你看看这个文件,它只是二进制垃圾,对任何人都没有什么用处。 Now you can unencrypt it using the private key:现在您可以使用私钥对其进行解密:

     openssl rsautl -decrypt -inkey private.pem -in file.ssl -out decrypted.txt
  • You will now have an unencrypted file in decrypted.txt:您现在将在decrypted.txt 中有一个未加密的文件:

     cat decrypted.txt |output -> too many secrets

RSA TOOLS Options in OpenSSL OpenSSL 中的 RSA 工具选项

  • NAME名称

    rsa - RSA key processing tool rsa - RSA 密钥处理工具

  • SYNOPSIS概要

    openssl rsa [-help] [-inform PEM|NET|DER] [-outform PEM|NET|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-aes128] [-aes192] [-aes256] [-camellia128] [-camellia192] [-camellia256] [-des] [-des3] [-idea] [-text] [-noout] [-modulus] [-check] [-pubin] [-pubout] [-RSAPublicKey_in] [-RSAPublicKey_out] [-engine id] openssl rsa [-help] [-inform PEM|NET|DER] [-outform PEM|NET|DER] [-in 文件名] [-passin arg] [-out 文件名] [-passout arg] [-aes128] [- aes192] [-aes256] [-camellia128] [-camellia192] [-camellia256] [-des] [-des3] [-idea] [-text] [-noout] [-modulus] [-check] [-pubin] [-pubout] [-RSAPublicKey_in] [-RSAPublicKey_out] [-engine id]

  • DESCRIPTION描述

    The rsa command processes RSA keys. rsa 命令处理 RSA 密钥。 They can be converted between various forms and their components printed out.它们可以在各种形式之间转换并打印出它们的组件。 Note this command uses the traditional SSLeay compatible format for private key encryption: newer applications should use the more secure PKCS#8 format using the pkcs8 utility.请注意,此命令使用传统的 SSLeay 兼容格式进行私钥加密:较新的应用程序应使用 pkcs8 实用程序使用更安全的 PKCS#8 格式。

  • COMMAND OPTIONS命令选项

    -help

    Print out a usage message.打印使用信息。

     -inform DER|NET|PEM

    This specifies the input format.这指定了输入格式。 The DER option uses an ASN1 DER encoded form compatible with the PKCS#1 RSAPrivateKey or SubjectPublicKeyInfo format. DER 选项使用与 PKCS#1 RSAPrivateKey 或 SubjectPublicKeyInfo 格式兼容的 ASN1 DER 编码形式。 The PEM form is the default format: it consists of the DER format base64 encoded with additional header and footer lines. PEM 格式是默认格式:它由带有附加页眉和页脚行的 DER 格式 base64 编码组成。 On input PKCS#8 format private keys are also accepted.也接受输入 PKCS#8 格式的私钥。 The NET form is a format is described in the NOTES section. NET 形式是一种在 NOTES 部分中描述的格式。

     -outform DER|NET|PEM

    This specifies the output format, the options have the same meaning as the -inform option.这指定了输出格式,这些选项与 -inform 选项具有相同的含义。

     -in filename

    This specifies the input filename to read a key from or standard input if this option is not specified.如果未指定此选项,则指定要从中读取密钥或标准输入的输入文件名。 If the key is encrypted a pass phrase will be prompted for.如果密钥已加密,将提示输入密码短语。

     -passin arg

    the input file password source.输入文件密码源。 For more information about the format of arg see the PASS PHRASE ARGUMENTS section in openssl.有关 arg 格式的更多信息,请参阅 openssl 中的 PASS PHRASE ARGUMENTS 部分。

     -out filename

    This specifies the output filename to write a key to or standard output if this option is not specified.如果未指定此选项,则指定要写入密钥或标准输出的输出文件名。 If any encryption options are set then a pass phrase will be prompted for.如果设置了任何加密选项,则会提示输入密码。 The output filename should not be the same as the input filename.输出文件名不应与输入文件名相同。

     -passout password

    the output file password source.输出文件密码源。 For more information about the format of arg see the PASS PHRASE ARGUMENTS section in openssl.有关 arg 格式的更多信息,请参阅 openssl 中的 PASS PHRASE ARGUMENTS 部分。

     -aes128|-aes192|-aes256|-camellia128|-camellia192|-camellia256|-des|-des3|-idea

    These options encrypt the private key with the specified cipher before outputting it.这些选项在输出之前使用指定的密码对私钥进行加密。 A pass phrase is prompted for.提示输入密码。 If none of these options is specified the key is written in plain text.如果没有指定这些选项,则密钥以纯文本形式写入。 This means that using the rsa utility to read in an encrypted key with no encryption option can be used to remove the pass phrase from a key, or by setting the encryption options it can be use to add or change the pass phrase.这意味着使用 rsa 实用程序读取没有加密选项的加密密钥可用于从密钥中删除密码,或者通过设置加密选项,它可用于添加或更改密码。 These options can only be used with PEM format output files.这些选项只能用于 PEM 格式的输出文件。

     -text

    prints out the various public or private key components in plain text in addition to the encoded version.除了编码版本之外,还以纯文本形式打印出各种公钥或私钥组件。

     -noout

    this option prevents output of the encoded version of the key.此选项可防止输出密钥的编码版本。

     -modulus

    this option prints out the value of the modulus of the key.此选项打印出键的模数的值。

     -check

    this option checks the consistency of an RSA private key.此选项检查 RSA 私钥的一致性。

     -pubin

    by default a private key is read from the input file: with this option a public key is read instead.默认情况下,从输入文件中读取私钥:使用此选项读取公钥。

     -pubout

    by default a private key is output: with this option a public key will be output instead.默认情况下输出私钥:使用此选项将输出公钥。 This option is automatically set if the input is a public key.如果输入是公钥,则会自动设置此选项。

     -RSAPublicKey_in, -RSAPublicKey_out

    like -pubin and -pubout except RSAPublicKey format is used instead.像 -pubin 和 -pubout 一样,但使用了 RSAPublicKey 格式。

     -engine id

    specifying an engine (by its unique id string) will cause rsa to attempt to obtain a functional reference to the specified engine, thus initialising it if needed.指定引擎(通过其唯一的 id 字符串)将导致 rsa 尝试获取对指定引擎的功能引用,从而在需要时对其进行初始化。 The engine will then be set as the default for all available algorithms.然后引擎将被设置为所有可用算法的默认值。

  • NOTES笔记

    The PEM private key format uses the header and footer lines: PEM 私钥格式使用页眉和页脚行:

     -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----

    The PEM public key format uses the header and footer lines: PEM 公钥格式使用页眉和页脚行:

     -----BEGIN PUBLIC KEY----- -----END PUBLIC KEY-----

    The PEM RSAPublicKey format uses the header and footer lines: PEM RSAPublicKey 格式使用页眉和页脚行:

     -----BEGIN RSA PUBLIC KEY----- -----END RSA PUBLIC KEY-----

    The NET form is a format compatible with older Netscape servers and Microsoft IIS .key files, this uses unsalted RC4 for its encryption. NET 格式是一种与旧版 Netscape 服务器和 Microsoft IIS .key 文件兼容的格式,它使用未加盐的 RC4 进行加密。 It is not very secure and so should only be used when necessary.它不是很安全,因此应仅在必要时使用。

    Some newer version of IIS have additional data in the exported .key files.某些较新版本的 IIS 在导出的 .key 文件中有附加数据。 To use these with the utility, view the file with a binary editor and look for the string "private-key", then trace back to the byte sequence 0x30, 0x82 (this is an ASN1 SEQUENCE).要将这些与实用程序一起使用,请使用二进制编辑器查看文件并查找字符串“private-key”,然后追溯到字节序列 0x30、0x82(这是 ASN1 SEQUENCE)。 Copy all the data from this point onwards to another file and use that as the input to the rsa utility with the -inform NET option.从此点开始将所有数据复制到另一个文件,并将其用作带有 -inform NET 选项的 rsa 实用程序的输入。

    EXAMPLES例子

    To remove the pass phrase on an RSA private key:要删除 RSA 私钥上的密码短语:

     openssl rsa -in key.pem -out keyout.pem

    To encrypt a private key using triple DES:要使用三重 DES 加密私钥:

     openssl rsa -in key.pem -des3 -out keyout.pem

    To convert a private key from PEM to DER format:要将私钥从 PEM 转换为 DER 格式:

     openssl rsa -in key.pem -outform DER -out keyout.der

    To print out the components of a private key to standard output:要将私钥的组件打印到标准输出:

     openssl rsa -in key.pem -text -noout

    To just output the public part of a private key:只输出私钥的公共部分:

     openssl rsa -in key.pem -pubout -out pubkey.pem

    Output the public part of a private key in RSAPublicKey format:以 RSAPublicKey 格式输出私钥的公共部分:

     openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem

To extract the key and cert from a pem file:从 pem 文件中提取密钥和证书:

Extract key提取密钥

openssl pkey -in foo.pem -out foo.key

Another method of extracting the key...提取密钥的另一种方法...

openssl rsa -in foo.pem -out foo.key

Extract all the certs, including the CA Chain提取所有证书,包括 CA 链

openssl crl2pkcs7 -nocrl -certfile foo.pem | openssl pkcs7 -print_certs -out foo.cert

Extract the textually first cert as DER将文本上的第一个证书提取为 DER

openssl x509 -in foo.pem -outform DER -out first-cert.der

Pre-requisite先决条件

openssl should be installed.应该安装openssl On Windows, if Git Bash is installed, try that!在 Windows 上,如果安装了Git Bash ,请尝试! Alternate binaries can be found here.可以在此处找到替代二进制文件

Step 1: Extract .key from .pem第 1 步:从.pem提取.key

openssl pkey -in cert.pem -out cert.key

Step 2: Extract .crt from .pem第 2 步:从.pem提取.crt

openssl crl2pkcs7 -nocrl -certfile cert.pem | openssl pkcs7 -print_certs -out cert.crt

This is what I did on windows.这就是我在windows上所做的。

  1. Download a zip file that contains the open ssl exe fromGoogleGoogle下载包含打开的 ssl exe 的 zip 文件
  2. Unpack the zip file and go into the bin folder.解压 zip 文件并进入 bin 文件夹。
  3. Go to the address bar in the bin folder and type cmd.转到 bin 文件夹中的地址栏并键入 cmd。 This will open a command prompt at this folder.这将在此文件夹中打开命令提示符。
  4. move/Put the .pem file into this bin folder.将 .pem 文件移动/放入此 bin 文件夹中。
  5. Run two commands.运行两个命令。 One creates the cert and the second the key file一个创建证书,第二个创建密钥文件
openssl x509 -outform der -in yourPemFilename.pem -out certfileOutName.crt
openssl rsa -in yourPemFilename.pem -out keyfileOutName.key

If you asked this question because you're using mkcert then the trick is that the .pem file is the cert and the -key.pem file is the key.如果您问这个问题是因为您使用的是mkcert那么诀窍是.pem文件是证书, -key.pem文件是密钥。

(You don't need to convert, just run mkcert yourdomain.dev otherdomain.dev ) (您不需要转换,只需运行mkcert yourdomain.dev otherdomain.dev

A .crt stores the certificate.. in pem format. .crt 以 pem 格式存储证书。 So a .pem, while it can also have other things like a csr (Certificate signing request), a private key, a public key, or other certs, when it is storing just a cert, is the same thing as a .crt.所以 .pem 也可以有其他东西,比如 csr(证书签名请求)、私钥、公钥或其他证书,当它只存储证书时,与 .crt 是一样的。

A pem is a base 64 encoded file with a header and a footer between each section. pem 是一个 base 64 编码的文件,每个部分之间有一个页眉和一个页脚。

To extract a particular section, a perl script such as the following is totally valid, but feel free to use some of the openssl commands.要提取特定部分,如下所示的 perl 脚本是完全有效的,但可以随意使用一些 openssl 命令。

 perl -ne "\$n++ if /BEGIN/; print if \$n == 1 && /BEGIN/.../END/;" mydomain.pem

where ==1 can be changed to which ever section you need.其中 ==1 可以更改为您需要的任何部分。 Obviously if you know exactly the header and footer you require and there is only one of those in the file (usually the case if you keep just the cert and the key in there), you can simplify it:显然,如果您确切地知道所需的页眉和页脚,并且文件中只有其中一个(通常情况下,如果您只保留证书和密钥),您可以简化它:

 perl -ne "print if /^-----BEGIN CERTIFICATE-----\$/.../END/;" mydomain.pem

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

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