keyObject.export([options])


对于对称密钥,可以使用以下编码选项:

🌐 For symmetric keys, the following encoding options can be used:

  • format <string> 必须是 'buffer'(默认)或 'jwk'

对于公钥,可以使用以下编码选项:

🌐 For public keys, the following encoding options can be used:

  • type <string> 必须是 'pkcs1'(仅限 RSA)或 'spki' 之一。
  • format <string> 必须是 'pem''der''jwk'

对于私钥,可以使用以下编码选项:

🌐 For private keys, the following encoding options can be used:

  • type <string> 必须是 'pkcs1'(仅限 RSA)、'pkcs8''sec1'(仅限 EC)。
  • format <string> 必须是 'pem''der''jwk'
  • cipher <string> 如果指定,私钥将使用给定的 cipherpassphrase 通过 PKCS#5 v2.0 基于密码的加密进行加密。
  • passphrase <string> | <Buffer> 用于加密的密码短语,请参阅 cipher

结果类型取决于所选的编码格式,当为 PEM 时,结果是一个字符串;当为 DER 时,结果将是一个包含 DER 编码数据的缓冲区;当为 JWK 时,结果将是一个对象。

🌐 The result type depends on the selected encoding format, when PEM the result is a string, when DER it will be a buffer containing the data encoded as DER, when JWK it will be an object.

当选择 JWK 编码格式时,所有其他编码选项将被忽略。

🌐 When JWK encoding format was selected, all other encoding options are ignored.

可以使用 cipherformat 选项的组合来加密 PKCS#1、SEC1 和 PKCS#8 类型的密钥。PKCS#8 类型可以与任何 format 一起使用,通过指定 cipher 来加密任何密钥算法(RSA、EC 或 DH)。PKCS#1 和 SEC1 只有在使用 PEM format 时指定 cipher 才能加密。为了最大兼容性,加密私钥时建议使用 PKCS#8。由于 PKCS#8 定义了其自己的加密机制,因此在加密 PKCS#8 密钥时不支持 PEM 级别的加密。有关 PKCS#8 加密,请参见 RFC 5208;有关 PKCS#1 和 SEC1 加密,请参见 RFC 1421

🌐 PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of the cipher and format options. The PKCS#8 type can be used with any format to encrypt any key algorithm (RSA, EC, or DH) by specifying a cipher. PKCS#1 and SEC1 can only be encrypted by specifying a cipher when the PEM format is used. For maximum compatibility, use PKCS#8 for encrypted private keys. Since PKCS#8 defines its own encryption mechanism, PEM-level encryption is not supported when encrypting a PKCS#8 key. See RFC 5208 for PKCS#8 encryption and RFC 1421 for PKCS#1 and SEC1 encryption.