-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecrypt.html
77 lines (70 loc) · 2.35 KB
/
decrypt.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
<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:
<input id="plaintext" readonly onfocus="this.select();">
<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=function(){
window.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
)
.then(function(privateKey){
//returns a publicKey
var data=text2bin($('#secret').value);
window.crypto.subtle.decrypt(
{
name: "RSA-OAEP",
//label: Uint8Array([...]) //optional
},
privateKey, //from generateKey or importKey above
data //ArrayBuffer of the data
)
.then(function(decrypted){
//returns an ArrayBuffer containing the decrypted data
// console.log(new Uint8Array(decrypted));
var enc = new TextDecoder();
// console.log(enc.decode(decrypted));
$("#err-msg").innerHTML = "";
$('#plaintext').value=enc.decode(decrypted);
})
.catch(function(err){
console.error(err);
displayError();
});
})
.catch(function(err){
console.error(err);
displayError();
});
}
</script>
</body>
</html>