-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_time_pad.html
357 lines (308 loc) · 15.7 KB
/
one_time_pad.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>One Time Pad (OTP)</title>
<style>
.all_text_areas_wrapper {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.copy_button {
margin-left: 1em;
}
h2 {
font-size: 12pt;
display: inline;
}
textarea {
display: block;
max-width: 97%;
}
fieldset {
display: inline-block;
}
#action_status {
color: darkgreen;
font-size: 11pt;
font-weight: normal;
opacity: 0;
user-select: none;
}
@keyframes FadeOut {
/* For animation for copy status text */
from { opacity: 1; }
to { opacity: 0; }
}
</style>
<script>
function ClearAll() {
document.getElementById('unencrypted_area').value = '';
document.getElementById('one_time_pad_area').value = '';
document.getElementById('encrypted_area').value = '';
}
function RemoveNewlines(inputString){
// Regex might be slightly more efficient, but this feels more readable
return inputString
.replaceAll("\r","")
.replaceAll("\n", "");
}
function ShowSuccessMessage(messageText) {
var element = document.getElementById('action_status');
element.textContent = messageText;
element.style.color = 'darkgreen';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 3.14s";
}
function ShowFailureMessage(messageText) {
var element = document.getElementById('action_status');
element.textContent = messageText;
element.style.color = 'darkred';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 5s";
}
function ShowWarningMessage(messageText) {
var element = document.getElementById('action_status');
element.textContent = messageText;
element.style.color = 'darkorange';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 10s";
}
function CopyUnencrypted() {
var content = document.getElementById('unencrypted_area').value;
ClipboardUtilities.CopyToClipboard(content);
}
function CopyOneTimePad() {
var content = document.getElementById('one_time_pad_area').value;
ClipboardUtilities.CopyToClipboard(content);
}
function CopyEncrypted() {
var content = document.getElementById('encrypted_area').value;
ClipboardUtilities.CopyToClipboard(content);
}
ClipboardUtilities = function () {
function CopyToClipboard(textToCopy) {
// Support for non-navigator clipboard copying when not over https:// or file:// based on https://stackoverflow.com/a/65996386/2890086
// navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method'
return navigator
.clipboard
.writeText(textToCopy)
.then(
ShowClipboardCopySuccessMessage,
ShowClipboardCopyFailureMessage
);
} else {
// text area method
console.log("Using text area with exec");
textArea.value = textToCopy;
// make the textarea out of viewport
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise(
(res, rej) => {
// here the magic happens
document.execCommand('copy') ? res() : rej();
textArea.remove();
}).then(
ShowClipboardCopySuccessMessage,
ShowClipboardCopyFailureMessage
);
}
}
const ShowClipboardCopySuccessMessage = function () {
var element = document.getElementById('action_status');
element.textContent = 'Copied to clipboard';
element.style.color = 'darkgreen';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 3.14s";
}
const ShowClipboardCopyFailureMessage = function () {
var element = document.getElementById('action_status');
element.textContent = 'Failed to copy to clipboard';
element.style.color = 'darkred';
element.style.animation = 'none';
element.offsetHeight; /* trigger reflow */
element.style.animation = "FadeOut ease 3.14s";
}
/* Expose functions needed outside module*/
return {
CopyToClipboard:CopyToClipboard,
}
}();
function GetUnencryptedAsUint8Array() {
var unencrypted = document.getElementById('unencrypted_area').value;
var enc = new TextEncoder(); // always utf-8
var unencryptedAsUint8Array = enc.encode(unencrypted);
return unencryptedAsUint8Array;
}
function GenerateOneTimePad() {
var unencryptedBytes = GetUnencryptedAsUint8Array();
// console.log("Length is: ", unencryptedBytes);
var randomBytes = new Uint8Array(unencryptedBytes.length);
// console.log("Random bytes are: ", randomBytes);
crypto.getRandomValues(randomBytes);
// console.log("Random bytes are: ", randomBytes);
var randomBytesBase64 = bytesToBase64(randomBytes);
document.getElementById('one_time_pad_area').value = randomBytesBase64;
}
function EncryptWithGeneratedOtp() {
var unencryptedAsUint8Array = GetUnencryptedAsUint8Array();
if(unencryptedAsUint8Array.length == null || unencryptedAsUint8Array.length < 1) {
ShowFailureMessage("Unencrypted input is required");
throw new Error("Unencrypted input is required");
}
GenerateOneTimePad();
var otpBase64String = document.getElementById('one_time_pad_area').value;
var otpUint8Array = base64ToBytes(otpBase64String);
if(otpUint8Array.length != unencryptedAsUint8Array.length){
throw new Error("Unencrypted input and OTP length don't match, something is awfully wrong");
}
var encryptedAsUint8Array = new Uint8Array(unencryptedAsUint8Array.length);
for (let i = 0; i < unencryptedAsUint8Array.length; i++) {
encryptedAsUint8Array[i] = unencryptedAsUint8Array[i] ^ otpUint8Array[i];
console.assert(encryptedAsUint8Array[i] == (unencryptedAsUint8Array[i] ^ otpUint8Array[i]), "result of xor (32 bit int) did not match what was put in uint8 array.");
}
var encryptedAsBase64String = bytesToBase64(encryptedAsUint8Array);
document.getElementById('encrypted_area').value = encryptedAsBase64String;
}
function EncryptWithSuppliedOtp() {
// var unencrypted = document.getElementById('unencrypted_area').value;
// var enc = new TextEncoder(); // always utf-8
// var unencryptedAsUint8Array = enc.encode(unencrypted);
var unencryptedAsUint8Array = GetUnencryptedAsUint8Array();
if(unencryptedAsUint8Array.length == null || unencryptedAsUint8Array.length < 1) {
ShowFailureMessage("Unencrypted input is required");
throw new Error("Unencrypted input is required");
}
var otpBase64String = document.getElementById('one_time_pad_area').value;
if(otpBase64String.length == null || otpBase64String.length < 1) {
ShowFailureMessage("OTP is required");
throw new Error("OTP is required");
}
var otpUint8Array = base64ToBytes(otpBase64String);
if(otpUint8Array.length != unencryptedAsUint8Array.length) {
if(otpUint8Array.length > unencryptedAsUint8Array.length) {
console.warn(`OTP length (${otpUint8Array.length} bytes) is longer than unencrypted message, only using first ${unencryptedAsUint8Array.length} bytes of OTP.`);
ShowWarningMessage(`OTP length (${otpUint8Array.length}) is longer than unencrypted message, only using first ${unencryptedAsUint8Array.length} bytes of OTP.`);
} else {
ShowFailureMessage("OTP must be same size as unencrypted message or longer.");
throw new Error("OTP must be same size as unencrypted message or longer.");
}
}
var encryptedAsUint8Array = new Uint8Array(unencryptedAsUint8Array.length);
for (let i = 0; i < unencryptedAsUint8Array.length; i++) {
encryptedAsUint8Array[i] = unencryptedAsUint8Array[i] ^ otpUint8Array[i];
console.assert(encryptedAsUint8Array[i] == (unencryptedAsUint8Array[i] ^ otpUint8Array[i]), "result of xor (32 bit int) did not match what was put in uint8 array.");
}
var encryptedAsBase64String = bytesToBase64(encryptedAsUint8Array);
document.getElementById('encrypted_area').value = encryptedAsBase64String;
}
function DecryptWithOtp() {
var encryptedB64 = document.getElementById('encrypted_area').value;
var encryptedAsUint8Array = base64ToBytes(encryptedB64);
if(encryptedAsUint8Array.length == null || encryptedAsUint8Array.length < 1) {
ShowFailureMessage("Encrypted input is required");
throw new Error("Encrypted input is required");
}
var otpBase64String = document.getElementById('one_time_pad_area').value;
if(otpBase64String.length == null || otpBase64String.length < 1) {
ShowFailureMessage("OTP not supplied");
throw new Error("OTP not supplied");
}
// var otpUint8Array = Base64Utilities.base64DecToArr(otpBase64String);
var otpUint8Array = base64ToBytes(otpBase64String);
if(otpUint8Array.length != encryptedAsUint8Array.length){
ShowFailureMessage("Encrypted input and OTP length don't match");
throw new Error("Encrypted input and OTP length don't match");
}
var unencryptedAsUint8Array = new Uint8Array(encryptedAsUint8Array.length);
for (let i = 0; i < encryptedAsUint8Array.length; i++) {
unencryptedAsUint8Array[i] = encryptedAsUint8Array[i] ^ otpUint8Array[i];
console.assert(unencryptedAsUint8Array[i] == (encryptedAsUint8Array[i] ^ otpUint8Array[i]), `DecryptWithOtp (iteration ${i}): result of xor (32 bit int) did not match what was put in uint8 array. When XOR ing encrypted ${encryptedAsUint8Array[i]} with OTP ${otpUint8Array[i]} get ${encryptedAsUint8Array[i] ^ otpUint8Array[i]}, but array contains ${encryptedAsUint8Array[i]}`);
}
var decoder = new TextDecoder("utf-8");
var unencryptedAsString = decoder.decode(unencryptedAsUint8Array);
// var unencryptedAsString = Base64Utilities.UTF8ArrToStr(unencryptedAsUint8Array);
document.getElementById('unencrypted_area').value = unencryptedAsString;
}
function base64ToBytes(base64) {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}
function bytesToBase64(bytes) {
const binString = Array.from(bytes, (x) => String.fromCodePoint(x)).join("");
return btoa(binString);
}
</script>
</head>
<body>
<h1>One Time Pad Tool</h1>
<button onclick="ClearAll()">Clear All</button>
<br>
<br>
<div class="all_text_areas_wrapper">
<div class="title_and_text_area_wrapper">
<h2><label for="unencrypted_area">Unencrypted</label></h2>
<button onclick="CopyUnencrypted()" title="Copy unencrypted to clipboard." class="copy_button">
<span style="position: relative; font-size: 1.3rem;">
<!-- Clipboard icon, &ClearOutput#128203; -->
📋
<span style="position: absolute; top: .4rem; left: .6rem; font-size: 1rem;">
<!-- Leftwards arrow with hook, ↩ -->
↩
</span>
</span>
</button>
<textarea id="unencrypted_area" name="unencrypted_area" rows="20" cols="60" placeholder="Raw UTF-8 formatted message"></textarea>
</div>
<div class="title_and_text_area_wrapper">
<h2><label for="one_time_pad_area">One Time Pad</label></h2>
<button onclick="CopyOneTimePad()" title="Copy one time pad to clipboard." class="copy_button">
<span style="position: relative; font-size: 1.3rem;">
<!-- Clipboard icon, &ClearOutput#128203; -->
📋
<span style="position: absolute; top: .4rem; left: .6rem; font-size: 1rem;">
<!-- Leftwards arrow with hook, ↩ -->
↩
</span>
</span>
</button>
<textarea id="one_time_pad_area" name="one_time_pad_area" rows="20" cols="60" placeholder="Base64 encoded bytes"></textarea>
<!-- <button onclick="GenerateOneTimePad()" title="Generate one time pad">Generate OTP</button> -->
</div>
<div class="title_and_text_area_wrapper">
<h2><label for="encrypted_area">Encrypted</label></h2>
<button onclick="CopyEncrypted()" title="Copy encrypted to clipboard." class="copy_button">
<span style="position: relative; font-size: 1.3rem;">
<!-- Clipboard icon, &ClearOutput#128203; -->
📋
<span style="position: absolute; top: .4rem; left: .6rem; font-size: 1rem;">
<!-- Leftwards arrow with hook, ↩ -->
↩
</span>
</span>
</button>
<textarea id="encrypted_area" name="encrypted_area" rows="20" cols="60" placeholder="Base64 encoded bytes"></textarea>
</div>
</div>
<span id="action_status">status</span><br>
<fieldset>
<legend>Actions</legend>
<button onclick="EncryptWithGeneratedOtp()">Generate OTP and encrypt</button>
<button onclick="EncryptWithSuppliedOtp()">Encrypt using OTP</button>
<button onclick="DecryptWithOtp()">Decrypt using OTP</button>
</fieldset>
</body>
</html>