-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes-eax.html
203 lines (176 loc) · 6.28 KB
/
aes-eax.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Encrypt and decrypt Uint8Array data with a password</title>
<script src="js/jquery.min.js"></script>
<style type="text/css">
.txtarea {
width:600px;
height:200px;
}
</style>
</head>
<body>
<h2>AES-EAX Encryption</h2>
The Encrypted Msg gets longer as more passwords you add
<br /><br />
<script src="js/openpgp.min.js"></script>
Passwort:<br />
<input id="password1" value="Secret#1" />
<input id="password2" value="" />
<input id="password3" value="" />
<br /><br />
Encryption Algorithm: <select id="algorithm"><option value="aex">AES-EAX</option><option value="ocb">OCB</option><option value="gcm">GCM</option></select><br />
with Compression: <select id="compression"><option value="uncompressed">uncompressed</option><option value="zip">zip</option><option value="zlib">zlib</option></select><br />
Message: <button id="btnEncrypt">encrypt</button><br />
<textarea id="inputText" class="txtarea">test 1234</textarea>
<br />
Encrypted: <br />
<textarea id="encryptedTxt" class="txtarea"></textarea>
<br />
Decrypted: <input id="decrypt_password" value="Secret#1" /><button id="btnDecrypt">decrypt</button><br />
<textarea id="decryptedTxt" class="txtarea"></textarea>
<script>
const base64abc = (() => {
let abc = [],
A = "A".charCodeAt(0),
a = "a".charCodeAt(0),
n = "0".charCodeAt(0);
for (let i = 0; i < 26; ++i) {
abc.push(String.fromCharCode(A + i));
}
for (let i = 0; i < 26; ++i) {
abc.push(String.fromCharCode(a + i));
}
for (let i = 0; i < 10; ++i) {
abc.push(String.fromCharCode(n + i));
}
abc.push("+");
abc.push("/");
return abc;
})();
function bytesToBase64(bytes) {
let result = '', i, l = bytes.length;
for (i = 2; i < l; i += 3) {
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
result += base64abc[((bytes[i - 1] & 0x0F) << 2) | (bytes[i] >> 6)];
result += base64abc[bytes[i] & 0x3F];
}
if (i === l + 1) { // 1 octet missing
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[(bytes[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) { // 2 octets missing
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
result += base64abc[(bytes[i - 1] & 0x0F) << 2];
result += "=";
}
return result;
}
const utf8encoder = new TextEncoder();
// All solutions at MDN only provide a way to encode a native JS string to UTF-16 base64 string.
// Here, you can apply any encoding supported by TextEncoder.
function base64utf8encode(str) {
return bytesToBase64(utf8encoder.encode(str));
}
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
(async () => {
var preferredCompressionAlgorithm;
var preferredAEADAlgorithm;
selectAlgorithm = function() {
switch($('#algorithm').val()) {
case 'ocb': preferredAEADAlgorithm = openpgp.enums.aead.ocb // Non-native
break;
case 'gcm': preferredAEADAlgorithm = openpgp.enums.aead.experimentalGCM // **Non-standard**, fastest
break;
default: preferredAEADAlgorithm = openpgp.enums.aead.eax // Default, native
}
}
selectCompression = function() {
switch($('#compression').val()) {
case 'zip': preferredCompressionAlgorithm = openpgp.enums.compression.zip
break;
case 'zlib': preferredCompressionAlgorithm = openpgp.enums.compression.zlib
break;
default: preferredCompressionAlgorithm = openpgp.enums.compression.uncompressed
}
}
selectAlgorithm();
selectCompression();
doEncrypt = async function() {
var usepwds = Array();
var passwd1 = $('#password1').val();
var passwd2 = $('#password2').val();
var passwd3 = $('#password3').val();
if (passwd1 != "") usepwds.push(passwd1);
if (passwd2 != "") usepwds.push(passwd2);
if (passwd3 != "") usepwds.push(passwd3);
//console.log(usepwds);
var cleartext = $('#inputText').val();
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: cleartext}), // input as Message object
passwords: usepwds, // multiple passwords possible
format: 'binary', // don't ASCII armor (for Uint8Array output)
config: { preferredAEADAlgorithm,
preferredCompressionAlgorithm }
});
$('#encryptedTxt').val(bytesToBase64(encrypted));
}
doDecrypt = async function() {
var encryptedd = $('#encryptedTxt').val();
var passwd = $('#decrypt_password').val();
var encryptedBytes = new Uint8Array(_base64ToArrayBuffer(encryptedd));
const message = await openpgp.readMessage({
binaryMessage: encryptedBytes // parse message
});
const { data: decrypted } = await openpgp.decrypt({
message,
passwords: [passwd], // decrypt with password
config: { preferredAEADAlgorithm,
preferredCompressionAlgorithm }
});
//console.log(decrypted)
$('#decryptedTxt').val(decrypted);
};
})();
doEncrypt()
$('#inputText').bind('input propertychange', function() {
doEncrypt()
});
$('#password1').bind('input propertychange', function() {
doEncrypt()
});
$('#password2').bind('input propertychange', function() {
doEncrypt()
});
$('#password3').bind('input propertychange', function() {
doEncrypt()
});
$('#decrypt_password').bind('input propertychange', function() {
$('#decryptedTxt').val('');
doDecrypt()
});
$('#btnEncrypt').click(function() {
doEncrypt()
})
$('#btnDecrypt').click(function() {
doDecrypt()
})
$('#algorithm').change( () => selectAlgorithm() )
$('#compression').change( () => selectCompression() )
</script>
</body>
</html>