I got it working by implementing the raw Integer method. See my code below, previous encryption is commented out. I don't know why but it wasn't working otherwise and *I think it has something to do with memory allocation maybe it works with -static flag because some references are staying in scope as the entire library is loaded into RAM ? *
I had to implement the PKCS1v15 again but I would like to know what is wrong so I can write the code accordingly. I have the Integer c. 1) If I use snippet 1, get the vector and then read it into a string "res" and return it, this works. 2) If I use snippet 2, aka do the exact same thing in the function. It doesn't work. I am trying to think whether the Vector needs to be declared with *new *? I am passing back the whole object not a pointer, so it should not go out of scope ?? But maybe the resarr2 is not a deep copy and when it goes out of scope the string data is also destroyed ?? On the other side, the serve complains with Snippet 2 and not with Snippet 1. Snippet 1: (works) std::vector<byte> resarr2 = convert_cryptopp_integer(c); string res(resarr2.begin(), resarr2.end()); return res; Snippet 2: (doesn't work) string res = convert_cryptopp_integer_str(c); return res; Functions: std::vector<byte> convert_cryptopp_integer(Integer n){ const size_t len = n.MinEncodedSize(Integer::UNSIGNED); std::vector<byte> v; v.resize(len); n.Encode((byte*)&v[0], v.size(), Integer::UNSIGNED); //std::cout << "Iostream: " << std::hex << n << std::endl; std::cout << "Iostream: " << n << std::endl; std::cout << " Vector: "; for(size_t i : v) { std::cout << (i & 0xff); } std::cout << " Done vector: " << endl; for(size_t i : v) { std::cout << int(i) << " "; } std::cout << " Done cout: " << endl; std::cout << std::endl; return v; } string convert_cryptopp_integer_str(Integer n){ std::vector<byte> resarr2 = convert_cryptopp_integer(n); string res(resarr2.begin(), resarr2.end()); return res; } std::string encrypt_rsa(std::string message, CryptoPP::RSA::PublicKey key) { try{ /* This was the previous code I couldn't get working without the -static flag cout << " In encrypt rsa string " << endl; std::string str(message.begin(), message.end()); message = b64encode(str); CryptoPP::AutoSeededRandomPool rng; //CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(key); CryptoPP::RSAES_PKCS1v15_Encryptor encryptor(key); std::string ciphertext; CryptoPP::StringSource(message, true, new CryptoPP::PK_EncryptorFilter(rng, encryptor, new CryptoPP::StringSink(ciphertext))); return ciphertext; */ std::string str(message.begin(), message.end()); string message = b64encode(str); cout << " Got message of length" << message.length() << endl; //convert the message to b64 string ts="\0"s; ts += "\2"s; //Add the 00 02 start header int target_length = key.GetModulus().ByteCount(); int msglength = message.length(); int padding_length = target_length - msglength - 3; //calculate the padding length for PKCS1 v1.5 SecByteBlock rand(padding_length); OS_GenerateRandomBlock(true, rand, padding_length); string rands; HexEncoder hex(new StringSink(rands)); hex.Put(rand, rand.size()); hex.MessageEnd(); std::regex reg("\0"); rands = std::regex_replace(rands,reg,"a"); rands.erase(padding_length); //generate random padding, replace any NULLs with "a" if(message.size()<key.GetModulus().ByteCount()){ ts += rands; //add the padding to the message ts += "\0"s; //NULL terminate the padding ts = ts + message; //add the payload } const char* msg_c = ts.c_str(); Integer m((const byte*)ts.data(), ts.size()); //convert it to Integer Integer c = key.ApplyFunction(m); //encrypt it size_t ensize = c.MinEncodedSize(); //get the size std::vector<byte> resarr2 = convert_cryptopp_integer(c); string res(resarr2.begin(), resarr2.end()); //This works //string res = convert_cryptopp_integer_str(c); //This line above doesn't work is it memory issue of the vector<byte> going out of scope?? return res; } catch(Exception e) { std::cout << "error encrypting RSA " << e.what(); return ""; } } On Tuesday, April 25, 2023 at 4:47:38 PM UTC-4 Dwight Kulkarni wrote: > Hi all: > > ldd ./server compiled without the -static flag shows following .so > dependencies. > > Since the -static flag will add the same libraries as .a form, it will > compile a slightly different version. I am trying to think why the > encryption would produce two different results, maybe something like big > endian or little endian treatment in a particular function ? Maybe > difference in the random generator ? Maybe byte size differences due to > typedef ? > > linux-vdso.so.1 (0x00007ffc5e0d7000) > libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 > (0x00007f8712987000) > libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f87125e9000) > libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f87123d1000) > libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 > (0x00007f87121b2000) > libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8711dc1000) > /lib64/ld-linux-x86-64.so.2 (0x00007f87131e2000) > > On Tuesday, April 25, 2023 at 2:47:34 PM UTC-4 Dwight Kulkarni wrote: > >> Further info: >> >> *without -static*, this is the output from the remote server: >> >> broker got data of size 512 >> Broker handler thread started... 512 >> first two bytes are 128 and 198 and 154 >> *clear text begins with 68 180 -------------->> this should be 00 02* >> Either no 00 02 beginning block or no 00 separator byte found in proper >> range >> Decryption failed >> Exception occurred: 'NoneType' object has no attribute 'decode' >> >> >> with -static flag, this is the output from the remote server: >> >> broker got data of size 512 >> Broker handler thread started... 512 >> first two bytes are 51 and 252 and 242 >> *clear text begins with 0 2 -------------> this is the correct value* >> Got base64 bytes >> >> b'eyJtZXNzYWdlIjoiMSIsInRybiI6MzM0NjQ1NjksInNlc3Npb24iOiJzc2Vzc2lvbiIsInBhc3Njb2RlIjoic3Bhc3Njb2RlIiwiZGF0ZXRpbWUiOiIwNC8yNS8yMDIzIDE4OjE1OjA0LjEwOCIsInNlcmlhbCI6InNzZXJpYWwiLCJjYW1lcmFfcG9ydCI6NTAwMCwia2V5IjoiTURObE1XWTBOMlUyTnpFNE1HTXdaak5pWkRZeE1UZ3haVGcyT0dFd05XST0iLCJpdiI6Ik9ETmtZMk0zTVdJek5UUmtaVFUyTUROak1HVTRaakptWlRNME5UQmhZMk09IiwidCI6Mn0=' >> initializing the lock... >> >> On Tuesday, April 25, 2023 at 2:33:23 PM UTC-4 Dwight Kulkarni wrote: >> >>> Hi all, >>> >>> I have my RSA encrypt function as below. >>> >>> I compile my program with the following: >>> >>> g++ -g -c -static -pthread -I../ >>> -I/data/prj/external-libs/include/cryptopp/ >>> ../src/threading/server.cpp >>> >>> g++ -g ../lbin/*.o -static -pthread -o server >>> -L/data/prj/external-libs/lib/ >>> -l:libcryptopp.a >>> >>> Here is what is strange. >>> >>> If I include the -static flag, when I run the encrypt_rsa command below >>> on the remote server it correctly decrypts. >>> >>> If I remove the -static flag, on the remove server it doesn't get the >>> proper message. However, the message that locally encrypted and decrypted >>> still works. >>> >>> It doesn't throw any error, encryption completes, but when the server >>> receives it, it doesn't decrypt properly. >>> >>> During compilation of the .a library from makefile it did not have a >>> -static flag. >>> >>> >>> >>> >>> std::string encrypt_rsa(std::string message, CryptoPP::RSA::PublicKey >>> key) >>> { >>> >>> try{ >>> cout << " In encrypt rsa string " << endl; >>> message = b64encode(message); >>> CryptoPP::AutoSeededRandomPool rng; >>> >>> //CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(key); >>> CryptoPP::RSAES_PKCS1v15_Encryptor encryptor(key); >>> std::string ciphertext; >>> CryptoPP::StringSource(message, true, new CryptoPP::PK_EncryptorFilter( >>> rng, encryptor, new CryptoPP::StringSink(ciphertext))); >>> return ciphertext; >>> >>> } >>> catch(...) >>> { >>> std::cout << "error encrypting RSA"; >>> return ""; >>> } >>> } >>> >> -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/29701ff1-f5eb-4b2c-b66d-ae6a0f95bc61n%40googlegroups.com.