I recently trying to mess around with RSA on a few files to gain a better understanding of how it works. However for one reason or another after the first decryption cycle on my code it seems to crash. I am a little bit in the dark as to why this is the case. If it is relevant the program was built and linked using the VS2012 x64 compiler which compiled without error.
-- -- You received this message because you are subscribed to the "Crypto++ Users" Google Group. To unsubscribe, send an email to cryptopp-users-unsubscr...@googlegroups.com. More information about Crypto++ and this group is available at http://www.cryptopp.com. --- You received this message because you are subscribed to the Google Groups "Crypto++ Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
/* A test program by Atton some elements taken from other sources 2015 */ #include <iostream> #include <string> // Linked via .lib file with VS2012 console #include "lib/rsa.h" #include "lib/modes.h" #include "lib/filters.h" #include "lib/files.h" #include "lib/hex.h" #include "lib/randpool.h" #include "lib/cryptlib.h" #include "lib/seed.h" #include "lib/osrng.h" using namespace CryptoPP; using namespace std; class Core { private: // Pretty much everything under private in this class is from the wiki. So far it all seems to work rather well. void Load(const string& filename, BufferedTransformation& bt) { // http://www.cryptopp.com/docs/ref/class_file_source.html FileSource file(filename.c_str(), true /*pumpAll*/); file.TransferTo(bt); bt.MessageEnd(); } void Save(const string& filename, const BufferedTransformation& bt) { // http://www.cryptopp.com/docs/ref/class_file_sink.html FileSink file(filename.c_str()); bt.CopyTo(file); file.MessageEnd(); } void SavePrivateKey(const string& filename, const PrivateKey& key) { // http://www.cryptopp.com/docs/ref/class_byte_queue.html ByteQueue queue; key.Save(queue); Save(filename, queue); } void SavePublicKey(const string& filename, const PublicKey& key) { // http://www.cryptopp.com/docs/ref/class_byte_queue.html ByteQueue queue; key.Save(queue); Save(filename, queue); } void LoadPrivateKey(const string& filename, PrivateKey& key) { // http://www.cryptopp.com/docs/ref/class_byte_queue.html ByteQueue queue; Load(filename, queue); key.Load(queue); } void LoadPublicKey(const string& filename, PublicKey& key) { // http://www.cryptopp.com/docs/ref/class_byte_queue.html ByteQueue queue; Load(filename, queue); key.Load(queue); } // Encode and Decode do what they are labled as void encode(string in,string &out,string keyFileName) { AutoSeededRandomPool rng; RSA::PublicKey publicKey; LoadPublicKey(keyFileName,publicKey); RSAES_OAEP_SHA_Encryptor e(publicKey); StringSource ss1(in, true, new PK_EncryptorFilter(rng, e, new StringSink(out) ) // PK_EncryptorFilter ); // StringSource } void decode(string in,string &out,string keyFileName) { AutoSeededRandomPool rng; RSA::PrivateKey privateKey; LoadPrivateKey(keyFileName,privateKey); RSAES_OAEP_SHA_Decryptor d(privateKey); StringSource ss2(in, true, new PK_DecryptorFilter(rng, d, new StringSink(out) ) // PK_DecryptorFilter ); // StringSource } public: // The title says it all void makeKeys() { AutoSeededRandomPool rnd; RSA::PrivateKey rsaPrivate; rsaPrivate.GenerateRandomWithKeySize(rnd, 3072); RSA::PublicKey rsaPublic(rsaPrivate); SavePrivateKey("rsa-private.Rkey", rsaPrivate); SavePublicKey("rsa-public.Rkey", rsaPublic); } void encodeFile(string inputFileName,string outputFileName,string keyFileName) { // Stuff that runs the file string in; size_t buffer_size = 342; string buffer(buffer_size,'\0'); ifstream fileStreamIn(inputFileName,ios::binary); ofstream fileStreamOut(outputFileName,ios::binary); while(fileStreamIn) { fileStreamIn.read(&buffer.front(), buffer_size); size_t count = fileStreamIn.gcount(); buffer.resize(count); encode(buffer,in,keyFileName); buffer = in; fileStreamOut.write(buffer.c_str(),buffer.length()); buffer.resize(buffer_size); buffer = ""; in = ""; if (!count) break; } fileStreamIn.close(); fileStreamOut.close(); } // This function for one reason or another seems to cause a crash void decodeFile(string inputFileName,string outputFileName,string keyFileName) { // Stuff that runs the file string in; size_t buffer_size = 384; string buffer(buffer_size,'\0'); ifstream fileStreamIn(inputFileName,ios::binary); ofstream fileStreamOut(outputFileName,ios::binary); while(fileStreamIn) { fileStreamIn.read(&buffer.front(), buffer_size); size_t count = fileStreamIn.gcount(); buffer.resize(buffer_size); // This bit of logic is to prevent crash if(count == buffer_size) { decode(buffer,in,keyFileName); } buffer = in; fileStreamOut.write(buffer.c_str(),buffer.length()); buffer = ""; in = ""; if (!count) break; } fileStreamIn.close(); fileStreamOut.close(); } }; void main () { Core Function; // Function.makeKeys(); Function.encodeFile("in","out","rsa-public.Rkey"); Function.decodeFile("out","out1","rsa-private.Rkey"); } // The build file using the .lib file /* call "F:\VISUALCPP\VC\vcvarsall.bat" x86_amd64 del main.exe del main.obj cl /EHsc /c main.cpp link /LTCG main.obj cryptlibX64.lib /out:main.exe del main.obj timeout /t 5 /nobreak */