-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateTestData.js
More file actions
67 lines (56 loc) · 2.13 KB
/
generateTestData.js
File metadata and controls
67 lines (56 loc) · 2.13 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
// Function to generate RSA key pair
function generateRSAKeyPair() {
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
return { publicKey, privateKey };
}
// Function to pad data
function padData(data) {
const blockSize = 16;
const padding = blockSize - (data.length % blockSize);
const paddedData = Buffer.concat([data, Buffer.alloc(padding, padding)]);
return paddedData;
}
// Function to encrypt data using AES
function encryptData(data, sessionKey, iv) {
const cipher = crypto.createCipheriv('aes-256-cbc', sessionKey, iv);
const paddedData = padData(data);
const encryptedData = Buffer.concat([cipher.update(paddedData), cipher.final()]);
return encryptedData;
}
// Function to encrypt session key using RSA
function encryptSessionKey(sessionKey, publicKey) {
const encryptedSessionKey = crypto.publicEncrypt(publicKey, sessionKey);
return encryptedSessionKey;
}
// Generate RSA key pair
const { publicKey, privateKey } = generateRSAKeyPair();
// Write private key to file
fs.writeFileSync(path.join(__dirname, 'private-key.pem'), privateKey);
// Write public key to file
fs.writeFileSync(path.join(__dirname, 'public-key.pem'), publicKey);
const sessionKey = crypto.randomBytes(32); // 256-bit key
const dataToEncrypt = Buffer.from('encryptme!');
const iv = crypto.randomBytes(16); // 128-bit IV
const encryptedData = encryptData(dataToEncrypt, sessionKey, iv);
const encryptedSessionKey = encryptSessionKey(sessionKey, publicKey);
const encodedSessionKey = encryptedSessionKey.toString('base64');
const encodedData = encryptedData.toString('base64');
const encodedIv = iv.toString('base64');
console.log('Public Key (PEM):\n', publicKey);
console.log('Private Key (PEM):\n', privateKey);
console.log('Encrypted Session Key (Base64):\n', encodedSessionKey);
console.log('Encrypted Data (Base64):\n', encodedData);
console.log('IV (Base64):\n', encodedIv);