-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathvigenere.js
91 lines (83 loc) · 3.12 KB
/
vigenere.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Cleans the input text by removing spaces and ensuring it contains only alphanumeric characters.
* @param {string} text - The input text to be cleaned.
* @returns {string} The cleaned text without spaces or special characters.
* @throws Will throw an error if special characters are found.
*/
function cleanText(text) {
const textNoSpaces = text.replace(/ /g, "");
if (/[^A-Za-z0-9]/.test(textNoSpaces)) {
throw new Error("Special characters detected. They must be removed.");
}
return textNoSpaces;
}
/**
* Encrypts a message using a key with a substitution cipher.
* @param {string} message - The message to encrypt.
* @param {string} key - The encryption key.
* @returns {string} The encrypted message.
*/
function encrypt(message, key) {
message = message.toLowerCase();
key = key.toLowerCase();
const combinedAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
const n = combinedAlphabet.length;
const encrypted = [];
let keyIndex = 0;
for (const char of message) {
if (combinedAlphabet.includes(char)) {
const mIndex = combinedAlphabet.indexOf(char);
const keyChar = key[keyIndex % key.length];
const shift = combinedAlphabet.indexOf(keyChar);
const newIndex = (mIndex + shift) % n;
encrypted.push(combinedAlphabet[newIndex]);
keyIndex++;
}
}
return encrypted.join("");
}
/**
* Decrypts an encrypted message using a key with a substitution cipher.
* @param {string} cipherText - The encrypted message to decrypt.
* @param {string} key - The decryption key.
* @returns {string} The decrypted message.
*/
function decrypt(cipherText, key) {
cipherText = cipherText.toLowerCase();
key = key.toLowerCase();
const combinedAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
const n = combinedAlphabet.length;
const decrypted = [];
let keyIndex = 0;
for (const char of cipherText) {
if (combinedAlphabet.includes(char)) {
const cIndex = combinedAlphabet.indexOf(char);
const keyChar = key[keyIndex % key.length];
const shift = combinedAlphabet.indexOf(keyChar);
const originalIndex = (cIndex - shift + n) % n;
decrypted.push(combinedAlphabet[originalIndex]);
keyIndex++;
}
}
return decrypted.join("");
}
/**
* The main function to clean, encrypt, and decrypt messages.
*/
function main() {
try {
const rawMessage = prompt("Enter the message: ");
const rawKey = prompt("Enter the key: ");
const cleanedMessage = cleanText(rawMessage);
const cleanedKey = cleanText(rawKey);
if (!cleanedMessage) throw new Error("Message is empty after cleaning!");
if (!cleanedKey) throw new Error("Key is empty after cleaning!");
const encryptedMsg = encrypt(cleanedMessage, cleanedKey);
alert(`Encrypted Message: ${encryptedMsg}`);
const decryptedMsg = decrypt(encryptedMsg, cleanedKey);
alert(`Decrypted Message: ${decryptedMsg}`);
} catch (e) {
alert(`An error occurred: ${e.message}`);
}
}
main();