/
vigenesque.html
172 lines (152 loc) · 6.84 KB
/
vigenesque.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title></title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
input[type="text"] {
font-family: Consolas, Monaco, Lucida Console, Liberation Mono,
DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New,
monospace;
font-size: 1em;
}
</style>
<script type="text/javascript">
var numberspace = "0123456789";
var characterspace = "abcdefghijklmnopqrstuvwxyz" + numberspace;
var characterspacelength = characterspace.length;
var numberspacelength = numberspace.length;
function wrapIndex(index, numbersonly = false) {
wrappedindex = index;
if (numbersonly) maxindex = numberspacelength;
else maxindex = characterspacelength;
while (wrappedindex < 0) wrappedindex += maxindex;
return wrappedindex % maxindex;
}
function getCharacter(index, numbersonly = false) {
return numbersonly
? numberspace.charAt(index)
: characterspace.charAt(index);
}
function getCharacterIndex(character, numbersonly = false) {
return numbersonly
? numberspace.indexOf(character)
: characterspace.indexOf(character);
}
function isSpace(character) {
return character == " ";
}
function isValidCipher(cipher) {
isvalid = true;
for (var i = 0; i < cipher.length; i++) {
cipherchar = cipher.charAt(i);
if (!characterspace.includes(cipherchar)) isvalid = false;
}
return isvalid;
}
function isValidCharacter(character, numbersonly = false) {
return (
(numbersonly && numberspace.includes(character)) ||
(!numbersonly && characterspace.includes(character)) ||
isSpace(character)
);
}
function computeCipher(
inputtext,
cipher,
decrypt = false,
numbersonly = false
) {
operation = decrypt ? -1 : 1;
inputerrortext = decrypt ? "Encrypted code" : "Backup code";
ciphertext = "";
if (!inputtext.length || !cipher.length) {
alert(inputerrortext + " and password are required.");
return "";
}
inputtext = inputtext.toLowerCase();
cipher = cipher.toLowerCase();
if (isValidCipher(cipher)) {
for (var i = 0, skipspaces = 0; i < inputtext.length; i++) {
inputtextchar = inputtext.charAt(i + skipspaces);
// Spaces pass through without applying the cipher
while (isSpace(inputtextchar) && i < inputtext.length) {
ciphertext += inputtextchar;
skipspaces++;
inputtextchar = inputtext.charAt(i + skipspaces);
}
// Loop through the cipher, if it's not as long as the plaintext. Note,
// a repeating cipher is not the most robust cryptographic protection.
if (i + skipspaces < inputtext.length) {
cipherchar = cipher.charAt(i % cipher.length);
if (isValidCharacter(inputtextchar, numbersonly)) {
ciphertext += getCharacter(
wrapIndex(
getCharacterIndex(
inputtextchar,
numbersonly
) +
operation *
getCharacterIndex(cipherchar),
numbersonly
),
numbersonly
);
} else {
alert(
inputerrortext +
" can only contain spaces and numbers" +
(!numbersonly ? " and letters." : ".")
);
return "";
}
}
}
} else {
alert("Password can only contain numbers and letters.");
}
return ciphertext.toUpperCase();
}
</script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">
You are using an <strong>outdated</strong> browser. Please
<a href="#">upgrade your browser</a> to improve your experience.
</p>
<![endif]-->
<form action="">
<label for="cipher"
>Password (key used to encrypt or decrypt):</label
>
<input type="text" id="cipher" name="cipher" /><br /><br />
<label for="plaintext">Backup Code (to encrypt):</label>
<input type="text" id="plaintext" name="plaintext" />
<input
type="button"
value="Encrypt"
onclick="document.getElementById('ciphertext').value = computeCipher(document.getElementById('plaintext').value,
document.getElementById('cipher').value,
false);"
/><br /><br />
<label for="ciphertext">Encrypted Code (to decrypt):</label>
<input type="text" id="ciphertext" name="ciphertext" />
<input
type="button"
value="Decrypt"
onclick="document.getElementById('plaintext').value = computeCipher(document.getElementById('ciphertext').value,
document.getElementById('cipher').value,
true);"
/>
</form>
</body>
</html>