Проблема, которую я получаю, состоит в том, что файл, который я пытаюсь ввести в алгоритм AES, кажется, не читает его вообще или читает его, но не весь. Я использую фиктивный файл mynikko для проверки шифрования. Это видео, которое я использовал, чтобы получить помощь с Cryptopp: https://www.youtube.com/watch?v=glFHKB8Y10Y
Я исследовал, и этот метод не работает правильно с большими файлами. Я попытался превратить файл в файл ASCII и выяснить, как это сделать, но из-за программы, которую я использую, это может быть проблемой.
#include "Debug\cryptopp820\aes.h"
#include "Debug\cryptopp820\cryptlib.h"
#include "Debug\cryptopp820\filters.h"
/**/#include "Debug\cryptopp820\osrng.h"
#include "Debug\cryptopp820\hex.h"
#include "Debug\cryptopp820\modes.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <fstream>
#include <sstream>
#include <streambuf>
using namespace std;
using namespace CryptoPP;
using CryptoPP::AutoSeededRandomPool;
using CryptoPP::StringSink;
using CryptoPP::StreamTransformation;
using CryptoPP::StreamTransformationFilter;
using CryptoPP::AES;
using CryptoPP::ECB_Mode;
//1.Initialization of key data
void InitKey(byte* key, size_t size) {
for (size_t i = 0; i < size; ++i) {
key[i] = rand();
}
}
void main()
{
for (int p = 1; p < 8 + 1; p++) {
string location = "Texts/Decrypted/";
string extension = ".txt";
stringstream sas;
sas << location << p << extension;
ofstream outfiledone(sas.str().c_str());
for (int i = 1; i < 100 + 1; i++) {
clock_t t;
t = clock();
//Initialize common key and IV with appropriate values
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
byte iv[CryptoPP::AES::BLOCKSIZE];
// Initialize common key and IV with appropriate values
InitKey(key, sizeof(key));
InitKey(iv, sizeof(iv));
string str_enc;
str_enc = "Texts/ToBeEncrypted/";
stringstream ss;
ss << p;
str_enc += ss.str();
str_enc += ".txt";
ifstream ifs(str_enc);
string plainText((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());
cout << "Plain Text : " << plainText << endl;
//Create an encrypted object
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption enc;
enc.SetKeyWithIV(key, sizeof(key), iv);
string encText;
CryptoPP::StreamTransformationFilter encFilter(enc, new CryptoPP::StringSink(encText));
// encryption
encFilter.Put(reinterpret_cast<const byte*>(plainText.c_str()), plainText.size());
encFilter.MessageEnd();
cout << "Encrypted Text : " << encText << endl;
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption dec;
dec.SetKeyWithIV(key, sizeof(key), iv);
//Creation of conversion filter for decryption
string decText;
CryptoPP::StreamTransformationFilter decFilter(dec, new CryptoPP::StringSink(decText));
decFilter.Put(reinterpret_cast<const byte*>(encText.c_str()), encText.size());
decFilter.MessageEnd();
t = clock() - t;
cout << "Decrypted Text : " << decText << endl;
printf("It took me %d clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
cout << i << " out of 100 done" << endl;
cout << "File: " << p << extension << " of 8" << endl;
outfiledone << "Test No " << i << ": " << t << " Clicks --------- Time: " << ((float)t / CLOCKS_PER_SEC) << " MS" << endl;
}
outfiledone.close();
}
system("PAUSE");
}
Мои ожидаемые результаты должны были бытьчем больше файл, тем больше времени потребуется для шифрования. Тем не менее, это не так, поскольку шифрование, похоже, больше не работает в процессе шифрования больших файлов. Это неправильно после исследования этого, но я не знаю, как это исправить.