#include <iostream>
#include <string>
#include <crypto++/aes.h>
#include <crypto++/modes.h>
#include <crypto++/filters.h>
#include <crypto++/osrng.h>
#include <crypto++/hex.h>
#include <crypto++/sha.h>
#include <crypto++/files.h>
using namespace std;
using namespace CryptoPP;
void testAES() {
cout << "--- Задание 2: AES Шифрование ---" << endl;
AutoSeededRandomPool prng;
CryptoPP::byte key[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key, sizeof(key));
CryptoPP::byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
string plainText = "Hello from MIPT Code of the Future!";
string cipherText, recoveredText;
cout << "Исходный текст: " << plainText << endl;
try {
CBC_Mode<AES>::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv);
StringSource s(plainText, true,
new StreamTransformationFilter(e, new StringSink(cipherText))
);
string hexCipher;
StringSource sHex(cipherText, true, new HexEncoder(new StringSink(hexCipher)));
cout << "Зашифрованный текст (HEX): " << hexCipher << endl;
} catch(const Exception& e) {
cerr << "Ошибка шифрования: " << e.what() << endl;
}
try {
CBC_Mode<AES>::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);
StringSource s2(cipherText, true,
new StreamTransformationFilter(d, new StringSink(recoveredText))
);
cout << "Расшифрованный текст: " << recoveredText << endl;
} catch(const Exception& e) {
cerr << "Ошибка расшифровки: " << e.what() << endl;
}
cout << endl;
}
void testFileHash() {
cout << "--- Задание 3: Сравнение хэшей файлов ---" << endl;
string file1, file2;
cout << "Введите путь к первому файлу (например, 1.txt): ";
cin >> file1;
cout << "Введите путь ко второму файлу (например, 2.txt): ";
cin >> file2;
string hash1, hash2;
SHA256 sha256;
try {
FileSource f1(file1.c_str(), true,
new HashFilter(sha256, new HexEncoder(new StringSink(hash1)))
);
cout << "Хэш первого файла: " << hash1 << endl;
} catch(...) {
cout << "Ошибка: не удалось открыть первый файл." << endl;
return;
}
try {
FileSource f2(file2.c_str(), true,
new HashFilter(sha256, new HexEncoder(new StringSink(hash2)))
);
cout << "Хэш второго файла: " << hash2 << endl;
} catch(...) {
cout << "Ошибка: не удалось открыть второй файл." << endl;
return;
}
if (hash1 == hash2) {
cout << "Результат: Хэши совпадают! Файлы одинаковые." << endl;
} else {
cout << "Результат: Хэши разные. Файлы отличаются." << endl;
}
}
int main() {
testAES();
testFileHash();
return 0;
}