-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
45 lines (37 loc) · 1.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as crypto from "crypto";
import { Buffer } from "buffer";
import * as readline from "readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
function encrypt(text: string): string {
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
return `${iv.toString("hex")}:${encrypted}`;
}
function decrypt(encryptedText: string): string {
const [ivHex, encrypted] = encryptedText.split(":");
const ivBuffer = Buffer.from(ivHex, "hex");
const decipher = crypto.createDecipheriv("aes-256-cbc", key, ivBuffer);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
function startEncryptionProcess() {
rl.question(
"Digite uma mensagem para cifrar (ou pressione Enter para usar a mensagem padrão): ",
(inputMessage) => {
const message = inputMessage || "Esta é uma mensagem secreta!";
const encryptedMessage = encrypt(message);
console.log("\nMensagem Cifrada:", encryptedMessage);
const decryptedMessage = decrypt(encryptedMessage);
console.log("Mensagem Decifrada:", decryptedMessage);
rl.close();
}
);
}
startEncryptionProcess();