-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
80 lines (71 loc) · 2.74 KB
/
script.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
// script.js
document.addEventListener('DOMContentLoaded', () => {
const resultText = document.getElementById('resultText');
const encryptBtn = document.getElementById('encryptBtn');
const inputText = document.getElementById('inputText');
// Function to perform encryption based on selected cipher
function encryptText(text, cipher) {
switch (cipher) {
case 'mono':
return monoalphabeticCipher(text);
case 'poly':
return polyalphabeticCipher(text);
case 'playfair':
return playfairCipher(text);
case 'hill':
return hillCipher(text);
default:
return 'Invalid cipher selected.';
}
}
// Example implementations of ciphers
function monoalphabeticCipher(text) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const shiftedAlphabet = 'QWERTYUIOPASDFGHJKLZXCVBNM';
let encrypted = '';
for (let char of text.toUpperCase()) {
if (alphabet.includes(char)) {
encrypted += shiftedAlphabet[alphabet.indexOf(char)];
} else {
encrypted += char;
}
}
return encrypted;
}
function polyalphabeticCipher(text) {
// Simplified Vigenère Cipher example
const key = 'KEY';
let encrypted = '';
for (let i = 0; i < text.length; i++) {
const charCode = text.charCodeAt(i);
const keyCode = key[i % key.length].charCodeAt(0) - 'A'.charCodeAt(0);
encrypted += String.fromCharCode(((charCode - 65 + keyCode) % 26) + 65);
}
return encrypted.toUpperCase();
}
function playfairCipher(text) {
return 'Playfair cipher not implemented yet!';
}
function hillCipher(text) {
return 'Hill cipher not implemented yet!';
}
// Event listener for the encrypt button
encryptBtn.addEventListener('click', () => {
const text = inputText.value.trim();
const cipherType = document.querySelector('.selected-cipher')?.dataset.type || 'mono'; // Default to mono
if (text) {
const encryptedText = encryptText(text, cipherType);
resultText.textContent = `Encrypted Text: ${encryptedText}`;
} else {
resultText.textContent = 'Please enter text to encrypt.';
}
});
// Event listener for cipher selection
const cipherOptions = document.querySelectorAll('.substitution-option');
cipherOptions.forEach(option => {
option.addEventListener('click', () => {
cipherOptions.forEach(opt => opt.classList.remove('selected-cipher'));
option.classList.add('selected-cipher');
});
});
});