Skip to content

Commit 48ad8e6

Browse files
save file
1 parent 267d43c commit 48ad8e6

1 file changed

Lines changed: 72 additions & 7 deletions

File tree

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

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,48 @@ rsa keys should be 4096
2727

2828
var key = await generateAesKey();
2929
var key_blob = await exportAesKey(key);
30-
console.log('key :',key_blob.size);
31-
var encrypted = await aesEncrypt(key,blob);
30+
console.log('key :',key_blob.size,'B');
31+
var b64=await blob_b64(key_blob);
32+
console.log(b64);
33+
var encrypted = await aesEncryptNode(key_blob,blob);
3234
var b64 = await blob_b64(encrypted);
3335
console.log('encrypted :');
3436
console.log(b64);
3537
console.log();
3638
var key = await importAesKey(key_blob);
3739

38-
var blob = await aesDecrypt(key,encrypted);
40+
var blob = await aesDecryptNode(key_blob,encrypted);
3941

4042
var txt = await blob.text();
4143
console.log('decrypted :');
4244
console.log(txt);
4345

46+
47+
console.log();
48+
console.log();
49+
50+
51+
if(enabled=1){
52+
console.log('decrypt test');
53+
console.log();
54+
var encrypted_b64 = 'dqeBcMIqEJy2E2z1ixpE98Dcnrp8r275UptzFMoeg7ZTYaZqSv4b';
55+
var encrypted = b64_blob(encrypted_b64);
56+
57+
var key_b64 = 'QWC78FsU8wpP9KtQotZn1zLfm1qXKG6S/0rJDF5KVbk=';
58+
var key_blob = b64_blob(key_b64);
59+
60+
var blob = await aesDecryptNode(key_blob,encrypted);
61+
62+
var txt = await blob.text();
63+
console.log('decrypted :');
64+
console.log(txt);
65+
66+
67+
console.log();
68+
console.log();
69+
}//enabled
70+
71+
4472
})();
4573

4674

@@ -89,7 +117,7 @@ rsa keys should be 4096
89117
var encrypted = Buffer.concat([buf1,buf2]);
90118
var tag = cipher.getAuthTag();
91119

92-
var buf = Buffer.concat([encrypted,authTag]);
120+
var buf = Buffer.concat([encrypted,tag]);
93121

94122
var blob = iv_buf_blob(iv,buf);
95123
return blob;
@@ -103,16 +131,17 @@ rsa keys should be 4096
103131
var bytes = iv_bits/8;
104132
var taglength = 16;
105133

134+
key = await blob_buffer(key);
106135
var buf = await blob_buffer(blob);
107136
var iv = buf.subarray(0,bytes);
108137
buf = buf.subarray(bytes);
109138
var n = buf.length-taglength;
110-
var authTag = buf.subarray(n);
139+
var tag = buf.subarray(n);
111140
buf = buf.subarray(0,n);
112141

113142

114143
var decipher = crypto.createDecipheriv('aes-256-gcm',key,iv);
115-
decipher.setAuthTag(authTag);
144+
decipher.setAuthTag(tag);
116145

117146
var buf1 = decipher.update(buf);
118147
var buf2 = decipher.final();
@@ -150,7 +179,7 @@ rsa keys should be 4096
150179
}//buffer_blob
151180

152181

153-
async function blob_buffer(){
182+
async function blob_buffer(blob){
154183

155184
var buf = await blob.arrayBuffer();
156185
var buffer = Buffer.from(buf);
@@ -159,6 +188,42 @@ rsa keys should be 4096
159188
}//blob_buffer
160189

161190

191+
async function blob_b64(blob){
192+
193+
var uint8 = await blob_uint8(blob);
194+
var bin = [...uint8].reduce((acc,byte)=>acc+=String.fromCharCode(byte),'');
195+
var b64 = btoa(bin);
196+
return b64;
197+
198+
}//blob_b64
199+
200+
201+
function b64_blob(b64,type='text/plain'){
202+
203+
var bin = atob(b64);
204+
var bytes = [...bin].map(c=>c.charCodeAt(0));
205+
var buf = new Uint8Array(bytes);
206+
var blob = new Blob([buf],{type});
207+
return blob;
208+
209+
}//b64_blob
210+
211+
212+
async function blob_uint8(blob){
213+
214+
var buf = await blob.arrayBuffer();
215+
var uint8 = new Uint8Array(buf);
216+
return uint8;
217+
218+
}//blob_uint8
219+
220+
221+
function uint8_blob(uint8){
222+
223+
var blob = new Blob([uint8]);
224+
return blob;
225+
226+
}//uint8_blob
162227

163228

164229

0 commit comments

Comments
 (0)