0

I'm creating an encryption function with a bit operator on both sides of the server client.

There was a problem with changing CString to char. After debugging, the sizeof(arr) value is only imported to 8.

I need to encrypt the buff length of the data I received, but I only calculate 8 pieces

Also, if it is larger than the length of the key, we have to calculate it from the beginning, but there seems to be a problem with the length here.

What is the cause and how should I correct it?

Function

char Encryption(char strEncordeData[])
{
    m_socket_comm.encrypt[0] = '\0';
    int length = sizeof((char*)strEncordeData);
    int j = 0;
    for (int i = 0; i < length; i++)
    {
        m_socket_comm.encrypt[i] = strEncordeData[i] ^ m_socket_comm.key[i];
        if (i > sizeof(m_socket_comm.key) || j > sizeof(m_socket_comm.key))
        {
            m_socket_comm.encrypt[i] = strEncordeData[i] ^ m_socket_comm.key[j];
        }
        int j = i - sizeof(m_socket_comm.key);;

    }
    return *strEncordeData;
}

variable

char key[BUFSIZE] = "key";
char encrypt[BUFSIZE];
char decrypt[BUFSIZE];

Use sample

char pstr[BUFSIZE];
ZeroMemory(pstr, BUFSIZE);
m_edit_chat.GetWindowText(str2);
strcpy(pstr, str2);

for (int i = 0; i < m_socket_comm.client_list.size(); i++)
{
    tcpserver.ServCurSelSend(i, pstr, sizeof(pstr));
}
LieuRabbit
  • 17
  • 6
  • 5
    You can't, you have to pass the size as a function argument. – Barmar Aug 03 '22 at 02:35
  • 1
    sizeof gets the amount of space required to store the variable you give it. In your case, you passed a char* to sizeof, i.e., a pointer. The amount of space required to store a pointer on (most) 64 bit systems is 8 bytes. If you want the length of the string that was passed rather than the size of a pointer to that string, you can use strlen if the string is zero terminated or you have to pass the length of the string to the function. – couteau Aug 03 '22 at 02:42
  • 1
    For a parameter, `char strEncordeData[]` is equvialent to `char *strEncordeData`. As such, you are getting the size of a pointer. You need to pass the size of the array to the function. `sizeof` can't possibly work the way you expect since it's resolved at compile-time, yet the function can accept pointers to buffers of different sizes. – ikegami Aug 03 '22 at 02:52
  • The "Use sample" doesn't call `Encryption`?!?! – ikegami Aug 03 '22 at 02:53
  • Use strlen(pstr) instead of sizeof(pstr) – NeilB Aug 03 '22 at 08:15
  • @ikegami In the "Use sample" the Encryption is inside of servCurSelSend function – LieuRabbit Aug 03 '22 at 23:58
  • @couteau Thank you I'm going to use strlen – LieuRabbit Aug 04 '22 at 00:00

0 Answers0