crypto.createDecipheriv(algorithm, key, iv[, options])


创建并返回一个使用给定 algorithm、key 和初始化向量 (iv) 的 Decipheriv 对象。

🌐 Creates and returns a Decipheriv object that uses the given algorithm, key and initialization vector (iv).

options 参数控制特定密码的设置和流的行为。
除非使用 CCM 或 OCB 模式的密码(例如 'aes-128-ccm'),否则它是可选的。在这种情况下,必须使用 authTagLength 选项,并指定认证标签的字节长度,详见 CCM 模式。对于 GCM 和 chacha20-poly1305,authTagLength 选项默认为 16 字节,如果使用不同长度,则必须设置。对于 SIV 和 GCM-SIV,authTagLength 选项默认为 16 字节,并且只支持 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. For GCM and chacha20-poly1305, the authTagLength option defaults to 16 bytes and must be set if a different length is used. For SIV and GCM-SIV, the authTagLength option defaults to 16 bytes and only 16-byte authentication tags are supported.

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.