-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js_backup
77 lines (63 loc) · 3.34 KB
/
javascript.js_backup
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
function addListeners(){
var button_encrypt = document.getElementById('button_encrypt');
var button_decrypt = document.getElementById('button_decrypt');
var output_text = document.getElementById('output_text');
var output_keyword = document.getElementById('output_keyword');
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(window.addEventListener) {
button_encrypt.addEventListener("click", encrypt, false);
button_decrypt.addEventListener("click", decrypt, false);
}
function encrypt(){
var keyword = document.getElementById('keyword').value;
var message = document.getElementById('message').value;
var alphabetlength=alphabet.length;
var keyword_repeat = keyword.repeat(Math.floor(message.length/keyword.length)) + keyword.slice(0,message.length%keyword.length);
var message_array = Array.from(message);
output_keyword.value = keyword_repeat.toUpperCase();
message_array.forEach(encrypt_letter);
output_text.innerHTML = message_array.join("");
function encrypt_letter(letter, index, calling_array){
var letterposition = alphabet.indexOf(letter.toUpperCase());
var key = keyword_repeat.charAt(index)
var keyposition = alphabet.indexOf(key.toUpperCase())
if(keyposition + letterposition < alphabet.length){
console.log(letter + "(" + letterposition + ") with key " + key + "(" + keyposition + ") becomes " + alphabet.charAt(keyposition + letterposition));
calling_array[index] = alphabet.charAt(keyposition + letterposition);
}else{
console.log(letter + "(" + letterposition + ") with key " + key + "(" + keyposition + ") becomes " + alphabet.charAt((keyposition + letterposition) - alphabet.length));
calling_array[index] = alphabet.charAt((keyposition + letterposition) - alphabet.length);
}
}
}
function decrypt(){
var keyword = document.getElementById('keyword').value;
var message = document.getElementById('message').value;
var alphabetlength=alphabet.length;
var keyword_repeat = keyword.repeat(Math.floor(message.length/keyword.length)) + keyword.slice(0,message.length%keyword.length);
var message_array = Array.from(message);
output_keyword.value = keyword_repeat.toUpperCase();
message_array.forEach(decrypt_letter);
output_text.innerHTML = message_array.join("");
function decrypt_letter(letter, index, calling_array){
// 1. take key accoring to index
var key = keyword_repeat.charAt(index)
// 2. get encrypted character position (message array [index])
var letterposition = alphabet.indexOf(letter.toUpperCase());
var keyposition = alphabet.indexOf(key.toUpperCase())
if(keyposition >= letterposition){
var alphabetposition = ((alphabet.length + (letterposition - keyposition)) % alphabet.length);
// alphabet.length - keyposition + letterposition)
console.log(">= " + letter + "(" + letterposition + ") with key " + key
+ "(" + keyposition + ") becomes "
+ alphabet.charAt(alphabetposition) + "(" + alphabetposition + ")");
calling_array[index] = alphabet.charAt(alphabetposition);
}else{
var alphabetposition = letterposition - keyposition;
console.log("else " + letter + "(" + letterposition + ") with key " + key + "(" + keyposition + ") becomes " + alphabet.charAt(alphabetposition) + "(" + alphabetposition + ")");
calling_array[index] = alphabet.charAt(alphabetposition);
}
}
}
}
window.onload = addListeners;