encryption/decryption problem

my goal would be to create an encrypted communication between a client and server

c strings and std::string use the null terminating character as the end of the string. when encryption is done, there may be a chance that it encrypts a char to a '\0' and the string prematurely terminates.

My friend told me that it would be best to convert each char in the string to a hex number, perform the encryption on that number and then concatenate the number as a string to send it across the network. The server side would then parse the string containing hex numbers into chars once again to reconstruct the original string.

How would I go about doing the conversion from char to hex and then hex back to char?
Wouldn't it be simpler to just send the length of the string as an integer before sending the actual data?
std::string does not use the '\0' as a terminator. It is quite safe to store '\0' values in the middle of the std::string.
Last edited on
Wouldn't it be simpler to just send the length of the string as an integer before sending the actual data?


I actually did that but I thought that my friend's method was more elegant and would like to know how it is to be done
Last edited on
std::string does not use the '\0' as a terminator. It is quite safe to store '\0' values in the middle of the std::string.


when using berkeley sockets, I eventually have to convert it to a c string in order to send and receive, and that is where the information is lost
when using berkeley sockets, I eventually have to convert it to a c string in order to send and receive, and that is where the information is lost

I don't see why. How are you sending it?
Last edited on
Here's a way to do the conversion (if I understand correctly what you want):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string str;
    string str_hex;

    cout << "enter a string:\n\n";
    getline(cin,str);

    stringstream buffer;
    buffer<<hex;

    for (int i=0; i<str.size(); i++)
    {
        buffer << (int)str[i];
        buffer << '.';
    }

    buffer << (int)'\0';

    str_hex=buffer.str();
    cout << '\n' << str_hex << endl;

    cout << "\nhit enter to quit";
    cin.get();
    return 0;
}

Though, I'd stick to the way you originally did it. It's simpler and faster.
thanks to master roshi but would doing my original way compromise the confidentiality of my message since the length is sent in plaintext or do I encrypt the length too?
unregistered wrote:
do I encrypt the length too?

That would be a good idea.

There are other tricks you could also do for increased protection. Depending on the size of the message, you could split it in parts and send it with the following format:

number_of_parts, σ(1), σ(2), ..., σ(number_of_parts),
partσ(1)_length, partσ(1)_data, partσ(2)_length, partσ(2)_data, ...

where σ(i) is a random permutation on the set {1,2,3,...,number_of_parts}

Of course, all of the above will be encrypted.
Last edited on
thanks, but as for the solution for converting the original string to the hex string you gave, how do I convert the hex string back to the original string?
Last edited on
Topic archived. No new replies allowed.