crypto.createCipheriv(algorithm, key, iv[, options])
algorithm<string>key<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey>iv<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <null>options<Object>stream.transform选项 拥有这些额外的属性:- 返回:Cipheriv
创建并返回一个 Cipheriv 对象,使用给定的 algorithm、key 和初始化向量(iv)。
🌐 Creates and returns a Cipheriv object, with the given algorithm, key and
initialization vector (iv).
options 参数控制特定密码的设置和流行为。
除了在使用 CCM 或 OCB 模式的密码(例如 'aes-128-ccm')时,它是可选的。在这种情况下,authTagLength 选项是必需的,并且指定认证标签的字节长度,参见 CCM 模式。在 GCM 模式下,authTagLength 选项不是必须的,但可以用来设置 getAuthTag() 返回的认证标签长度,默认是 16 字节。
对于 SIV、GCM-SIV 和 chacha20-poly1305,authTagLength 选项默认是 16 字节。SIV 和 GCM-SIV 只支持 16 字节的认证标签。
🌐 The options argument controls cipher-specific settings and stream behavior.
It is optional except when a cipher in CCM or OCB mode (e.g. 'aes-128-ccm')
is used. In that case, the authTagLength option is required and specifies the
length of the authentication tag in bytes, see CCM mode. In GCM mode, the
authTagLength option is not required but can be used to set the length of the
authentication tag that will be returned by getAuthTag() and defaults to 16
bytes.
For SIV, GCM-SIV, and chacha20-poly1305, the authTagLength option
defaults to 16 bytes. SIV and GCM-SIV only support 16-byte authentication
tags.
ctsMode 和 xtsStandard 选项用于配置 OpenSSL 提供程序提供的参数。它们仅在 OpenSSL 3.0 或更高版本以及支持相应参数的提供程序中可用。ctsMode 仅适用于 CBC-CTS 加密算法,xtsStandard 仅适用于 sm4-xts。如果你为可用的加密实现提供了不支持的选项,会抛出 ERR_CRYPTO_UNSUPPORTED_OPERATION 错误。具体内容请参见 CBC-CTS 模式 和 XTS模式。
🌐 The ctsMode and xtsStandard options configure parameters exposed by OpenSSL
providers. They are available only with OpenSSL 3.0 or later and a provider
that supports the corresponding parameter. ctsMode applies only to CBC-CTS
ciphers, and xtsStandard applies only to sm4-xts. Supplying either option
for an available cipher implementation that does not support it throws an
ERR_CRYPTO_UNSUPPORTED_OPERATION error. See CBC-CTS mode and XTS mode
for details.
可用的算法取决于 OpenSSL。crypto.getCiphers() 列出了 Node.js 所提供的算法。在近期的 OpenSSL 版本中,openssl list -cipher-algorithms 会显示 OpenSSL 可用的算法,这些算法可能包括 Node.js 没有提供的算法。
🌐 The available algorithms depend on OpenSSL. crypto.getCiphers() lists the
algorithms exposed by Node.js. On recent OpenSSL releases,
openssl list -cipher-algorithms displays the algorithms available to OpenSSL,
which can include algorithms that Node.js does not expose.
key 是由 algorithm 使用的原始密钥,而 iv 是一个 初始化向量。每一个都可以是字符串、ArrayBuffer、Buffer、TypedArray 或 DataView。字符串 key 使用 options.encoding 解码,默认值是 'utf8';字符串 iv 总是以 UTF-8 解码。key 可以可选是类型为 secret 的 KeyObject。如果密码不需要初始化向量,iv 可以是 null。
🌐 The key is the raw key used by the algorithm and iv is an
initialization vector. Each may be a string, ArrayBuffer, Buffer,
TypedArray, or DataView. A string key is decoded using options.encoding,
which defaults to 'utf8'; a string iv is always decoded as UTF-8. The key
may optionally be a KeyObject of type secret. If the cipher does not
need an initialization vector, iv may be null.
在传递 key 或 iv 的字符串时,请考虑 将字符串用作加密 API 输入时的注意事项。
🌐 When passing strings for key or iv, please consider
caveats when using strings as inputs to cryptographic APIs.
初始化向量的要求取决于算法。对于某些算法,IV必须是不可预测且唯一的;对于其他算法,只要唯一就够了,或者需要一个固定值,或者根本不使用IV。请遵循所选算法的要求。IV通常不需要保密,可以和密文一起传输。
🌐 Initialization vector requirements depend on the algorithm. For some algorithms, an IV must be unpredictable and unique; for others, uniqueness alone is sufficient, a fixed value is required, or no IV is used. Follow the requirements for the selected algorithm. IVs typically do not have to be secret and can be transmitted with the ciphertext.