-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecrypt.html
More file actions
76 lines (67 loc) · 2.28 KB
/
Copy pathdecrypt.html
File metadata and controls
76 lines (67 loc) · 2.28 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
<html>
<head>
<style>
input, textarea {
display: block;
width: 100%;
}
#private {background: palevioletred;}
#plaintext {background: lightyellow;}
#secret {background: lightblue;}
</style>
</head>
<body>
Paste the PRIVATE key that you generated at <a href="generate.html">generate.html</a>:
<textarea id="private" onfocus="this.select();" oninput='autosize(this)'></textarea>
Paste the message that sender ENCRYPTED at <a href="encrypt.html">encrypt.html</a>::
<textarea id="secret" onfocus="this.select();" oninput='autosize(this)'></textarea>
<br>
<input type="button" id="decrypt" value="decrypt">
Here is your secret message:
<textarea id="plaintext" readonly onfocus="this.select();"></textarea>
<p id="err-msg"></p>
<script>
function $(s){return document.querySelector(s)};
function text2bin(text){try {return new Uint8Array(atob(text).split('').map(function(a){return a.charCodeAt(0)}))}catch(err){displayError();}};
function autosize(textarea){textarea.style.height = "";textarea.style.height = textarea.scrollHeight +3 + "px"};
function displayError() {$('#plaintext').value="";$("#err-msg").innerHTML = "Sorry, an error occurred";}
$('#decrypt').onclick=async function(){
privateKey = await crypto.subtle.importKey(
"pkcs8", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
text2bin($('#private').value),
{ //these are the algorithm options
name: "RSA-OAEP",
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["decrypt"] //"encrypt" or "wrapKey" for public key import or
);
const [iv, ciphertext, encryptedKey] = $('#secret').value.split(",").map(text2bin);
// Decrypt AES key
const rawKey = await crypto.subtle.decrypt(
{ name: "RSA-OAEP" },
privateKey,
encryptedKey
);
const aesKey = await crypto.subtle.importKey(
"raw",
rawKey,
{ name: "AES-GCM" },
false,
["decrypt"]
);
// Decrypt data
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv },
aesKey,
ciphertext
);
var enc = new TextDecoder();
// console.log(enc.decode(decrypted));
$("#err-msg").innerHTML = "";
$('#plaintext').value=enc.decode(decrypted);
autosize($('#plaintext'));
}
</script>
</body>
</html>