forked from ga-wdi-exercises/vignere_cipher
/
cipher.js
97 lines (96 loc) · 3.34 KB
/
cipher.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
92
93
94
95
96
97
var cipher = {
//adding extra character to handle spaces
"alphabet": "abcdefghijklmnopqrstuvwxyz ",
"encode": function(message, keyword) {
var self = this;
var cipherIndex = [];
var ciphertext = "";
var newKey = "";
var repeatKeyFull = parseInt(message.length / keyword.length);
var repeatKeyPartial = message.length % keyword.length;
if (repeatKeyFull >= 1) {
for (var i=0; i<repeatKeyFull; i++) {
newKey += keyword;
}
for (var i=0; i<repeatKeyPartial; i++) {
newKey += keyword.charAt(i);
}
}
else {
newKey = keyword;
}
for (i=0; i<message.length; i++) {
var cipherNum = self.alphabet.indexOf(message.charAt(i)) + self.alphabet.indexOf(newKey.charAt(i))
cipherIndex.push(cipherNum % self.alphabet.length);
}
for (var i in cipherIndex) {
ciphertext += self.alphabet.charAt(cipherIndex[i]);
}
return ciphertext;
},
"decode": function(message, keyword) {
var self = this;
var cipherIndex = [];
var ciphertext = "";
var newKey = "";
var repeatKeyFull = parseInt(message.length / keyword.length);
var repeatKeyPartial = message.length % keyword.length;
if (repeatKeyFull >= 1) {
for (var i=0; i<repeatKeyFull; i++) {
newKey += keyword;
}
for (var i=0; i<repeatKeyPartial; i++) {
newKey += keyword.charAt(i);
}
}
else {
newKey = keyword;
}
console.log(newKey);
for (i=0; i<message.length; i++) {
var cipherNum = self.alphabet.indexOf(message.charAt(i)) - self.alphabet.indexOf(newKey.charAt(i))
cipherIndex.push((cipherNum + self.alphabet.length) % self.alphabet.length);
}
console.log(cipherNum)
console.log(cipherIndex)
for (var i in cipherIndex) {
ciphertext += self.alphabet.charAt(cipherIndex[i]);
}
console.log(ciphertext)
return ciphertext;
},
"makeSecret": function(message, keyword) {
var secretMessage = this.encode(message, keyword);
return secretMessage;
},
"revealSecret": function(message, keyword) {
var secretMessage = this.decode(message, keyword);
return secretMessage;
}
}
var app = {
"encodeMessage": document.getElementById("encodeMessage"),
"encodeKeyword": document.getElementById("encodeKeyword"),
"decodeMessage": document.getElementById("decodeMessage"),
"decodeKeyword": document.getElementById("decodeKeyword"),
"encodeButton": document.getElementById("encodeButton"),
"decodeButton": document.getElementById("decodeButton"),
"encodeForm": document.getElementById("encode"),
"decodeForm": document.getElementById("decode"),
"run": function(){
this.encodeButton.addEventListener("click", function() {
var secretMessage = cipher.makeSecret(this.encodeMessage.value, this.encodeKeyword.value);
var keyword = this.encodeKeyword.value;
this.encodeForm.reset();
alert("Here's your encoded message: " + secretMessage +
"\n\nHere's your keyword: " + keyword +
"\n\nRemember your keyword, you need it to decode the message.");
}.bind(this));
this.decodeButton.addEventListener("click", function() {
var secretMessage = cipher.revealSecret(this.decodeMessage.value, this.decodeKeyword.value);
this.decodeForm.reset();
alert("Here's your decoded message: " + secretMessage);
}.bind(this));
}
}
app.run();