-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
70 lines (57 loc) · 2.3 KB
/
decrypt.cpp
File metadata and controls
70 lines (57 loc) · 2.3 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
68
69
70
#include <iostream>
#include <filesystem>
#include <fstream>
#include "VirtualHardDrive.h"
namespace fs = std::filesystem;
bool checkFileExists(const std::string& fileName) {
std::ifstream file(fileName);
return file.good();
}
void decryptAndSave(const fs::path& destPath, VirtualHardDrive& vhd, encryption& enc) {
std::vector<std::pair<std::string, std::size_t>> files = vhd.getAllFiles();
for (const auto& [virtualPath, size] : files) {
std::string decryptedData = vhd.readData(virtualPath, size);
fs::path outputPath = destPath / virtualPath;
fs::create_directories(outputPath.parent_path());
std::ofstream outFile(outputPath, std::ios::binary);
if (!outFile) {
std::cerr << "Failed to open file for writing: " << outputPath << std::endl;
continue;
}
outFile.write(decryptedData.data(), decryptedData.size());
outFile.close();
}
}
int main() {
std::cout << "Decrypting virtual hard drive..." << std::endl;
encryption enc;
// Check if key files exist
std::string aesKeyFile = "aeskey.dat";
std::string privateKeyFile = "private_key.bin";
if (!checkFileExists(aesKeyFile)) {
std::cerr << "Error: AES key file '" << aesKeyFile << "' not found!" << std::endl;
return 1; // Exit with an error code
}
if (!checkFileExists(privateKeyFile)) {
std::cerr << "Error: Private key file '" << privateKeyFile << "' not found!" << std::endl;
return 1; // Exit with an error code
}
// Try to decrypt the key with proper error handling
try {
enc.decryptkey(aesKeyFile, privateKeyFile); // Decrypt using the provided keys
} catch (const std::exception& e) {
std::cerr << "Failed to decrypt key: " << e.what() << std::endl;
return 1; // Exit if key decryption fails
}
try {
std::string fileName = "virtual_hard_drive.vhd";
VirtualHardDrive vhd(fileName, enc); // Read from the existing virtual hard drive
fs::path destDirectory = "./decrypted";
decryptAndSave(destDirectory, vhd, enc);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1; // Exit with an error code
}
std::cout << "Decryption complete." << std::endl;
return 0;
}