-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
75 lines (57 loc) · 2.22 KB
/
main.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
const alphabetic='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const form = document.forms[0];
const operationRadios = form.elements['operation'] //returns nodeRadioList
const inputField = document.getElementById('input')
const outputField = document.getElementById('output')
const offsetField = document.getElementById('offset')
inputField.addEventListener('input',runChosenOperation)
offsetField.addEventListener('change',runChosenOperation)
for (const radio of operationRadios) radio.addEventListener("change",runChosenOperation)
function encrypt(plaintext, offset) {
plaintext = plaintext.replace(/\s/g, "")// remove spaces
plaintext = plaintext.toUpperCase(); // put all letters to uppercase
let ciphertext="";
for (const letter of plaintext) {
const letterNumber =alphabetic.indexOf(letter)
if(letterNumber===-1) {
ciphertext+=letter // not an alphabetical letter (add it to string without any modification)
continue; //skip this iteration
}
ciphertext += alphabetic[(letterNumber + offset) % alphabetic.length]
}
return ciphertext;
}
function decrypt(ciphertext,offset){
ciphertext = ciphertext.toUpperCase();
let plaintext="";
for (const letter of ciphertext) {
const letterNumber = alphabetic.indexOf(letter)
if(letterNumber===-1) {
plaintext+=letter // not an alphabetical letter (add it to string without any modification)
continue; //skip this iteration
}
let newLetter = (letterNumber - offset) % alphabetic.length
if(newLetter < 0) newLetter += alphabetic.length
plaintext += alphabetic[newLetter]
}
return plaintext;
}
function runChosenOperation(){
const offset = parseInt(offsetField.value)
const input = inputField.value
const operation = operationRadios.value
let output
if(output = operation==="encrypt"){
output=encrypt(input,offset)
const inputLabel = document.querySelectorAll('.card-title')
inputLabel[0].innerText = "Plain text:"
inputLabel[1].innerText = "Cipher Text:"
}
else{
output=decrypt(input,offset)
const inputLabel = document.querySelectorAll('.card-title')
inputLabel[0].innerText = "Cipher text:"
inputLabel[1].innerText = "Plain Text:"
}
outputField.innerText = output
}