-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES-file.html
More file actions
417 lines (362 loc) · 13 KB
/
Copy pathAES-file.html
File metadata and controls
417 lines (362 loc) · 13 KB
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
<style> .Broken_IE11{ display:none; }</style>
<div class="d-flex flex-row flex-wrap justify-content-center">
<div class="container Broken_IE11">
<div class="col-sm Broken_IE11"> </div>
<div class="col-md Broken_IE11">
<h4 style="text-align: center; width: 100%;">I'm sorry, but Internet Explorer/Edge doesn't support the <a href="https://www.w3.org/TR/WebCryptoAPI/">WebCrypto API.</a>
</h4>
<div class="col-sm Broken_IE11"> </div>
</div>
<div id="AES_Encrypt" class="playtile">
<div class="tile_title text-center">AES Encrypt File</div>
<div>
AES Passphrase: <input id="encrypt_pass" class="key" type="password" size=35
value="Password" onchange="measurekey(this)">
<input id="show_encrypt_pass" type="checkbox" onclick="togglePW(getElementById('encrypt_pass'))"><br>
AES Mode: <select id="encrypt_mode" size="1">
<option>Webcrypto-GCM</option>
</select>
<div id="holder" class="d-flex justify-content-center">
<div id="busy" class="loader" style="display:none;"></div>
<div id="hasher_tip" style="display:block;">Drop a file here to encrypt.</div>
</div>
<!-- The button used to copy the text -->
<a href="#" class="button" onclick="get_ciphertext()">Copy</a>
<div class="row">
<div class="col-2">file:</div>
<div class="col" id="filename">none</div>
</div >
<div class="row">
<div class="col-2">mime: </div>
<div class="col" id="mimetype">none</div>
</div >
<div class="row">
<div class="col-2">size: </div>
<div class="col" id="filesize">0</div>
<div class="col-2"> bytes </div>
</div >
</div>
<div>Plaintext:</div>
<div class="hash" id="encrypt_plaintext" style="overflow-y: scroll; max-height: 200px;"></div>
<div>
<div>sha256: </div>
<div class="hash" id="sha256sum"></div>
</div>
<div>Ciphertext:</div>
<div class="hash" id="encrypt_port" style="overflow-y: scroll; max-height: 200px;"></div>
</div>
<div id="AES_Decrypt" class="playtile">
<div class="tile_title text-center">AES Decrypt File</div>
<div>
AES Passphrase: <input id="decrypt_pass" class="key" type="password" size=35
value="Password" onchange="measurekey(this)">
<input id="show_decrypt_pass" type="checkbox" onclick="togglePW(getElementById('decrypt_pass'))"><br>
AES Mode: <select id="decrypt_mode" size="1">
<option>Webcrypto-GCM</option>
</select>
<a class="button" id="decrypt" href="#" onclick="aes_decrypt(getElementById('decrypt_pass'),
getElementById('decrypt_port'),
getElementById('decrypt_plaintext'),
getElementById('decrypt_mode'))">
Decrypt
</a>
<a class="button" id="download" download="" href="#"> Download</a><br>
Ciphertext:<textarea class="hash" id="decrypt_port" style="overflow-y: scroll; height: 200px;"></textarea>
</div>
<div class="col"></div>
<div>Plaintext:</div>
<div class="hash" id="decrypt_plaintext" style="overflow-y: scroll; height: 200px;"></div>
<div>
<div>sha256: </div>
<div class="hash" id="decrypt_sha256sum"></div>
</div>
</div>
</div>
<script type="application/javascript">
function togglePW(x) {
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
function get_ciphertext(){
var source = document.getElementById('encrypt_port');
var range = document.createRange();
range.selectNodeContents(source);
window.getSelection().addRange(range);
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
}
function hex_to_ascii(hex){
var str = '';
var remainder = 0;
var minprint = 33;
var maxprint = 126;
var printlen = maxprint - minprint;
for (var n = 0; n < hex.length; n += 2) {
var code = (remainder + parseInt(hex.substr(n, 2), 16)) % printlen + minprint;
remainder = code - minprint;
// console.log(code);
var this_char = String.fromCharCode(code);
// console.log(this_char);
str += this_char;
}
return str;
}
function arrayBufferToBase64(ab){
var dView = new Uint8Array(ab); //Get a byte view
var arr = Array.prototype.slice.call(dView); //Create a normal array
var arr1 = arr.map(function(item){
return String.fromCharCode(item); //Convert
});
return window.btoa(arr1.join('')); //Form a string
}
function arrayBufferToHexString(buffer) {
const byteArray = new Uint8Array(buffer);
const hexCodes = [...byteArray].map(value => {
const hexCode = value.toString(16);
const paddedHexCode = hexCode.padStart(2, '0');
return paddedHexCode;
});
return hexCodes.join('');
}
function base64ToArrayBuffer(base64) {
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function base64ToArray(b64encoded){
return new Uint8Array(atob(b64encoded).split("").map(function(c) {
return c.charCodeAt(0); }));
}
function measurekey(keyinput){
var length = keyinput.value.length;
if (length != 64 && length != 48 && length != 32) {
keyinput.style.color = "red";
}
else {
keyinput.style.color = "inherit";
}
}
function get_mode(mode){
switch(mode) {
case "Webcrypto-GCM":
return "Webcrypto-GCM";
default:
return "Webcrypto-GCM";
}
}
async function getKeyMaterial(epass) {
let password = epass;
let enc = new TextEncoder();
return window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{name: "PBKDF2"},
false,
["deriveBits", "deriveKey"]
);
}
async function encrypt(password, plaintext, salt, iv) {
let keyMaterial = await getKeyMaterial(password);
let key = await window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: salt,
"iterations": 100000,
"hash": "SHA-256"
},
keyMaterial,
{ "name": "AES-GCM", "length": 256},
true,
[ "encrypt", "decrypt" ]
);
return window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
plaintext
);
}
async function decrypt(password, ciphertext, salt, iv) {
let keyMaterial = await getKeyMaterial(password);
let key = await window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: salt,
"iterations": 100000,
"hash": "SHA-256"
},
keyMaterial,
{ "name": "AES-GCM", "length": 256},
true,
[ "encrypt", "decrypt" ]
);
try {
let decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: iv
},
key,
ciphertext
);
return decrypted;
} catch (e) {
alert("*** Decryption error ***");
}
}
async function aes_decrypt(pass,port,PT,mode){
var dec = port.value;
let salt = await base64ToArray(dec.slice(0,24));
let iv = await base64ToArray(dec.slice(25,41));
let ciphertext = await base64ToArrayBuffer(dec.slice(42));
var epass = pass.value;
var emode = get_mode(mode.value);
let dec_buffer = await decrypt(epass, ciphertext, salt, iv);
let sha256 = await window.crypto.subtle.digest('SHA-256', dec_buffer);
document.getElementById('decrypt_sha256sum').innerHTML = await arrayBufferToHexString(sha256);
let decrypted = atob(arrayBufferToBase64(dec_buffer));
PT.innerHTML = decrypted;
var filename = decrypted.split(',')[0].split(':')[1];
var data = decrypted.split(',')[1] + "," + decrypted.split(',')[2];
document.getElementById('download').download=filename;
document.getElementById('download').href=data;
}
var randomString = async function(length) {
var text = "";
var possible = "abcdef0123456789";
for(var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
const encoder = new TextEncoder();
const data = encoder.encode(text);
let sha512buffer = await crypto.subtle.digest('SHA-512', data);
var sha512 = arrayBufferToHexString(sha512buffer).substring(7, (length+7));
return sha512;
}
var randomBuffer = async function() {
var text = "";
var possible = "abcdef0123456789";
var rlength = 4096;
for(var i = 0; i < rlength; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders();
text += headers;
var d = new Date();
var n = d.getTime().toString();
text += n;
const encoder = new TextEncoder();
const data = encoder.encode(text);
let sha512buffer = await crypto.subtle.digest('SHA-512', data);
return sha512buffer;
}
var holder = document.getElementById('holder');
holder.ondragover = function() {
return false;
};
holder.ondragend = function() {
return false;
};
holder.ondrop = function(event) {
//AES Encrypt dropped file
event.preventDefault();
var file = event.dataTransfer.files[0];
document.getElementById('busy').style = "display:block;"
document.getElementById('hasher_tip').style = "display:none;"
document.getElementById('filename').innerHTML = file.name;
document.getElementById('mimetype').innerHTML = file.type;
document.getElementById('filesize').innerHTML = file.size;
document.getElementById('encrypt_plaintext').innerHTML = "";
document.getElementById('sha256sum').innerHTML = "0000000000000000000000000000000000000000000000000000000000000000";
var reader = new FileReader();
reader.onload = async function(event) {
var plaintext = "file:"+file.name+","+event.target.result;
console.log(file.name);
console.log(file.type);
console.log(file.size);
document.getElementById('encrypt_plaintext').innerHTML = plaintext;
document.getElementById('busy').style = "display:none;"
document.getElementById('hasher_tip').style = "display:block;"
var epass = document.getElementById('encrypt_pass').value;
var emode = get_mode(document.getElementById('encrypt_mode').value);
console.log("Webcrypto GCM");
let salt = window.crypto.getRandomValues(new Uint8Array(16));
let iv = window.crypto.getRandomValues(new Uint8Array(12));
let enc = new TextEncoder();
let encoded_plaintext = await enc.encode(plaintext);
let sha256 = await window.crypto.subtle.digest('SHA-256', encoded_plaintext);
document.getElementById('sha256sum').innerHTML = await arrayBufferToHexString(sha256);
let ciphertext = await encrypt(epass, encoded_plaintext, salt, iv);
var ctstring = arrayBufferToBase64(salt)+"<br>"+
arrayBufferToBase64(iv)+"<br>"+
arrayBufferToBase64(ciphertext);
document.getElementById('encrypt_port').innerHTML = ctstring;
};
reader.readAsDataURL(file);
};
$(document).ready(async function(){
let mybuf = await randomBuffer();
let sha512 = await arrayBufferToHexString(mybuf);
//alert(pass);
var pass = hex_to_ascii(sha512);
var pass_pass = pass + pass;
start_index = Math.floor(Math.random() * pass.length);
var string20 = pass_pass.substring(start_index, (start_index+20));
document.getElementById('encrypt_pass').value = string20;
document.getElementById('decrypt_pass').value = string20;
})
</script>
<style type="text/css">
.tile_title { margin-bottom:8px; line-height: 2em; font-size: 1.25rem;
box-shadow: inset 0 0 5px rgba(0,0,0,0.15);}
.thumb { height: 75px; border: 1px solid #000; margin: 10px 5px 0 0; }
input, button { margin: 5px; }
button { width: 100px; }
.playtile { padding: .5rem!important;
box-shadow: inset 0 0 5px rgba(0,0,0,0.15); border: 1px solid #ccc;
padding:5px; margin:5px; width:40em; min-width:25em; min-height:28em; }
#holder { border: 10px dashed #ccc; height: 5em; padding: 10px; }
#holder.hover { border: 10px dashed #333; }
.hash {overflow-wrap: break-word; box-shadow: inset 0 0 5px rgba(155,0,0,0.15); padding-left:5px;}
#byte_content { margin: 5px 0; max-height: 100px; overflow-y: auto; overflow-x: hidden; }
#byte_range { margin-top: 5px; }
.loader {
border: 0.5em solid #f3f3f3;
border-radius: 50%;
border-top: 0.5em solid #3498db;
width: 2.75em;
height: 2.75em;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite; }
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); } }
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); } }
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* IE10+ specific styles go here */
.playtile{filter: alpha(opacity=25);
zoom: 1; /* Fix for IE7 */
display:none;}
.Broken_IE11{ display:inline-block; }
}
</style>