我有一些使用 SubtleCrypto 的代码来加密密钥并将其存储在数据库中。
对于另一个功能,我必须能够在 Node 12 中对其进行加密。如果没有 SubtleCrypto,我必须在 Crypto 中重新创建该功能。
我得到相同大小的输出,但它似乎无法被 SubtleCrypto 解密,我试图找出哪里出了问题。
这是在浏览器上使用 SubtleCrypto 运行的代码:
key = getKeyMaterial(password)];
salt = base64ToArraybuffer(optionalSalt)
aesKey = crypto.subtle.deriveKey(
{
name: constants.algorithms.pbkdf2,
salt: salt,
iterations: pbkdf2Iterations,
hash: { name: constants.hash.sha256 },
},
key,
{
name: constants.algorithms.aesGcm,
length: aesWrapKeyBitsLength,
},
true,
['wrapKey', 'unwrapKey']
),
return {
salt: arraybufferTobase64(salt),
aesKey: aesKey,
}
function getKeyMaterial(password) {
var enc = new TextEncoder();
crypto.subtle.importKey(
constants.format.raw,
enc.encode(password),
{
name: constants.algorithms.pbkdf2,
},
false,
['deriveKey']
)
Run Code Online (Sandbox Code Playgroud)
如果没有 SubtleCrypto,在 Node 12 中,我被迫使用 Crypto …