最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS crypto.createCipheriv Error: Invalid key length - Stack Overflow

programmeradmin2浏览0评论

I am trying to encrypt text in using node.js crypto module.

Here is code:

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);

And I get following error:

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._pile (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

What am I doing wrong ?

I am trying to encrypt text in using node.js crypto module.

Here is code:

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);

And I get following error:

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._pile (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

What am I doing wrong ?

Share Improve this question asked Nov 19, 2020 at 11:32 Parth AgrawalParth Agrawal 431 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

you used aes-256-gmc you need to use key length of 32 and iv of length 16

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');
发布评论

评论列表(0)

  1. 暂无评论