crypto.publicEncrypt(key, buffer)
key
<Object> | <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey>key
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey> PEM 编码的公钥或私钥、<KeyObject> 或 <CryptoKey>。oaepHash
<string> 用于 OAEP 填充和 MGF1 的哈希函数。 默认值:'sha1'
oaepLabel
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 用于 OAEP 填充的标签。 如果未指定,则不使用标签。passphrase
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 可选的私钥密码。padding
<crypto.constants>crypto.constants
中定义的可选填充值,可以是:crypto.constants.RSA_NO_PADDING
、crypto.constants.RSA_PKCS1_PADDING
或crypto.constants.RSA_PKCS1_OAEP_PADDING
。encoding
<string> 当buffer
、key
、oaepLabel
或passphrase
是字符串时使用的字符串编码。
buffer
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>- 返回: <Buffer> 带有加密内容的新
Buffer
。
用 key
加密 buffer
的内容,并返回带有加密内容的新 Buffer
。
返回的数据可以使用相应的私钥解密,例如使用 crypto.privateDecrypt()
。
如果 key
不是 KeyObject
,则此函数的行为就像将 key
传给 crypto.createPublicKey()
一样。
如果是对象,则可以传入 padding
属性。
否则,该函数使用 RSA_PKCS1_OAEP_PADDING
。
由于 RSA 公钥可以从私钥派生,因此可以传入私钥而不是公钥。
key
<Object> | <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey>key
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey> A PEM encoded public or private key, <KeyObject>, or <CryptoKey>.oaepHash
<string> The hash function to use for OAEP padding and MGF1. Default:'sha1'
oaepLabel
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> The label to use for OAEP padding. If not specified, no label is used.passphrase
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> An optional passphrase for the private key.padding
<crypto.constants> An optional padding value defined incrypto.constants
, which may be:crypto.constants.RSA_NO_PADDING
,crypto.constants.RSA_PKCS1_PADDING
, orcrypto.constants.RSA_PKCS1_OAEP_PADDING
.encoding
<string> The string encoding to use whenbuffer
,key
,oaepLabel
, orpassphrase
are strings.
buffer
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>- Returns: <Buffer> A new
Buffer
with the encrypted content.
Encrypts the content of buffer
with key
and returns a new
Buffer
with encrypted content. The returned data can be decrypted using
the corresponding private key, for example using crypto.privateDecrypt()
.
If key
is not a KeyObject
, this function behaves as if
key
had been passed to crypto.createPublicKey()
. If it is an
object, the padding
property can be passed. Otherwise, this function uses
RSA_PKCS1_OAEP_PADDING
.
Because RSA public keys can be derived from private keys, a private key may be passed instead of a public key.