-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymmetric.js
More file actions
21 lines (14 loc) · 799 Bytes
/
symmetric.js
File metadata and controls
21 lines (14 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const { createCipheriv, randomBytes, createDecipheriv } = require('crypto');
/// Cipher
const message = 'i like turtles';
const key = randomBytes(32); console.log(key, "key");
const iv = randomBytes(16); console.log(iv, "iv");
//This uses the aes256 encryption algorithm
const cipher = createCipheriv('aes256', key, iv); console.log(cipher, "cipher")
/// Encrypt
const encryptedMessage = cipher.update(message, 'utf8', 'hex') + cipher.final('hex');
console.log(`Encrypted: ${encryptedMessage}`, "Encrypted data");
/// Decrypt
const decipher = createDecipheriv('aes256', key, iv); console.log(decipher, "decipher");
const decryptedMessage = decipher.update(encryptedMessage, 'hex', 'utf-8') + decipher.final('utf8');
console.log(`Deciphered: ${decryptedMessage.toString('utf-8')}`, decipher);