-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (82 loc) · 3.12 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const crypto = require('crypto');
const fs = require('fs');
const rl = require('readline-sync');
const os = require('os');
console.log("Program Başlatılıyor...");
console.log("Github: github.com/fastuptime");
const algorithm = 'aes-256-cbc';
const path = rl.question('Dosyalarin bulundugu klasorun yolu: ');
const doEncrypt = rl.keyInYN('Sifrelemek istiyor musunuz? Y = Evet, N = Hayir: ');
function encryptFolder(folderPath, key, iv) {
const files = fs.readdirSync(folderPath);
for (const file of files) {
if (fs.lstatSync(`${folderPath}/${file}`).isDirectory()) {
encryptFolder(`${folderPath}/${file}`, key, iv);
continue;
} else {
const filePath = `${folderPath}/${file}`;
try {
const data = fs.readFileSync(filePath);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(data);
encrypted = Buffer.concat([encrypted, cipher.final()]);
fs.writeFileSync(filePath, encrypted);
console.log(`Şifrelenen dosya: ${filePath}`);
} catch (e) {
console.log(`Şifrelenemedi: ${filePath}`);
}
}
}
}
function decryptFolder(folderPath, key, iv) {
const files = fs.readdirSync(folderPath);
for (const file of files) {
if (fs.lstatSync(`${folderPath}/${file}`).isDirectory()) {
decryptFolder(`${folderPath}/${file}`, key, iv);
continue;
} else {
const filePath = `${folderPath}/${file}`;
try {
const data = fs.readFileSync(filePath);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(data);
decrypted = Buffer.concat([decrypted, decipher.final()]);
fs.writeFileSync(filePath, decrypted);
console.log(`Şifresi çözülen dosya: ${filePath}`);
} catch (e) {
console.log(`Şifresi çözülemedi: ${filePath}`);
}
}
}
}
if (doEncrypt === true) {
let key = crypto.randomBytes(32);
let iv = crypto.randomBytes(16);
console.log('Key:', key.toString('hex'));
console.log('IV:', iv.toString('hex'));
console.log('Bu verileri saklayin. Kaybolursa dosyalarinizi geri alamayacaksiniz.');
let d = {
key: key.toString('hex'),
iv: iv.toString('hex'),
pc: os.hostname()
}
fs.writeFileSync('key.txt', JSON.stringify(d));
encryptFolder(path, key, iv);
} else {
if (!fs.existsSync('key.txt')) {
console.log('key.txt dosyasi bulunamadi. Daha hızlı işlem yapmak için key.txt dosyaniz var ise dosyanizin bulundugu dizine koyunuz.');
let key = rl.question('Key: ');
let iv = rl.question('IV: ');
key = Buffer.from(key, 'hex');
iv = Buffer.from(iv, 'hex');
decryptFolder(path, key, iv);
} else {
fs.readFile('key.txt', (err, data) => {
if (err) throw err;
let d = JSON.parse(data);
let key = Buffer.from(d.key, 'hex');
let iv = Buffer.from(d.iv, 'hex');
decryptFolder(path, key, iv);
});
}
}