forked from urbashi123/Caesar-Cipher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
86 lines (67 loc) · 2.6 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
80
81
82
83
84
85
86
/**
* Created by Maximilian Lloyd.
*/
$(document).ready(function() {
var alphabet = "abcdefghijklmnopqrstuvwxyz";
$("#encrypt").on("click", function() {
// Retreving the value fields
var input = $("#input");
var key = $("#key").val();
var PlainText = input.val();
PlainText = PlainText.toLowerCase();
key = parseInt(key);
var CipherText = "";
for (var i = 0; i < PlainText.length; i++) {
var char = PlainText.charAt(i);
var index = alphabet.indexOf(char);
var cindex = index + key;
// If it can't find an empty space in the PlainText index is equal to -1. Thus it
// runs when the encryption process runs when it is not equal to 1
if (index !== -1) {
if (cindex >= 26) {
cindex = cindex - 26;
}
CipherText += alphabet.charAt(cindex); // Insert key here when the function is fully implemented
} else {
if (CipherText === " ") {
CipherText += " ";
} else {
CipherText += char;
}
}
}
$(".content").text(CipherText).hide().delay(800).fadeIn();
});
});
$(document).ready(function() {
var alphabet = "zyxwvutsrqponmlkjihgfedcba";
$("#decrypt").on("click", function() {
// Retreving the value fields
var input = $("#input");
var key = $("#key").val();
var PlainText = input.val();
PlainText = PlainText.toLowerCase();
key = parseInt(key);
var CipherText = "";
for (var i = 0; i < PlainText.length; i++) {
var char = PlainText.charAt(i);
var index = alphabet.indexOf(char);
var cindex = index + key;
// If it can't find an empty space in the PlainText index is equal to -1. Thus it
// runs when the encryption process runs when it is not equal to 1
if (index !== -1) {
if (cindex >= 26) {
cindex = 26-cindex;
}
CipherText += alphabet.charAt(cindex); // Insert key here when the function is fully implemented
} else {
if (CipherText === " ") {
CipherText += " ";
} else {
CipherText += char;
}
}
}
$(".content").text(CipherText).hide().delay(800).fadeIn();
});
});