Skip to content

Instantly share code, notes, and snippets.

@kevnnard
Last active July 5, 2023 02:36
Show Gist options
  • Save kevnnard/420bdad9a94953f626c5c9b622904087 to your computer and use it in GitHub Desktop.
Save kevnnard/420bdad9a94953f626c5c9b622904087 to your computer and use it in GitHub Desktop.
Crypto Node Js
const crypto = require('crypto');
const claveString = 'MiClaveSecreta123';
const ivString = '1234567890123456'; // IV de 16 bytes (128 bits)
const textoOriginal = 'Hola, mundo!';
// Generar una clave a partir del string
const clave = crypto.createHash('sha256').update(claveString).digest();
// Crear un buffer a partir del IV string
const iv = Buffer.from(ivString, 'utf8');
// Crear un objeto de cifrado
const cifrador = crypto.createCipheriv('aes-256-cbc', clave, iv);
// Cifrar el texto original
let textoCifrado = cifrador.update(textoOriginal, 'utf8', 'hex');
textoCifrado += cifrador.final('hex');
console.log('Texto cifrado:', textoCifrado);
// Crear un objeto de descifrado
const descifrador = crypto.createDecipheriv('aes-256-cbc', clave, iv);
// Descifrar el texto cifrado
let textoDescifrado = descifrador.update(textoCifrado, 'hex', 'utf8');
textoDescifrado += descifrador.final('utf8');
console.log('Texto descifrado:', textoDescifrado);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment