Question
Below is the code I have written so far. It uses RSA/AES in Crypto++ (C/C++) in order to read/write a large file. RSA alone only
Below is the code I have written so far. It uses RSA/AES in Crypto++ (C/C++) in order to read/write a large file. RSA alone only focus on small files. I get the key, iv to generate but not the file to encrypt and decrypt and I am stuck.
Here at FileSource fs (" ", false); and here FileSource file(" ", true, new StringSink(str)) located below in the " " of both calls I inserted a file (LARGE File) consisting of paragraphs. I removed it because it was large to post, but it was just a regular .txt .pdf file.
Add/Edit what is necessary to the code below, so that it will Encrypt/Decrypt a large file with RSA/AES CBC mode for Crypto++? It should generate a random key and initial vector (iv). From the command line keyboard (Linux) after compiling (g++/gcc) type something like-- $./test [file to encrypt] [output file]-- to show Plaintext, and generated key , iv, and encrypted plaintext which should go in its own file. From that encrypted file, decrypt the encrypted file put decrypted output in its own file that show the recovered plaintext, which should be the same as the original plaintext? The file(s) to be encrypted and decrypted are large, big content, so its not like the RSA algorihm that only reads files of smaller byte lengths.
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include"cryptopp/cryptlib.h"
#include"cryptopp/hex.h"
#include"cryptopp/filters.h" #include"cryptopp/files.h" #include"cryptopp/des.h" #include"cryptopp/modes.h" #include "cryptopp/rsa.h" #include "cryptopp/sha.h" #include "cryptopp/osrng.h" #include "cryptopp/secblock.h" #include "cryptopp/aes.h" #include "cryptopp/eax.h" #include "cryptopp/integer.h" #include "cryptopp/hrtimer.h" #include "cryptopp/algparam.h" #include"cryptopp/ccm.h"
using namespace std; using namespace CryptoPP;
int main(int argc, char* argv[]) { AutoSeededRandomPool rng; vector plain, cipher, recover; HexEncoder encoder(new FileSink(cout)); SecByteBlock key (AES::DEFAULT_KEYLENGTH), iv (AES::BLOCKSIZE); InvertibleRSAFunction params; params.GenerateRandomWithKeySize (rng, 1024); memset(key, 0x00, sizeof(key)); memset(iv, 0x00, sizeof(iv));
RandomNumberSource rs1 (rng, AES::DEFAULT_KEYLENGTH, true, new ArraySink (key, key.size ())); RandomNumberSource rs2 (rng, AES::BLOCKSIZE, true, new ArraySink (iv, iv.size ())); string str; FileSource file(" ", true, new StringSink(str)); { cout << str << endl; }
cout << "key " << endl; encoder.Put (key, key.size ()); encoder.MessageEnd (); cout << "\"" << endl;
cout << "iv: " << endl; encoder.Put (iv, iv.size ()); encoder.MessageEnd (); cout << "\"" << endl; CBC_Mode < AES >::Encryption encryptor; encryptor.SetKeyWithIV (key, key.size (), iv, iv.size ()); SecByteBlock key2 (AES::DEFAULT_KEYLENGTH), iv2 (AES::BLOCKSIZE); FileSource fs (" ", false); // RSA decryption filter RSAES_OAEP_SHA_Decryptor rsaDecryptor(params); PK_DecryptorFilter rsaDecryptorFilter(rng, rsaDecryptor); // read and decrypt the AES key fs.Attach(new Redirector(rsaDecryptorFilter)); rsaDecryptorFilter.Attach(new ArraySink(key2, key2.size())); fs.Pump(rsaDecryptor.CiphertextLength(CryptoPP::AES::DEFAULT_KEYLENGTH)); rsaDecryptorFilter.MessageEnd(); ArraySink as2 (iv2, iv2.size ()); fs.Pump(rsaDecryptor.CiphertextLength(CryptoPP::AES::BLOCKSIZE));
CBC_Mode < AES >::Decryption decryptor; decryptor.SetKeyWithIV (key2, key2.size (), iv2, iv2.size ());
ByteQueue queue; fs.Detach (new StreamTransformationFilter (decryptor, new Redirector (queue))); fs.PumpAll ();
FileSink output(std::ostream &output); SecByteBlock block (queue.MaxRetrievable ()); ArraySink sink (block, block.size ()); queue.TransferTo (sink); string recovered;
queue.TransferTo (sink); cout << "iv: " << endl; encoder.Put (iv2, iv2.size ()); encoder.MessageEnd (); cout << "\"" << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started