-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_encryption.html
554 lines (515 loc) · 24.9 KB
/
test_encryption.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Test Encryption</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="https://github.com/didww/didww-encrypt">DIDWW Encrypt</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/didww-encrypt">Encrypt file</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="">Test Encryption</a>
</li>
</ul>
</div>
</nav>
<div class="card m-5">
<div class="card-header">Test Encryption</div>
<div class="card-body">
<div class="form-group row">
<label class="control-label col-md-2" for="public-key-a">Public Key A</label>
<textarea
id="public-key-a"
rows="15"
class="form-control col-md-8 mr-2"
readonly
></textarea>
<button
class="btn btn-secondary form-control col-md-1 copy-btn"
data-target="#public-key-a"
>
Copy
</button>
</div>
<div class="form-group row">
<label class="control-label col-md-2" for="public-key-b">Public Key B</label>
<textarea
id="public-key-b"
rows="15"
class="form-control col-md-8 mr-2"
readonly
></textarea>
<button
class="btn btn-secondary form-control col-md-1 copy-btn"
data-target="#public-key-b"
>
Copy
</button>
</div>
<div class="form-group row">
<label class="control-label col-md-2" for="encryption-fingerprint">Fingerprint</label>
<input
type="text"
readonly
id="encryption-fingerprint"
class="form-control col-md-8 mr-2"
>
<button
class="btn btn-secondary form-control col-md-1"
id="encryption-fingerprint-copy-btn"
>
Copy
</button>
</div>
<div class="form-group row">
<label class="control-label col-md-2" for="encrypt-file">Files</label>
<input
type="file"
multiple
id="encrypt-file"
class="form-control-file col-md-8 mr-2"
>
<button class="btn btn-secondary form-control col-md-1 clear-btn" data-target="#encrypt-file">Clear</button>
</div>
<div class="form-group row">
<label class="control-label col-md-2" for="encrypt-file">Files Status</label>
<span id="encrypt-file-status" class="col-md-8">Empty</span>
</div>
<div class="form-group" id="encrypted-download-link">
</div>
<div class="form-group row">
<label class="control-label col-md-2" for="encrypt-file">File To Decrypt</label>
<input
type="file"
id="decrypt-file"
class="form-control-file col-md-8 mr-2"
>
<button class="btn btn-secondary form-control col-md-1 clear-btn" data-target="#decrypt-file">Clear</button>
</div>
<div class="form-group" id="decrypted-download-link">
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/@didww/[email protected]/dist/browser.min.js"></script>
<script>
function readFile(file, onLoad, onError) {
if (!onError) onError = function (error) {
console.error(error)
}
const reader = new FileReader()
reader.onload = function () {
onLoad(reader.result)
}
reader.onerror = function () {
onError(reader.error)
}
reader.readAsArrayBuffer(file)
return reader
}
function buildDownloadLink(file, filename) {
try {
const blobUrl = URL.createObjectURL(file)
return $('<a>', { download: filename, href: blobUrl })
.text('Download ' + filename)
.css({ display: 'block' })
} catch (e) {
console.error(e)
return $('<span>')
.text(e.toString())
.css({ display: 'block', color: 'red' })
}
}
function convertPemToArrayBuffer(keyPem) {
keyPem = keyPem.replace(/\r/g, '')
if (keyPem[keyPem.length - 1] !== "\n") keyPem = keyPem + "\n"
let keyBase64 = keyPem.split("\n").slice(1, -2).join("")
return stringToArrayBuffer(atob(keyBase64))
}
function pemToCryptoPrivateKey(privatePem) {
const algo = { name: 'RSA-OAEP', hash: 'SHA-256' }
const privateKeyBuf = convertPemToArrayBuffer(privatePem)
return crypto.subtle.importKey('pkcs8', privateKeyBuf, algo, true, ['decrypt'])
}
function stringToArrayBuffer(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function arrayBufferToString(buf) {
var bytes = new Uint8Array(buf);
return bytes.reduce(function (str, byte) {
return str + String.fromCharCode(byte);
}, "");
}
function decryptRSA(privateKey, content) {
const algo = { name: "RSA-OAEP", label: new Uint8Array([]) }
const contentBuf = stringToArrayBuffer(content)
return crypto.subtle.decrypt(algo, privateKey, contentBuf).then(function (decryptedBuf) {
return arrayBufferToString(decryptedBuf)
}).catch(function (error) {
console.error('decryptRSA')
console.error(error)
throw error
})
}
const buildFile = (content, name, type) => {
const contentBuf = stringToArrayBuffer(content)
if (!type) type = 'application/octet-stream'
// Edge browser does not support File
if (window && window.navigator && window.navigator.msSaveBlob) {
let file = new Blob([contentBuf], { type: type });
file.lastModifiedDate = new Date();
file.name = name;
return file;
}
return new File([contentBuf], name, { type: type, lastModified: new Date() })
}
function decryptAES(key, iv, content) {
const algo = { name: "AES-CBC" }
const rules = ["encrypt", "decrypt"]
const keyBuf = stringToArrayBuffer(key)
return crypto.subtle.importKey("raw", keyBuf, algo, false, rules).then(function (cryptoKey) {
const decAlgo = { name: "AES-CBC", iv: stringToArrayBuffer(iv) }
const contentBuf = stringToArrayBuffer(content)
return crypto.subtle.decrypt(decAlgo, cryptoKey, contentBuf).then(function (decryptedBuf) {
return arrayBufferToString(decryptedBuf)
})
}).catch(function (error) {
console.error('decryptAES')
console.error(error)
throw error
})
}
// index can be 0 or 1 - first or second encrypted credentials
function decryptFile(binary, privateKey, index) {
const encryptedAesCredentialsA = binary.substring(0, 512)
const encryptedAesCredentialsB = binary.substring(512, 1024)
const encryptedAesCredentials = index === 0 ? encryptedAesCredentialsA : encryptedAesCredentialsB
const encryptedContent = binary.substring(1024)
return decryptRSA(privateKey, encryptedAesCredentials).then(function (aesCredentials) {
const key = aesCredentials.substring(0, 32)
const iv = aesCredentials.substring(32)
return decryptAES(key, iv, encryptedContent)
}).then(function (decryptedBinary) {
return buildFile(decryptedBinary, 'decrypted')
})
}
const privateKeyPemA = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDYSXvP4o+srkDP\n" +
"YfgxbdIYXwMv0ldOlrBBjGjmG9qnQZefAO4A8lAoBpdrBThTjRWu375s9hrKwB2C\n" +
"fAgooEYdbnzxo9ytFxZAjZ89/8FoYOMQnLi10DXW+r35JawHDpVJnxTRQaTEb0TJ\n" +
"YubWJDXMC3mGXy8axBl++7s80/fh620SHUrJHyUzfUg1l6VacT8GIj4QdK5haBUi\n" +
"DpW3OaQII8JJZGg5gW2drUR9Hp73GgT4bgXDH+MrpFFOAv1E4Fbl9QFoukSJjhuP\n" +
"jPkZyhbPoWSUBe3msGTpdCHh+apzNmk+EZ5FfO+c7QmJwrQ/CSOE+FzpZ8rg2eEu\n" +
"Mf/EgjgTaGpUFHydd7fYZOCBA6U4XSCB8TaTfEqVEOAJXfdkc/2GdfB6SsCcPja+\n" +
"Zi7M2IKfxf9P6+fDzPscMw7XVE2Qp1IdjMTaR0HBgitanLO358VpMZR+hZrcHtfH\n" +
"OKr4uMaw9NaHI7cvzjjS9lLX8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Z1fOw03oglP7LI9LuUknnarDbP8HayM1gXyNs7cS1w6riWy5p01sFD\n" +
"FvMqzwilss/2/u/JfoaGErWFp2dlnVDmsXDVueECggEBANDj1hjqPZCFLKgIdzqg\n" +
"GqKk18qfMkAWLn6qR05WnQTAr7s459j3L3hS8ySo3vhwOPG9dzGIauDp+G0Fgf68\n" +
"gQWPzwYuB23vP1FfIMiiFMd/hUsblA0tl2ZM3wuNU/et2H0p2mkJliLNcrWZAW1d\n" +
"mFkXzQnlTHPxaKwKqPEeO02yUW3Hzm8GcMbaN4wGRfiY68yrZpTchVzwyr+8MUKa\n" +
"qgymYRzvDR4G56msgfx9Z4i+6Vi8rWIWCaGT2Jf8nhmMMOMMKvynSqSzPUEnUDhj\n" +
"1b26jb7ED9SgkfdOuCsmFEiKgal9tU4mFCsPGwOxbYrR7zCNqlbWGb09JHDnc9pT\n" +
"RXkCggEBANE2F5STmIWV0W0Pr2G5M6fEtH2PcTEHCS+yw/H5dkIL65co/ZCQGRQD\n" +
"/3qWLTHSYDh+BY007HZS6n4lyyiO/V6gP1o/BqVRvXVr6k6Lh5BzL3aKbzeilHy7\n" +
"xY758554Rg9nj16+kNcSEXvTScOlXaBFvEpMOMb0zeQ3vMPgKBV3n29jH997sxQa\n" +
"ui4s534QeLHd5sMw+eFc3BvOv15vxKXQAHh6RSzIcDDhx6l08QqgwYB76VAlpGwp\n" +
"5CcoVNMHAidr9NEQWlBruAnKtvMgDjCWW+phmeH1TnH7nETP4dTZKjL6VbEon/vJ\n" +
"QNNgyrVbeTyHTwzf/EbyWneRRyK0kZo=\n" +
"-----END RSA PRIVATE KEY-----\n"
const publicKeyPemB = "-----BEGIN RSA PUBLIC KEY-----\n" +
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0d/ef0k/Y/e8iyM0hW+u\n" +
"fgd6QpKRI9deTqii8Mwc8qfnzuwbrvRBMDQ89wHY23UFPbWERV5Pw9S5Opdwet6r\n" +
"hFiCGzRmixElu0DZVLctp8jeWYvV9wHtaKnXSMHG0j3OJ5nStuGP2/SGIz1uGphB\n" +
"1BWZKX24SEQlpJBOGY3epRsO3ABsKxiOOnMdOansZ/4tLIsLnk1Jhi3OXMOPMIzB\n" +
"+iIjmbfDZtW6wduR1vzk04eGcrTcXgbTkJm9RPXrnC18L+qa2dv1r9gr4SG0X7tG\n" +
"bglzK+ZoYqAk87644sPNCSMU/UGMdlGuVmb+y1i0r7GPNDzr9kRAMttpYVaTeMFi\n" +
"bfoEELuWReHlBmDlqnHdvznGo79Q+qS6XO9Q14Jf4EYiSY4RQ9RBup4J7BKJXOeb\n" +
"gIuOReyg+FRVDTsjuoifsCxOXHwrJlhIry6CN1cdiItMLl+9PM5Ybr59tB3H4kfd\n" +
"KtT3hHi77jBcvyCoj4UPYqqYx0tWROLqw5vi1qPByC/tp8b4JmTrJYKYx7u00Tr7\n" +
"emFSEHeT2qHzH2/J0W5BWYN0eLZBJ16+XY55kX/R6JewwUY2aLLNFT33+mh/iNqN\n" +
"8lYSRsV7jDK0l4Ta3I1P215SoIubRVAqveAS4sNjKvLAWXvKYBrs80lo6x3dgwlm\n" +
"gtm35+NPfdL6CJJWdsAsANECAwEAAQ==\n" +
"-----END RSA PUBLIC KEY-----\n"
const encryptFileInput = $('#encrypt-file')
const decryptFileInput = $('#decrypt-file')
const fingerprintInput = $('#encryption-fingerprint')
const downloadLinksContainer = $('#encrypted-download-link')
const decryptedContainer = $('#decrypted-download-link')
const statusNode = $('#encrypt-file-status')
const publicKeyAInput = $('#public-key-a')
const publicKeyBInput = $('#public-key-b')
let cryptoPrivateKeyA = null
let cryptoPrivateKeyB = null
let encryptReader = null
let decryptReader = null
// function generateRSAKey() {
// return crypto.subtle.generateKey(
// {
// name: "RSA-OAEP",
// modulusLength: 4096,
// publicExponent: new Uint8Array([1, 0, 1]),
// extractable: true,
// hash: {
// name: "SHA-256"
// }
// },
// true,
// ["encrypt", "decrypt"]
// )
// }
//
// function cryptoPrivateKeyToPem(privateKey) {
// return crypto.subtle.exportKey('pkcs8', privateKey).then(function (privateKeyBuf) {
// const privatePem = convertArrayBufferToPem(privateKeyBuf, 'RSA PRIVATE KEY')
// return privatePem
// })
// }
//
// function cryptoPublicKeyToPem(publicKey) {
// return window.crypto.subtle.exportKey('spki', publicKey)
// .then(function (spki) {
// return convertArrayBufferToPem(spki, "RSA PUBLIC KEY")
// })
// }
//
// function convertArrayBufferToPem(buf, label) {
// const base64Cert = btoa(arrayBufferToString(buf))
// let pemCert = "-----BEGIN " + label + "-----\r\n"
// let nextIndex = 0
// while (nextIndex < base64Cert.length) {
// if (nextIndex + 64 <= base64Cert.length) {
// pemCert += base64Cert.substr(nextIndex, 64) + "\r\n"
// } else {
// pemCert += base64Cert.substr(nextIndex) + "\r\n"
// }
// nextIndex += 64
// }
// pemCert += "-----END " + label + "-----\r\n"
// return pemCert
// }
//
// function pemToCryptoPublicKey(publicPem) {
// const algo = { name: 'RSA-OAEP', hash: 'SHA-256' }
// const publicKeyBuf = convertPemToArrayBuffer(publicPem)
// return crypto.subtle.importKey('pkcs8', publicKeyBuf, algo, true, ['encrypt'])
// }
//
// generateRSAKey().then(function (value) {
// cryptoPublicKeyToPem(value.publicKey).then(function (pem) {
// publicKeyPemA = pem
// })
// cryptoPrivateKeyToPem(value.privateKey).then(function (pem) {
// privateKeyPemA = pem
// })
// })
// generateRSAKey().then(function (value) {
// cryptoPublicKeyToPem(value.publicKey).then(function (pem) {
// publicKeyPemB = pem
// })
// cryptoPrivateKeyToPem(value.privateKey).then(function (pem) {
// privateKeyPemB = pem
// })
// })
const encryptor = new DidwwEncrypt({
environment: 'test',
publicKeys: [publicKeyPemA, publicKeyPemB]
})
encryptor.getFingerprint().then(function (fingerprint) {
fingerprintInput.val(fingerprint)
})
publicKeyAInput.val(publicKeyPemA)
publicKeyBInput.val(publicKeyPemB)
pemToCryptoPrivateKey(privateKeyPemA).then(function (privateKeyA) {
cryptoPrivateKeyA = privateKeyA
})
pemToCryptoPrivateKey(privateKeyPemB).then(function (privateKeyB) {
cryptoPrivateKeyB = privateKeyB
})
encryptFileInput.on('change', function () {
downloadLinksContainer.html('')
const file = encryptFileInput[0].files[0]
if (!file) {
statusNode.text('Empty')
return
}
statusNode.text('Encrypting...')
if (encryptReader) encryptReader.abort()
encryptReader = readFile(file, function (binaryBuf) {
const binary = arrayBufferToString(binaryBuf)
encryptor.encryptContent(binary).then(function (encrypted) {
const encryptedFile = encrypted.toFile()
const link = buildDownloadLink(encryptedFile, file.name + '.testenc')
downloadLinksContainer.append(link)
statusNode.text('Encrypted.')
})
})
})
decryptFileInput.on('change', function () {
decryptedContainer.html('')
if (decryptReader) decryptReader.abort()
const file = decryptFileInput[0].files[0]
if (!file) {
decryptedContainer.text('Empty')
return
}
decryptReader = readFile(file, function (binaryBuf) {
const binary = arrayBufferToString(binaryBuf)
decryptFile(binary, cryptoPrivateKeyA, 0).then(function (decryptedFile) {
const link = buildDownloadLink(decryptedFile, decryptedFile.name + '_by_key_a')
decryptedContainer.append(link)
})
decryptFile(binary, cryptoPrivateKeyB, 1).then(function (decryptedFile) {
const link = buildDownloadLink(decryptedFile, decryptedFile.name + '_by_key_b')
decryptedContainer.append(link)
})
})
})
$('.copy-btn').on('click', function (event) {
const btn = $(event.target)
const targetSelector = btn.data('target')
if (!targetSelector) return
const target = $(targetSelector)
target[0].select()
document.execCommand('copy')
btn.text('Copied').attr('disabled', 'disabled')
setTimeout(function () {
btn.text('Copy').removeAttr('disabled')
}, 200)
})
$('.clear-btn').on('click', function (event) {
const btn = $(event.target)
const targetSelector = btn.data('target')
if (!targetSelector) return
const target = $(targetSelector)
target.val('')
target.trigger('change')
})
</script>
</body>
</html>