gpt4 book ai didi

javascript - Node JS crypto.createCipheriv 错误 : Invalid key length

转载 作者:行者123 更新时间:2023-12-05 03:47:15 35 4
gpt4 key购买 nike

我正在尝试使用 node.js 加密模块加密文本。

代码如下:

const crypto = require('crypto');

const password = 'password';
const key = crypto.scryptSync(password, 'salt', 24);

const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
var encrypted = cipher.update("Hello", 'utf8', 'hex') + cipher.final('hex');

console.log(encrypted);

我得到以下错误:

internal/crypto/cipher.js:103
this[kHandle].initiv(cipher, credential, iv, authTagLength);
^

Error: Invalid key length
[90m at Cipheriv.createCipherBase (internal/crypto/cipher.js:103:19)[39m
[90m at Cipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)[39m
[90m at new Cipheriv (internal/crypto/cipher.js:225:22)[39m
[90m at Object.createCipheriv (crypto.js:117:10)[39m
at Object.<anonymous> (F:\Misc\App\MySQL-Buzzer-Electron\demo.js:7:23)
[90m at Module._compile (internal/modules/cjs/loader.js:1156:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:1000:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:899:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)[39m

我做错了什么?

最佳答案

你使用了aes-256-gmc你需要使用长度为32的 key 和长度为16的iv

const crypto = require('crypto');

const password = 'password';
const key = crypto.scryptSync(password, 'salt', 32);

const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
var encrypted = cipher.update("Hello", 'utf8', 'hex') + cipher.final('hex');

关于javascript - Node JS crypto.createCipheriv 错误 : Invalid key length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64910819/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com