getting TypeError: Invalid initialization vector while using crypto module in js

3.5k views Asked by At
this[kHandle].initiv(cipher, credential, iv, authTagLength);

                  ^
TypeError: Invalid initialization vector
    at Cipheriv.createCipherBase (node:internal/crypto/cipher:122:19)
    at Cipheriv.createCipherWithIV (node:internal/crypto/cipher:141:3)
    at new Cipheriv (node:internal/crypto/cipher:249:3)      
    at Object.createCipheriv (node:crypto:141:10)
    at Object.<anonymous> (C:\Users\vivek\Desktop\BlockChain\SpringBoard\crypto\index.js:8:19)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'ERR_CRYPTO_INVALID_IV'
}

Node.js v18.16.0

my code

let crypto=require('crypto')
const fs = require("fs");

var myKey = fs.readFileSync("private.pem", "utf8").replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "").trim().toString();

const iv = crypto.randomBytes(8);

var cipher=crypto.createCipheriv("aes-256-cbc",Buffer.from(myKey),iv);
var encrypt=cipher.update("hello world","utf-8","hex");
encrypt+=cipher.final("hex");

console.log(encrypt);

i was trying to encrypt a message

0

There are 0 answers