Search code examples
c++socketsrsac-str

c_str() is terminating my encrypted message is there a way to bypass this?


I am encrypting a message using RSA.

Everything works fine if I try to encrypt/decrypt the message locally, however, when converting the string to void constant* in order to use the socket::send method like so,

int sendRes = send(socket, tempMessage.c_str(), tempMessage.size() + 1, 0);

the following happens,

My original string, tempMessage

"_��JE<D�p�-d�R�U���`}�+�;���|[�t�Ԕ���^;D�Mn��?ÕW>�-�6���Pr��[͉dZ%�"

is truncated to this, when using tempMessage.c_str()

"_��JE<D�p�-d�R�U���`}�+�;���|[�"

I do know this is because, during encryption, there is a \0 symbol that gets appended in between once or several times, terminating thus everything there when using c_str().

I also know it happens before using send(), I put it here so that you know why I am using c_str() in the first place.

It's there a way to bypass this? or a workaround?

I have seen this previous answer but did not help with my situation and could not find any other solution out there.

Thanks in advance for the help!


Solution

  • In the end, it was an easier fix.

    Since all messages are encrypted following the same type, the length of the bits will always be the same for the encrypted part.

    In my case, this was 256, meaning that I can now send-receive without any further issues since I now know the real string length.

    If this would not have been the case, I would have gone with @prog-fh answer