-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypt.html
89 lines (81 loc) · 2.08 KB
/
encrypt.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
<label> Encrypt <input type="file" id="encrypt"> </label>
<label> Decrypt <input type="file" id="decrypt"> </label>
<a id="link">Download</a>
<script>
async function main() {
const initial = Uint8Array.from([
55, 44, 146, 89,
30, 93, 68, 30,
209, 23, 56, 140,
88, 149, 55, 221
])
const key = await crypto.subtle.importKey("jwk", {
"alg": "A256CBC",
"ext": true,
"k": prompt(
"AES-CBC JSON Web Key"),
"key_ops": ["encrypt", "decrypt"],
"kty": "oct"
}, {
"name": "AES-CBC"
},
false,
["encrypt", "decrypt"]
)
/*
const key = await crypto.subtle.generateKey({
'name': 'AES-CBC',
'length': 256
}, false, ['encrypt', 'decrypt'])
const exported = JSON.stringify(await crypto.subtle.exportKey("jwk", key))
*/
const encrypt = document.getElementById("encrypt")
const decrypt = document.getElementById("decrypt")
encrypt.addEventListener("change", () => {
const file = encrypt.files[0]
const reader = new FileReader()
reader.onload = function(event) {
const data = event.target.result
crypto.subtle.encrypt({
'name': 'AES-CBC',
'iv': initial
}, key, data)
.then(encrypted => {
const blob = new Blob([encrypted], {
type: "application/octet-stream"
})
const url = URL.createObjectURL(blob)
const link = document.getElementById(
"link")
link.href = url
})
.catch(console.error)
}
reader.readAsArrayBuffer(file)
})
decrypt.addEventListener("change", () => {
const file = decrypt.files[0]
const reader = new FileReader()
reader.onload = function(event) {
const data = event.target.result
crypto.subtle.decrypt({
'name': 'AES-CBC',
'iv': initial
}, key, data)
.then(decrypted => {
console.log(decrypted)
const blob = new Blob([decrypted], {
type: "image/png"
})
const url = URL.createObjectURL(blob)
const link = document.getElementById(
"link")
link.href = url
})
.catch(console.error)
}
reader.readAsArrayBuffer(file)
})
}
main()
</script>