RSA私钥和公钥文件格式 (pkcs#7, pkcs#8, pkcs#12, pem)

FormatNameDescription
PKCS #7Cryptographic Message Syntax StandardA PKCS #7 file can be used to store certificates, which is a SignedData structure without data (just the certificates). The file name extension is usually .p7b.p7c
PKCS #8Private-Key Information Syntax Standard.Used to carry private certificate keypairs (encrypted or unencrypted).
PKCS #12Personal Information Exchange Syntax Standard.Defines a file format commonly used to store private keys with accompanying public key certificates, protected with a password-based symmetric key. It is the successor to PFX from Microsoft.
DERDistinguished Encoding RulesA binary format for keys or certificates. It is a message transfer syntax specified by the ITU in X.690.
PEMPrivacy Enhanced MailBase64 encoded DER certificates or keys, with additional header and footer lines. 

The PEM private key format uses the header and footer lines: 
-----BEGIN RSA PRIVATE KEY----- 
-----END RSA PRIVATE KEY----- 

The PEM public key format uses the header and footer lines: 
-----BEGIN PUBLIC KEY----- 
-----END PUBLIC KEY----- 

The PEM certificate uses the header and footer lines: 
-----BEGIN CERTIFICATE----- 
-----END CERTIFICATE----- 

RSA Public Key file (PKCS#1)

The RSA Public key PEM file is specific for RSA keys.

It starts and ends with the tags:

-----BEGIN RSA PUBLIC KEY-----
BASE64 ENCODED DATA
-----END RSA PUBLIC KEY-----

Within the base64 encoded data the following DER structure is present:

RSAPublicKey ::= SEQUENCE {
    modulus           INTEGER,  -- n
    publicExponent    INTEGER   -- e
}

Public Key file (PKCS#8)

Because RSA is not used exclusively inside X509 and SSL/TLS, a more generic key format is available in the form of PKCS#8, that identifies the type of public key and contains the relevant data.

It starts and ends with the tags:

-----BEGIN PUBLIC KEY-----
BASE64 ENCODED DATA
-----END PUBLIC KEY-----

Within the base64 encoded data the following DER structure is present:

PublicKeyInfo ::= SEQUENCE {
  algorithm       AlgorithmIdentifier,
  PublicKey       BIT STRING
}

AlgorithmIdentifier ::= SEQUENCE {
  algorithm       OBJECT IDENTIFIER,
  parameters      ANY DEFINED BY algorithm OPTIONAL
}

So for an RSA public key, the OID is 1.2.840.113549.1.1.1 and there is a RSAPublicKey as the PublicKey key data bitstring.

RSA Private Key file (PKCS#1)

The RSA private key PEM file is specific for RSA keys.

It starts and ends with the tags:

-----BEGIN RSA PRIVATE KEY-----
BASE64 ENCODED DATA
-----END RSA PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

RSAPrivateKey ::= SEQUENCE {
  version           Version,
  modulus           INTEGER,  -- n
  publicExponent    INTEGER,  -- e
  privateExponent   INTEGER,  -- d
  prime1            INTEGER,  -- p
  prime2            INTEGER,  -- q
  exponent1         INTEGER,  -- d mod (p-1)
  exponent2         INTEGER,  -- d mod (q-1)
  coefficient       INTEGER,  -- (inverse of q) mod p
  otherPrimeInfos   OtherPrimeInfos OPTIONAL
}

Private Key file (PKCS#8)

Because RSA is not used exclusively inside X509 and SSL/TLS, a more generic key format is available in the form of PKCS#8, that identifies the type of private key and contains the relevant data.

The unencrypted PKCS#8 encoded data starts and ends with the tags:

-----BEGIN PRIVATE KEY-----
BASE64 ENCODED DATA
-----END PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

PrivateKeyInfo ::= SEQUENCE {
  version         Version,
  algorithm       AlgorithmIdentifier,
  PrivateKey      BIT STRING
}

AlgorithmIdentifier ::= SEQUENCE {
  algorithm       OBJECT IDENTIFIER,
  parameters      ANY DEFINED BY algorithm OPTIONAL
}

So for an RSA private key, the OID is 1.2.840.113549.1.1.1 and there is a RSAPrivateKey as the PrivateKey key data bitstring.

The encrypted PKCS#8 encoded data start and ends with the tags:

-----BEGIN ENCRYPTED PRIVATE KEY-----
BASE64 ENCODED DATA
-----END ENCRYPTED PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

EncryptedPrivateKeyInfo ::= SEQUENCE {
  encryptionAlgorithm  EncryptionAlgorithmIdentifier,
  encryptedData        EncryptedData
}

EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

EncryptedData ::= OCTET STRING

The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo (see above).

        

         

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一些关于使用 OpenSSL 生成 PKCS#8 证书的简单说明。请注意,这只是一个简单的指南,如果您需要更详细的信息,请查阅 OpenSSL 文档或咨询专业人士。 步骤如下: 1. 生成私钥 要生成私钥,可以使用以下命令: ``` openssl genpkey -algorithm RSA -out private_key.pem -aes128 ``` 这将生成一个 RSA 算法的私钥,并将其保存在名为 "private_key.pem" 的文件中。 "-aes128" 选项将使用 128 位的对称加密算法来保护私钥文件。在生成私钥时,您将被要求输入一个密码来保护私钥文件。 2. 生成证书请求 接下来,您需要生成一个证书请求 (CSR),它将包含您的公钥和一些其他信息,以便证书颁发机构 (CA) 可以使用它来签署您的证书。要生成 CSR,请使用以下命令: ``` openssl req -new -key private_key.pem -out certificate_request.csr ``` 这将使用您在步骤 1 中生成的私钥来创建一个新的 CSR,并将其保存在名为 "certificate_request.csr" 的文件中。在生成 CSR 时,您将被要求提供一些其他信息,例如您的组织名称、国家/地区、城市等。 3. 生成证书 一旦您获得了 CSR,您可以将其发送给 CA,以便他们签署您的证书。但是,如果您正在使用自签名证书,或者您是 CA,那么您需要使用 CSR 和您的私钥来生成证书。要生成证书,请使用以下命令: ``` openssl x509 -req -in certificate_request.csr -signkey private_key.pem -out certificate.pem ``` 这将使用您在步骤 1 中生成的私钥来签署您的 CSR,并将签名后的证书保存在名为 "certificate.pem" 的文件中。 4. 将证书导出为 PKCS#8 格式 如果您需要将证书导出为 PKCS#8 格式,可以使用以下命令: ``` openssl pkcs8 -topk8 -in private_key.pem -out pkcs8_private_key.pem -nocrypt ``` 这将将您在步骤 1 中生成的私钥导出为 PKCS#8 格式,并将其保存在名为 "pkcs8_private_key.pem" 的文件中。 "-nocrypt" 选项指定不使用密码保护私钥文件。 希望这些步骤能够帮助您生成 PKCS#8 证书。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值