Pranab V V
Pranab V V

Reputation: 1446

Invalid key length issue when trying to do encryption in nodejs

I am trying to do encryption and decryption in nodejs.

encrypt.js

  module.exports=function(crypto) {
        algorithm = 'aes-256-gcm',
        password = 'd6F3Efeq';
        iv = '60iP0h6vJoEa';
        this.testFunc=function(text) {
            var cipher=crypto.createCipheriv(algorithm,password,iv);
            var crypted=cipher.update(text,'utf8','hex');
            crypted +=cipher.final('hex');
            var tag=cipher.getAuthTag();
            return {
                content:encrypted,
                tag:tag
            }
        }
    }

when I call the testFunc() with parameter it shows following error.

Error: Invalid key length
    at new Cipheriv (internal/crypto/cipher.js:139:16)
    at Object.createCipheriv (crypto.js:98:10)
    at module.exports.testFunc (/var/www/html/nodejs/encrypt.js:26:21)
    at /var/www/html/nodejs/routes.js:17:19

I am followed this link to create encryption and decryption in nodejs.

Upvotes: 0

Views: 4936

Answers (1)

Sridhar
Sridhar

Reputation: 11786

Error: Invalid key length

You're getting this error because your key d6F3Efeq is just 8 characters in length. aes-256-gcm requires the key to be 32 characters in length.

var cipher = crypto.createCipheriv(algorithm, 'd6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq', iv);

Upvotes: 2

Related Questions