Skip to content

Commit 66c9edd

Browse files
save file
1 parent abfd43f commit 66c9edd

1 file changed

Lines changed: 76 additions & 48 deletions

File tree

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

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,81 @@ aes-encrypt-decrypt-nodejs.js
1313

1414

1515

16-
const crypto = require("crypto");
17-
18-
function importAesKey(base64) {
19-
return Buffer.from(base64, "base64"); // raw 32‑byte key
20-
}
21-
22-
23-
function aesEncryptNode(keyBuf, plaintext) {
24-
const iv = crypto.randomBytes(12);
25-
26-
const cipher = crypto.createCipheriv("aes-256-gcm", keyBuf, iv);
27-
28-
const encrypted = Buffer.concat([
29-
cipher.update(plaintext, "utf8"),
30-
cipher.final()
31-
]);
32-
33-
const tag = cipher.getAuthTag();
34-
35-
return {
36-
iv: iv.toString("base64"),
37-
data: encrypted.toString("base64"),
38-
tag: tag.toString("base64"),
39-
};
40-
}
41-
42-
43-
44-
function aesDecryptNode(keyBuf, encrypted) {
45-
const iv = Buffer.from(encrypted.iv, "base64");
46-
const data = Buffer.from(encrypted.data, "base64");
47-
const tag = Buffer.from(encrypted.tag, "base64");
48-
49-
const decipher = crypto.createDecipheriv("aes-256-gcm", keyBuf, iv);
50-
decipher.setAuthTag(tag);
16+
var crypto = require('crypto');
17+
18+
19+
20+
function importAesKey(base64){
21+
22+
return Buffer.from(base64,'base64'); // raw 32‑byte key
23+
24+
}//importAesKey
25+
26+
27+
function aesEncryptNode(keyBuf,plaintext,iv_bits=96){
28+
29+
var bytes = iv_bits/8;
30+
var iv = crypto.randomBytes(bytes);
31+
32+
var cipher = crypto.createCipheriv('aes-256-gcm',keyBuf,iv);
33+
34+
var buf1 = cipher.update(plaintext,'utf8');
35+
var buf2 = cipher.final();
36+
37+
var encrypted = Buffer.concat([buf1,buf2]);
38+
39+
var tag = cipher.getAuthTag();
40+
41+
var buf = Buffer.concat([ciphertext,authTag]);
42+
43+
var blob = iv_buf_blob(iv,buf);
44+
return blob;
45+
46+
}//aesEncryptNode
47+
48+
49+
50+
function aesDecryptNode(keyBuf,encrypted){
51+
52+
var iv = Buffer.from(encrypted.iv, 'base64');
53+
var data = Buffer.from(encrypted.data, 'base64');
54+
var tag = Buffer.from(encrypted.tag, 'base64');
55+
56+
var decipher = crypto.createDecipheriv('aes-256-gcm', keyBuf, iv);
57+
decipher.setAuthTag(tag);
58+
59+
var decrypted = Buffer.concat([
60+
decipher.update(data),
61+
decipher.final()
62+
]);
63+
64+
return decrypted.toString('utf8');
65+
66+
}//aesDecryptNode
67+
68+
69+
//:
5170

52-
const decrypted = Buffer.concat([
53-
decipher.update(data),
54-
decipher.final()
55-
]);
5671

57-
return decrypted.toString("utf8");
58-
}
59-
60-
61-
62-
63-
64-
65-
72+
function iv_buf_blob(iv,buf){
73+
74+
var n1 = iv.length;
75+
var n = n1+buf.length;
76+
var uint8 = new Uint8Array(n);
77+
uint8.set(iv,0);
78+
uint8.set(buf,n1);
79+
var blob = new Blob([uint8]);
80+
return blob;
81+
82+
}//iv_buf_blob
83+
84+
85+
86+
87+
async function blob_buffer(){
88+
89+
var buf = await blob.arrayBuffer();
90+
var buffer = Buffer.from(buf);
91+
return buffer;
92+
93+
}//blob_buffer

0 commit comments

Comments
 (0)