-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
78 lines (63 loc) · 2.41 KB
/
Copy pathdecrypt.cpp
File metadata and controls
78 lines (63 loc) · 2.41 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
71
72
73
74
75
76
77
78
#include<iostream>
#include<fstream>
#include<openssl/evp.h>
#include<vector>
using namespace std;
// Function to decrypt data using AES CBC mode
string decryptData(const string& ciphertext, const unsigned char* key, const unsigned char* iv) {
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), nullptr, key, iv);
int plaintextLen = ciphertext.length() + EVP_MAX_BLOCK_LENGTH; // Make room for padding
unsigned char* plaintext = new unsigned char[plaintextLen];
int len;
EVP_DecryptUpdate(ctx, plaintext, &len, reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.length());
int plaintextLen1 = len;
// Finalize decryption
int finalLen;
EVP_DecryptFinal_ex(ctx, plaintext + len, &finalLen);
plaintextLen1 += finalLen;
string decryptedData(reinterpret_cast<char*>(plaintext), plaintextLen1);
delete[] plaintext;
EVP_CIPHER_CTX_free(ctx);
return decryptedData;
}
int AES_decrypt(vector<string>& encrypted_files) {
// Read key from the key file
ifstream keyFile("key.txt", ios::binary);
if (!keyFile.is_open()) {
cerr << "Error: Unable to open key file." << endl;
return 1;
}
unsigned char key[16];
keyFile.read(reinterpret_cast<char*>(key), sizeof(key));
keyFile.close();
for(string file:encrypted_files){
// Read IV and ciphertext from the encrypted file
ifstream inputFile(file+".dat", ios::binary);
if (!inputFile.is_open()) {
cerr << "Error: Unable to open encrypted file." << endl;
return 1;
}
unsigned char iv[16];
inputFile.read(reinterpret_cast<char*>(iv), sizeof(iv));
string ciphertext((istreambuf_iterator<char>(inputFile)), (istreambuf_iterator<char>()));
inputFile.close();
// Decrypt the ciphertext using AES CBC mode
string decryptedText = decryptData(ciphertext, key, iv);
// Write the decrypted plaintext to a file
ofstream outputFile(file+".txt");
if (!outputFile.is_open()) {
cerr << "Error: Unable to create output file." << endl;
return 1;
}
outputFile << decryptedText;
outputFile.close();
}
cout << "Decryption of files completed successfully.\n" << endl;
return 0;
}
/*
g++ -o program decrypt.cpp -lssl -lcrypto
./program
g++ -o program client_main.cpp -lssl -lcrypto
*/