Symmetric Key Encryption Methods (AES, ARIA, Blowfish, Camellia, ChaCha20, Cast, DES, ...)In this case we will create cipher text using a random key and a random IV value. |
Outline
The code used is:
const crypto = require("crypto"); var message="Hello"; var algorithm="aes-128-xts"; var args = process.argv; if (args.length>2) message=args[2]; if (args.length>3) algorithm=args[3]; var key=crypto.randomBytes(16); var iv = crypto.randomBytes(16); var info=crypto.getCipherInfo(algorithm); key=crypto.randomBytes(info.keyLength); iv=crypto.randomBytes(info.ivLength); if (algorithm.indexOf("ecb")>-1) { iv = null; } const cipher = crypto.createCipheriv(algorithm, Buffer.from(key),iv,{ authTagLength: 16 }); let encrypted = cipher.update(message); encrypted = Buffer.concat([encrypted, cipher.final()]); var decipher; var decrypted; decipher = crypto.createDecipheriv(algorithm, Buffer.from(key),iv,{ authTagLength: 16 }); if (algorithm=="aes-128-gcm" || algorithm=="aes-128-ocb" || algorithm=="aes-128-ccm" || algorithm=="aes-256-gcm" || algorithm=="aes-256-ocb" || algorithm=="aes-256-ccm" ) { const tag = cipher.getAuthTag(); decipher.setAuthTag(tag); } decrypted = decipher.update(encrypted); decrypted = Buffer.concat([decrypted, decipher.final()]); console.log("Message: ",message); console.log("Algorithm: ",algorithm); console.log("Key ",key.toString('hex')); console.log("Key size (Bytes): ",info.keyLength); console.log("\nIV size (Bytes): ",info.ivLength); if (iv!=null) console.log("IV: ",iv.toString('hex')); console.log("\nEncrypted: ",encrypted.toString('hex')); console.log("Encrypted: ",encrypted.toString('base64')); console.log("Decrypted: ",decrypted.toString());