-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypt.html
73 lines (66 loc) · 2.22 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
<html>
<head>
<style>
input, textarea {
display: block;
width: 100%;
}
#public {background: lightgreen;}
#plaintext {background: lightyellow;}
#secret {background: lightblue;}
</style>
</head>
<body>
Paste a PUBLIC key that receiver generated at <a href="generate.html">generate.html</a>:
<textarea id="public" onfocus="this.select();" oninput='autosize(this)'></textarea>
Enter a secret message to them (max 62 chars EN or 32 chars RU):
<input id="plaintext" maxlength="62">
<br>
<input type="button" id="encrypt" value="encrypt">
Here is your ENCRYPTED message:
<textarea id="secret" readonly onfocus="this.select();"></textarea>
Now, send it back to receiver and ask them to use <a href="decrypt.html">decrypt.html</a> to read it.
<script>
function $(s){return document.querySelector(s)};
function bin2text(bin){return btoa(String.fromCharCode(...new Uint8Array(bin)));}
function text2bin(text){return new Uint8Array(atob(text).split('').map(function(a){return a.charCodeAt(0)}))};
function autosize(textarea){textarea.style.height = "";textarea.style.height = textarea.scrollHeight +3 + "px"};
$('#encrypt').onclick=function(){
window.crypto.subtle.importKey(
"spki", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
text2bin($('#public').value),
{ //these are the algorithm options
name: "RSA-OAEP",
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["encrypt"] //"encrypt" or "wrapKey" for public key import or
)
.then(function(publicKey){
//returns a publicKey
var enc = new TextEncoder();
var message=enc.encode($('#plaintext').value);
window.crypto.subtle.encrypt(
{
name: "RSA-OAEP",
//label: Uint8Array([...]) //optional
},
publicKey, //from generateKey or importKey above
message //ArrayBuffer of data you want to encrypt
)
.then(function(encrypted){
//returns an ArrayBuffer containing the encrypted data
$('#secret').value=btoa(String.fromCharCode(...new Uint8Array(encrypted)));
autosize($('#secret'));
})
.catch(function(err){
console.error(err);
});
})
.catch(function(err){
console.error(err);
});
}
</script>
</body>
</html>