-
Notifications
You must be signed in to change notification settings - Fork 0
/
AES256-Encrypt.js
58 lines (49 loc) · 2.21 KB
/
AES256-Encrypt.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
47
48
49
50
51
52
53
54
55
56
57
58
// node Encrypt-NTLM.js
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const fs = require('fs');
const crypto = require('crypto');
// Meminta input folder tujuan
readline.question('Masukkan Folder Tujuan: ', (folderPath) => {
// Meminta input file
readline.question('Masukkan path file (pisahkan dengan koma jika lebih dari satu): ', (filePaths) => {
// Meminta input password
readline.question('Masukkan password enkripsi: ', (password) => {
// Proses enkripsi setiap file di dalam folder
filePaths.split(',').forEach((filePath) => {
// Ambil nama file dari path lengkap
const fileName = filePath.split('/').pop();
// Membaca isi file
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}: ${err}`);
return;
}
// Mengenkripsi file dengan password
const cipher = crypto.createCipher('aes-256-cbc', password);
const encryptedData = Buffer.concat([cipher.update(data), cipher.final()]);
// Menyimpan file terenkripsi
const encryptedFilePath = `${folderPath}/${fileName}.enc`;
fs.writeFile(encryptedFilePath, encryptedData, (err) => {
if (err) {
console.error(`Error writing encrypted file ${encryptedFilePath}: ${err}`);
return;
}
console.log(`File ${filePath} telah dienkripsi dan disimpan di ${encryptedFilePath}`);
});
// Menghapus file asli
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting original file ${filePath}: ${err}`);
return;
}
});
});
});
// Menutup interface readline setelah selesai
readline.close();
});
});
});