Skip to content

Commit ac921b4

Browse files
save file
1 parent 5185a4d commit ac921b4

1 file changed

Lines changed: 40 additions & 11 deletions

File tree

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ aes encrypt / decrypt browser
1616
(async()=>{
1717
console.clear();
1818

19-
19+
var blob = new Blob(['hello world']);
20+
2021
var key = await generateAesKey();
2122
var encrypted = await aesEncrypt(key,'hello world');
2223
console.log('encrypted :');
2324
console.log(' iv :',enrypted.iv);
2425
console.log(' data :',encrypted.data);
2526
console.log();
26-
var txt = await aesDecrypt(key,encrypted);
27+
var blob = await aesDecrypt(key,encrypted);
28+
29+
var txt = await blob.text();
2730
console.log('decrypted :');
2831
console.log(txt);
2932

3033

3134

32-
async function generateAesKey() {
35+
async function generateAesKey(){
3336

3437
var algorithm = {name:'AES-GCM',length:256};
3538
var extractable = true;
@@ -42,7 +45,7 @@ aes encrypt / decrypt browser
4245
}//generateAesKey
4346

4447

45-
async function exportAesKey(key) {
48+
async function exportAesKey(key){
4649

4750
var buf = await crypto.subtle.exportKey('raw',key);
4851

@@ -67,9 +70,9 @@ aes encrypt / decrypt browser
6770
}//importAesKey
6871

6972

70-
async function aesEncrypt(key,txt){
73+
async function aesEncrypt(key,blob){
7174

72-
var buf = txt_buf(txt);
75+
var buf = await blob.arrayBuffer();
7376
// 96-bit IV recommended
7477
var iv = crypto.getRandomValues(new Uint8Array(12));
7578

@@ -99,16 +102,42 @@ aes encrypt / decrypt browser
99102
data = b64_uint8(data);
100103

101104
var buf = await crypto.subtle.decrypt(algorithm,key,data);
102-
var txt = buf_txt(buf);
103105

104-
return txt;
106+
var blob = new Blob([buf]);
107+
return blob;
105108

106109
}//aesDecrypt
107110

108111

109112
//:
110113

111114

115+
function iv_buf_blob(iv,buf){
116+
}//iv_buf
117+
118+
119+
async function blob_b64(blob){
120+
121+
var buf = await blob.arrayBuffer();
122+
var bytes = new Uint8Array(buf);
123+
var bin = [...bytes].reduce((acc,byte)=>acc+=String.fromCharCode(byte),'');
124+
var b64 = btoa(bin);
125+
return b64;
126+
127+
}//blob_b64
128+
129+
130+
function b64_blob(b64,type='text/plain'){
131+
132+
var bin = atob(b64);
133+
var bytes = [...bin].map(c=>c.charCodeAt(0));
134+
var buf = new Uint8Array(bytes);
135+
var blob = new Blob([buf],{type});
136+
return blob;
137+
138+
}//b64_blob
139+
140+
112141
function buf_b64(buf){
113142

114143
var uint8 = new Uint8Array(buf);
@@ -128,10 +157,10 @@ aes encrypt / decrypt browser
128157
}//b64_buf
129158

130159

131-
function txt_buf(txt){
160+
function txt_uint8(txt){
132161

133-
var buf = new TextEncoder().encode(txt);
134-
return buf;
162+
var uint8 = new TextEncoder().encode(txt);
163+
return uint8;
135164

136165
}//txt_buf
137166

0 commit comments

Comments
 (0)