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