-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (52 loc) · 1.66 KB
/
index.js
File metadata and controls
57 lines (52 loc) · 1.66 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
51
52
53
54
55
56
57
const readLine = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
const { caeserEncrypt, caeserDecrypt } = require('./algorithms/caeser');
const { viginereEncrypt, viginereDecrypt } = require('./algorithms/vigenere');
const { playfairEncrypt, playfairDecrypt } = require('./algorithms/playfair');
function execute(message, functionName) {
readLine.question(message, input => {
console.log(functionName(input));
readLine.close();
});
return;
}
function runAlgorithm(encryptionFunc, decryptionFunc) {
readLine.question('Enter 0 to encrypt or 1 to decrypt: ', input => {
if (input === '0') {
execute('Enter plain text to encrypt: ', encryptionFunc);
} else if (input === '1') {
execute('Enter cipher text to decrypt: ', decryptionFunc);
} else {
console.log('Enter either 0 or 1');
readLine.close();
}
});
return;
}
console.log('IMPLEMENTED ALGORITHMS');
console.log('[1]. Caeser Cipher');
console.log("[2]. Viginère's Cipher");
console.log('[3]. Playfair Cipher');
readLine.question('Enter encryption algorithm number: ', answer => {
switch (answer) {
// Caeser Cipher
case '1': {
runAlgorithm(caeserEncrypt, caeserDecrypt);
break;
}
// Vigenère's Cipher
case '2': {
runAlgorithm(viginereEncrypt, viginereDecrypt);
break;
}
case '3': {
runAlgorithm(playfairEncrypt, playfairDecrypt);
break;
}
default:
console.log('Invalid cypher name.');
break;
}
});