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:
format<string> 必须是'pem'、'der'、'jwk'或'raw-public'。有关格式支持,请参见 非对称密钥类型。type<string> 当format是'pem'或'der'时,必须是'pkcs1'(仅限 RSA)或'spki'。对于具有'raw-public'格式的 EC 密钥,可以是'uncompressed'(默认)或'compressed'。当format为'jwk'时被忽略。
对于私钥,可以使用以下编码选项:
🌐 For private keys, the following encoding options can be used:
format<string> 必须是'pem'、'der'、'jwk'、'raw-private'或'raw-seed'。有关格式支持,请参见 非对称密钥类型。type<string> 当format为'pem'或'der'时,必须为'pkcs1'(仅限 RSA)、'pkcs8'或'sec1'(仅限 EC)。当format为'jwk'、'raw-private'或'raw-seed'时将被忽略。cipher<string> 如果指定,私钥将使用给定的cipher和passphrase通过基于密码的 PKCS#5 v2.0 加密进行加密。当format为'jwk'、'raw-private'或'raw-seed'时将被忽略。passphrase<string> | <Buffer> 用于加密的密码短语。当指定cipher时必需。
结果类型取决于所选的编码格式,当为 PEM 时,结果是一个字符串;当为 DER 时,它将是一个包含 DER 编码数据的缓冲区;当为 JWK 时,它将是一个对象。原始格式返回一个包含原始密钥材料的 <Buffer>。
🌐 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. Raw formats return a <Buffer> containing the raw key material.
可以通过指定 cipher 和 passphrase 来加密私钥。PKCS#8 type 支持使用 PEM 和 DER format 对任何密钥算法进行加密。PKCS#1 和 SEC1 只有在使用 PEM format 时才可以加密。为了最大兼容性,请使用 PKCS#8 来加密私钥。由于 PKCS#8 定义了自己的加密机制,因此在加密 PKCS#8 密钥时不支持 PEM 级别的加密。有关 PKCS#8 加密,请参见 RFC 5208,有关 PKCS#1 和 SEC1 加密,请参见 RFC 1421。
🌐 Private keys can be encrypted by specifying a cipher and passphrase.
The PKCS#8 type supports encryption with both PEM and DER format for any
key algorithm. PKCS#1 and SEC1 can only be encrypted 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.