-1

I have created a code for compressing and encrypting but now I got to know that createCipher is deprecated I would like to replace with createCipheriv, the code for encryption:

/*Importing necessary modules*/
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
const crypto = require('crypto');

/*driver program*/
const getCipherKey = require('./getCipherKey');

function Encrypt({ file, password }){
// const initVect = crypto.randomBytes(16);
const CIPHER_KEY = getCipherKey(password);
const readStream = fs.createReadStream(file);
const zip = zlib.createGzip();
const cipher = crypto.createCipher('aes256', CIPHER_KEY);
const writeStream =fs.createWriteStream(path.join(file + ".enc"));
readStream
    .pipe(zip)
    .pipe(cipher)
    .pipe(writeStream);
}
Encrypt({ file: './video.mp4', password: 'dogzrgr8' });

for generating key:

const crypto = require('crypto-js');
var sha256 = crypto.algo.SHA256.create();
value=getCipherKey("mySup3rC00lP4ssWord");
function getCipherKey(password) {
    const SHA256= sha256.update(password);
    var hash = SHA256.finalize();
    return hash.toString(crypto.enc.Hex);
  }
module.exports = getCipherKey;

for decryption:

const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');

const getCipherKey = require('./getCipherKey');

function decrypt({ file, password }) {
  // First, get the initialization vector from the file.

  // Once we’ve got the initialization vector, we can decrypt the file.
    const cipherKey = getCipherKey(password);
    const readStream = fs.createReadStream(file);
    const decipher = crypto.createDecipher('aes256', cipherKey);
    const unzip = zlib.createUnzip();
    const writeStream = fs.createWriteStream(file + '.unenc');

    readStream
      .pipe(decipher)
      .pipe(unzip)
      .pipe(writeStream);
}
decrypt({ file: './video.mp4.enc', password: 'dogzrgr8' });

I tried referring multiple sites like link1 , link2 but it is returning error like as follows

internal/crypto/cipher:116 this[kHandle].initiv(cipher, credential, iv, authTagLength); ^ Error: Unknown cipher

burnsi
  • 6,194
  • 13
  • 17
  • 27

1 Answers1

0

You need to provide an IV (initialization vector) as the third argument to crypto.createCipheriv.

function Encrypt({ file, password }){
    const CIPHER_KEY = getCipherKey(password);
    const iv = crypto.randomBytes(16);
    const readStream = fs.createReadStream(file);
    const zip = zlib.createGzip();
    const cipher = crypto.createCipheriv('aes256', CIPHER_KEY, iv);
    const writeStream = fs.createWriteStream(path.join(file + ".enc"));
    writeStream.write(iv);
    readStream
        .pipe(zip)
        .pipe(cipher)
        .pipe(writeStream);
}

To decrypt the file, you need to read the IV from the beginning of the encrypted file and pass it to crypto.createDecipheriv.

function decrypt({ file, password }) {
  const cipherKey = getCipherKey(password);
  const readStream = fs.createReadStream(file);
  const writeStream = fs.createWriteStream(file + '.unenc');
  let iv;
  readStream.once('readable', () => {
    iv = readStream.read(16);
    const decipher = crypto.createDecipheriv('aes256', cipherKey, iv);
    const unzip = zlib.createGunzip();
    readStream
      .pipe(decipher)
      .pipe(unzip)
      .pipe(writeStream);
  });
}