-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.js
More file actions
50 lines (44 loc) · 1.49 KB
/
decrypt.js
File metadata and controls
50 lines (44 loc) · 1.49 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
import fs from 'fs';
// import crypto from 'crypto';
import forge from 'node-forge';
const privateKeyPath = 'private_key.pem';
const importFilename = 'input.csv';
const exportFilename = 'export.csv';
// const privateKey = crypto.createPrivateKey({ key: fs.readFileSync(privateKeyPath) });
const privateKey = forge.pki.privateKeyFromPem(fs.readFileSync(privateKeyPath).toString());
console.log('Starting decryption process...');
const decrypt = (encryptedValue) => {
try {
return privateKey.decrypt(
forge.util.decode64(encryptedValue),
'RSAES-PKCS1-V1_5'
);
// return crypto.privateDecrypt(
// {
// key: privateKey,
// padding: crypto.constants.RSA_PKCS1_PADDING,
// },
// Buffer.from(encryptedValue, 'base64')
// ).toString();
} catch (error) {
console.error(`Decryption error: ${error.message}`);
}
};
const processFile = async () => {
try {
const data = await fs.promises.readFile(importFilename, 'utf8');
const results = data.split('\n')
.filter(line => line)
.map(line => {
const [email, encryptedValue] = line.split(',');
const unquotedEmail = email.replace(/"/g, '');
const decryptedValue = decrypt(encryptedValue);
return `${unquotedEmail},${decryptedValue}`;
});
await fs.promises.writeFile(exportFilename, results.join('\n'));
console.log('Export completed successfully.');
} catch (err) {
console.error(`Error processing file: ${err.message}`);
}
};
processFile();