-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.js
46 lines (33 loc) · 1.35 KB
/
aes.js
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
import * as crypto from 'crypto'
import assert from 'assert';
export const aesCreateKey = () => {
// Generate a secret key for encryption and decryption
const key = crypto.randomBytes(32); // 32 bytes = 256 bits
const iv = crypto.randomBytes(16); // 16 bytes = 128 bits
const result = key.toString('base64') + ':' + iv.toString('base64')
return result
}
export const aesEncrypt = (plainText, secret) => {
const parts = secret.split(':')
assert(parts.length === 2)
const key = Buffer.from(parts[0], 'base64');
const iv = Buffer.from(parts[1], 'base64');
// Create an AES cipher object for encryption
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Encrypt the plaintext
let encrypted = cipher.update(plainText, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted
}
export const aesDecrypt = (encryptedText, secret) => {
const parts = secret.split(':')
assert(parts.length === 2)
const key = Buffer.from(parts[0], 'base64');
const iv = Buffer.from(parts[1], 'base64');
// Create a new AES cipher object for decryption
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
// Decrypt the encrypted message
let decrypted = decipher.update(encryptedText, 'base64', 'utf8');
decrypted += decipher.final('utf8');
return decrypted
}