Get your own Node server
const crypto = require('crypto');
const fs = require('fs');

// Generate a sample key and IV for this example
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

// Save the key and IV for decryption
fs.writeFileSync('encryption_key.txt', key.toString('hex'));
fs.writeFileSync('encryption_iv.txt', iv.toString('hex'));

// Create a sample file to encrypt
const sampleData = Buffer.from('This is a sample file that will be encrypted and then decrypted', 'utf8');
fs.writeFileSync('sample.txt', sampleData);

// Encrypt the file
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const input = fs.createReadStream('sample.txt');
const output = fs.createWriteStream('sample.txt.enc');
input.pipe(cipher).pipe(output);

output.on('finish', () => {
  console.log('File encrypted successfully');
  
  // Now decrypt the file
  const readStream = fs.createReadStream('sample.txt.enc');
  const writeStream = fs.createWriteStream('decrypted.txt');
  
  // Create decipher stream
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  
  // Decrypt the file
  readStream
    .pipe(decipher)
    .pipe(writeStream);
  
  writeStream.on('finish', () => {
    const decryptedContent = fs.readFileSync('decrypted.txt', 'utf8');
    console.log('File decrypted successfully');
    console.log('Original content:', sampleData.toString('utf8'));
    console.log('Decrypted content:', decryptedContent);
    
    // Clean up temporary files
    fs.unlinkSync('sample.txt');
    fs.unlinkSync('sample.txt.enc');
    fs.unlinkSync('decrypted.txt');
    fs.unlinkSync('encryption_key.txt');
    fs.unlinkSync('encryption_iv.txt');
  });
});

              
File encrypted successfully
File decrypted successfully
Original content: This is a sample file that will be encrypted and then decrypted
Decrypted content: This is a sample file that will be encrypted and then decrypted