Skip to content

Commit 69a9c93

Browse files
save file
1 parent faa41d3 commit 69a9c93

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

blog/26-04-26/x509-certificates-in-js---encrypt-decrypt-data/ex/aes-encrypt-decrypt-browser.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
3636
var key = await generateAesKey();
3737
var key_blob = await exportAesKey(key);
3838
console.log('key :',key_blob.size);
39-
var encrypted = await aesEncrypt(key,blob);
39+
var encrypted = await aesEncrypt(key_blob,blob);
4040
var b64 = await blob_b64(encrypted);
4141
console.log('encrypted :');
4242
console.log(b64);
4343
console.log();
44-
var key = await importAesKey(key_blob);
45-
46-
var blob = await aesDecrypt(key,encrypted);
44+
45+
var blob = await aesDecrypt(key_blob,encrypted);
4746

4847
var txt = await blob.text();
4948
console.log('decrypted :');
@@ -65,7 +64,7 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
6564
var key_b64 = 'QWC78FsU8wpP9KtQotZn1zLfm1qXKG6S/0rJDF5KVbk=';
6665
var key_blob = b64_blob(key_b64);
6766

68-
var blob = await aesDecryptNode(key_blob,encrypted);
67+
var blob = await aesDecrypt(key_blob,encrypted);
6968

7069
var txt = await blob.text();
7170
console.log('decrypted :');
@@ -102,11 +101,11 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
102101

103102
async function importAesKey(blob){
104103

105-
var buf = await blob.arrayBuffer();
106-
var uint8 = new Uint8Array(buf);
104+
var buf = await blob_buf(blob);
105+
//var uint8 = new Uint8Array(buf);
107106

108107
var format = 'raw';
109-
var keydata = uint8;
108+
var keydata = buf;//uint8;
110109
var algorithm = 'AES-GCM';
111110
var extractable = true;
112111
var keyusage = ['encrypt','decrypt'];
@@ -118,8 +117,9 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
118117
}//importAesKey
119118

120119

121-
async function aesEncrypt(key,blob,iv_bits=96){
120+
async function aesEncrypt(key_blob,blob,iv_bits=96){
122121

122+
var key = await blob_buf(key_blob);
123123
var buf = await blob.arrayBuffer();
124124
// 96-bit IV recommended ( 12 bytes )
125125
var bytes = iv_bits/8;
@@ -139,8 +139,9 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
139139
}//aesEncrypt
140140

141141

142-
async function aesDecrypt(key,blob){
142+
async function aesDecrypt(key_blob,blob){
143143

144+
var key = await importAesKey(key_blob);
144145
var {iv,buf} = await blob_iv_buf(blob);
145146

146147
var algorithm = {name:'AES-GCM',iv};
@@ -184,6 +185,14 @@ TypedArray.prototype.slice() : https://developer.mozilla.org/en-US/docs/Web
184185
}//blob_iv_buf
185186

186187

188+
async function blob_buf(blob){
189+
190+
var buf = await blob.arrayBuffer();
191+
return buf;
192+
193+
}//blob_buf
194+
195+
187196
async function blob_uint8(blob){
188197

189198
var buf = await blob.arrayBuffer();

0 commit comments

Comments
 (0)