-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecrypMode.cpp
108 lines (99 loc) · 2.52 KB
/
DecrypMode.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "dirent.h"
#include "FileEncryptor.hpp"
#include "LoggerModule.hpp"
#include <fstream>
namespace
{
void decryptFile(const std::string &key, const std::string &filePath)
{
std::string fileContent;
// Read the encrypted file into a string
std::ifstream inputFile(filePath);
if (inputFile)
{
std::ostringstream ss;
ss << inputFile.rdbuf();
fileContent = ss.str();
}
else
{
std::cerr << "Failed to open " << filePath << " ";
perror("Error"); // Print more detailed error information
}
inputFile.close(); // Close the file to release any locks on it
try
{
FileEncryptor fileEncryptor(key, filePath);
fileEncryptor.decryptToFile(fileContent, filePath);
}
catch (const std::exception &e)
{
ERROR_LOG("Failed to decrypt file: {}", e.what());
}
catch (...)
{
ERROR_LOG("Failed to decrypt file: Unknown exception.", "");
}
}
bool isValidKey(const std::string &key)
{
// Check if the key is 32 characters long
if (key.length() != 32)
{
return false;
}
// Check if the key is a valid hexadecimal string
for (char c : key)
{
if (!std::isxdigit(c))
{
return false;
}
}
return true;
}
const std::string getKey()
{
std::string key;
while (true)
{
std::cout << "Enter the decryption key: ";
std::cin >> key;
if (isValidKey(key))
{ // You need to define this function to check the key's validity
break; // Exit the loop if the key is valid
}
std::cout << "Invalid key. Please try again.\n";
}
return key;
}
int decrypt()
{
auto key = getKey();
// Open directory
DIR *dir;
struct dirent *ent;
std::string dirPath = "../server/uploaded_files/";
if ((dir = opendir(dirPath.c_str())) != nullptr)
{
// Loop through all files in the directory
while ((ent = readdir(dir)) != nullptr)
{
std::string fileName = ent->d_name;
if (fileName == "." || fileName == "..") continue; // Skip . and ..
std::string filePath = dirPath + fileName;
LOG("Decrypting file: {}", filePath);
// Call the new decryptFile function
decryptFile(key, filePath);
}
closedir(dir);
}
else
{
// Could not open directory
perror("Could not open directory");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace