-
Notifications
You must be signed in to change notification settings - Fork 4
/
polysafe.html
434 lines (381 loc) · 15.9 KB
/
polysafe.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<!DOCTYPE html>
<html>
<!--
Copyright 2018 Fabian Henneke
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<head>
<meta charset="utf-8" />
<title>PolySafe</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
form {
display: grid;
grid-template-columns: max-content max-content;
grid-gap: 10px;
align-items: center;
}
.grid-1-l {
grid-row: 1;
grid-column: 1;
}
.grid-1-r {
grid-row: 1;
grid-column: 2;
}
.grid-2-l {
grid-row: 2;
grid-column: 1;
}
.grid-2-r {
grid-row: 2;
grid-column: 2;
}
.grid-3-l {
grid-row: 3;
grid-column: 1;
}
.grid-3-r {
grid-row: 3;
grid-column: 2;
}
.grid-4-r {
grid-row: 4;
grid-column: 2;
}
</style>
</head>
<body>
<noscript>
PolySafe will not work without JavaScript enabled!<br/>
</noscript>
<form action="javascript:;" style="display:none">
<div class="grid-1-l">
<label for="file">File to encrypt:</label>
</div>
<div class="grid-1-r">
<input type="file" id="file" autofocus>
</div>
<div class="grid-2-l">
<label for="password">Password:</label>
</div>
<div class="grid-2-r">
<input type="password" id="password">
</div>
<div class="grid-3-l">
<label for="password_repeated">Repeat password:</label>
</div>
<div class="grid-3-r">
<input type="password" id="password_repeated">
</div>
<div class="grid-4-r">
<input type="submit" value="Encrypt file">
</div>
</form>
<script>
'use strict';
function getRandomBytes(num) {
let bytes = new Uint8Array(num);
window.crypto.getRandomValues(bytes);
return bytes;
}
function readFile(file) {
return new Uint8Array(new FileReader().readAsArrayBuffer(file));
}
function readFile(file) {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onerror = () => {
reader.abort();
reject(new DOMException('Cannot read input file.'));
};
reader.onload = () => {
resolve(new Uint8Array(reader.result));
};
reader.readAsArrayBuffer(file);
});
};
function preprendFilename(filename, data_bytes) {
const filename_bytes = new TextEncoder('utf-8').encode(filename + '/');
const combined_bytes = new Uint8Array(filename_bytes.length + data_bytes.length);
combined_bytes.set(filename_bytes, 0);
combined_bytes.set(data_bytes, filename_bytes.length);
return combined_bytes;
}
function deriveKey(password, salt) {
const passwordBytes = new TextEncoder('utf-8').encode(password);
return window.crypto.subtle.importKey(
'raw',
passwordBytes, {
name: 'PBKDF2',
},
false,
['deriveKey']
).then(function (key) {
return window.crypto.subtle.deriveKey({
name: 'PBKDF2',
salt,
iterations: 200000,
hash: {
name: 'SHA-1'
}
},
key, {
name: 'AES-GCM',
length: 128
},
false,
['encrypt']
)
});
}
function encrypt(key, iv, bytes) {
return window.crypto.subtle.encrypt({
name: 'AES-GCM',
length: 128,
iv
}, key, bytes);
}
/*
MIT LICENSE
Copyright 2011 Jon Leighton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function base64ArrayBuffer(arrayBuffer) {
let base64 = '';
const encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const bytes = new Uint8Array(arrayBuffer);
const byteLength = bytes.byteLength;
const byteRemainder = byteLength % 3;
const mainLength = byteLength - byteRemainder;
let a, b, c, d;
let chunk;
// Main loop deals with bytes in chunks of 3
for (var i = 0; i < mainLength; i = i + 3) {
// Combine the three bytes into a single integer
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
// Use bitmasks to extract 6-bit segments from the triplet
a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18
b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12
c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6
d = chunk & 63; // 63 = 2^6 - 1
// Convert the raw binary segments to the appropriate ASCII encoding;
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d];
}
// Deal with the remaining bytes and padding
if (byteRemainder == 1) {
chunk = bytes[mainLength];
a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2
// Set the 4 least significant bits to zero
b = (chunk & 3) << 4; // 3 = 2^2 - 1
base64 += encodings[a] + encodings[b] + '==';
} else if (byteRemainder == 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10
b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4
// Set the 2 least significant bits to zero
c = (chunk & 15) << 2; // 15 = 2^4 - 1
base64 += encodings[a] + encodings[b] + encodings[c] + '=';
}
return base64;
}
function serialize(salt, iv, cipher_buffer) {
const payload = JSON.stringify({
salt: Array.prototype.slice.call(salt),
iv: Array.prototype.slice.call(iv),
encrypted: base64ArrayBuffer(cipher_buffer)
});
const decryptor_html = DECRYPTOR_TEMPLATE.replace('{{___PAYLOAD___}}', payload);
return new TextEncoder('utf-8').encode(decryptor_html);
}
function download(filename, buffer) {
const a = document.createElement('a');
a.style = 'display: none';
document.body.appendChild(a);
const blob = new Blob([buffer], {
type: 'application/octet-stream'
});
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}
function runEncrypt() {
if (!window.crypto) {
alert(
'Full support for the WebCrypto API is needed for decryption. Please use one of the following browsers: Chrome 37+, Firefox 34+, Safari 7+, Opera 24+'
)
return;
}
const file = document.getElementById('file').files[0];
if (!file) {
alert('Select a file first.');
return;
}
const password = document.getElementById('password').value;
if (password != document.getElementById('password_repeated').value) {
alert('Passwords must match.');
return;
}
const encryptButton = document.querySelector('input[type=submit]');
encryptButton.disabled = true;
encryptButton.value = 'Encrypting...';
const salt = getRandomBytes(16);
const iv = getRandomBytes(16);
readFile(file)
.then(file_bytes => preprendFilename(file.name, file_bytes))
.then(payload_bytes => deriveKey(password, salt)
.then(key => encrypt(key, iv, payload_bytes)))
.then(buffer => serialize(salt, iv, buffer))
.then(buffer => download(file.name + '.html', buffer))
.catch(e => alert('Error during encryption: ' + e.message))
.finally(() => {
encryptButton.disabled = false;
encryptButton.value = 'Encrypt file';
});
}
document.querySelector('form').addEventListener('submit', e => {
e.preventDefault();
runEncrypt();
});
document.querySelector('form').style.display = 'block';
const DECRYPTOR_TEMPLATE =
`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>PolySafe</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<noscript>
Decryption will not work without JavaScript enabled!<br/>
</noscript>
<form action="javascript:;" style="display:none">
<label for="password">Password:</label>
<input type="password" autofocus>
<input type="submit" value="Decrypt file">
</form>
<script id="data" type="application/json">
{{___PAYLOAD___}}
<\/script>
<script>
if (window.crypto && !window.crypto.subtle && window.crypto.webkitSubtle) {
window.crypto.subtle = window.crypto.webkitSubtle;
}
function base64ToArray(base64) {
return new Uint8Array(atob(base64).split('').map(function (c) {
return c.charCodeAt(0);
}));
}
function deriveKey(password, salt) {
var passwordBytes = new TextEncoder('utf-8').encode(password);
return window.crypto.subtle.importKey(
'raw',
passwordBytes, {
name: 'PBKDF2'
},
false,
['deriveKey']
).then(function (key) {
return window.crypto.subtle.deriveKey({
name: 'PBKDF2',
salt: salt,
iterations: 200000,
hash: {
name: 'SHA-1'
}
},
key, {
name: 'AES-GCM',
length: 128
},
false,
['decrypt']
)
});
}
function decrypt(key, iv, bytes) {
return window.crypto.subtle.decrypt({
name: 'AES-GCM',
length: 128,
iv: iv
}, key, bytes);
}
function extractDecryptedFile(buffer) {
var bytes = new Uint8Array(buffer);
var slash_pos = Array.prototype.indexOf.call(bytes, '/'.charCodeAt(0));
if (slash_pos == -1) {
throw 'Decrypted data is corrupted';
} else {
var name = new TextDecoder('utf-8').decode(new Uint8Array(Array.prototype.slice.call(bytes, 0, slash_pos)));
var content = new Uint8Array(Array.prototype.slice.call(bytes, slash_pos + 1));
return {
name: name,
content: content
};
}
}
function download(file) {
var a = document.createElement('a');
a.style = 'display: none';
document.body.appendChild(a);
var blob = new Blob([file.content]);
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = file.name;
a.click();
setTimeout(function () {
window.URL.revokeObjectURL(url);
}, 10000);
}
function runDecrypt() {
if (!window.crypto) {
alert(
'Full support for the WebCrypto API is needed for decryption. Please use one of the following browsers: Chrome 37+, Firefox 34+, Safari 7+, Opera 24+'
)
}
var decryptButton = document.querySelector('input[type=submit]');
decryptButton.disabled = true;
decryptButton.value = 'Decrypting...';
setTimeout(function () {
var data = JSON.parse(document.getElementById('data').textContent);
var salt = new Uint8Array(data.salt);
var iv = new Uint8Array(data.iv);
var encrypted = base64ToArray(data.encrypted);
var password = document.querySelector('input[type=password]').value;
deriveKey(password, salt)
.then(function (key) {
return decrypt(key, iv, encrypted);
})
.catch(function () {
throw 'Wrong password or corrupted file.';
})
.then(extractDecryptedFile)
.then(download)
.catch(function (e) {
alert('Error during decryption: ' + e);
})
.then(function () {
decryptButton.disabled = false;
decryptButton.value = 'Decrypt file';
});
}, 0);
}
document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();
runDecrypt();
});
document.querySelector('form').style.display = 'block';
<\/script>
</body>
</html>`;
</script>
</body>
</html>