Need help for strlen

159 views Asked by At

I have a problem, with this and I wanted to know if any of you could fix it will be very appreciated!

I am trying to encrypt a message to send to people in my game

Problem: When I try to send my message the 1st time gonna works and show the text on Letter and the second time is gonna show the encrypted text and I don't want people see that

("ZFDKWEK,CSJ,$MS(,,,)")

if (MessageEnc)
                    {
                    if (GET_GAME_TIMER() >= destroy_tick2){
                       destroy_tick2 = GET_GAME_TIMER() + 82000;
                        int i = 0;
                        for (i = 0;i<18;i++)
                        {
                        if (i == PlayerId()) continue;
                        int Handle = GetPlayerHandle(i);
                        if(!DoesEntityExist(Handle)) continue;
                        char key = 'j'; //Key
                        char *encMess = "ZFDKWEK,CSJ,$MS(,,,)";

                        for (int i = 0; i < strlen(encMess); i++)
                        {
                            encMess[i] ^= key;
                        }
                        char buf[64];
                    snprintf(buf, sizeof(buf), "~r~%s", encMess);
                    NETWORK::NETWORK_HANDLE_FROM_PLAYER(i,  &networkHandle, 13);
                    NETWORK::NETWORK_SEND_TEXT_MESSAGE(buf, &networkHandle);    
                    printf(encMess);                        
                    }
                    }
                    }
1

There are 1 answers

0
selbie On

Your XOR operation is encrypting the static string literal on every odd iteration and then decrypting it on ever even iteration. Use a tmp buffer to avoid modifying the string literal. I don't recall the rules, but I think that attempting to modify the memory contents of a string literal is entering undefined behavior territory. So it's best to avoid this type of coding to begin with.

Change this set of code:

char *encMess = ",Z8J'Z$/3J.8Z:J&Z((3J-ZJ9?(9)8#(/J>ZJ'3J3Z?>?(/PJ9!3+)8Z";

for (int i = 0; i < strlen(encMess); i++)
{
    encMess[i] ^= key;
}

char buf[64];
snprintf(buf, sizeof(buf), "~r~%s", encMess);

To this:

char tmp[64];
const char *encMess = ",Z8J'Z$/3J.8Z:J&Z((3J-ZJ9?(9)8#(/J>ZJ'3J3Z?>?(/PJ9!3+)8Z";

strcpy(tmp, encMess, 64);
size_t len = strlen(encMess);

for (int i = 0; i < len; i++)
{
    tmp[i] ^= key;
}

char buf[64];
snprintf(buf, sizeof(buf), "~r~%s", tmp);