-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (41 loc) · 1.55 KB
/
script.js
File metadata and controls
50 lines (41 loc) · 1.55 KB
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
46
47
48
49
50
// --- Encrypt Function ---
function encryptText() {
const plaintext = document.getElementById("plaintext").value;
const key = document.getElementById("key").value;
if (key.length !== 24) {
alert("Key must be exactly 24 characters for 3-key 3DES.");
return;
}
const keyHex = CryptoJS.enc.Utf8.parse(key);
const iv = CryptoJS.lib.WordArray.random(8);
const encrypted = CryptoJS.TripleDES.encrypt(plaintext, keyHex, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
const output = {
iv: CryptoJS.enc.Hex.stringify(iv),
ciphertext: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
};
document.getElementById("ciphertext").value = JSON.stringify(output, null, 2);
document.getElementById("decrypted").value = "";
}
// --- Decrypt Function ---
function decryptText() {
try {
const key = document.getElementById("key").value;
const encryptedData = JSON.parse(document.getElementById("ciphertext").value);
const iv = CryptoJS.enc.Hex.parse(encryptedData.iv);
const ciphertext = CryptoJS.enc.Base64.parse(encryptedData.ciphertext);
const keyHex = CryptoJS.enc.Utf8.parse(key);
const decrypted = CryptoJS.TripleDES.decrypt(
{ ciphertext: ciphertext },
keyHex,
{ iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }
);
const plaintext = decrypted.toString(CryptoJS.enc.Utf8);
document.getElementById("decrypted").value = plaintext || "Invalid decryption!";
} catch (err) {
alert("Error decrypting! Ensure ciphertext format is correct.");
}
}