-5

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
    Welcome to stack overflow. Please take a [tour], read [ask] and how to create a [mcve]. Don't post picture when you can post text and please avoid "Need help", "I have a little problem, with this and I wanted to know if any of you could fix it will be very appreciated !", "Hi everyone today" and never do this "If you can help me to fix I will donate over paypal :)". Thanks. – Stargateur Dec 21 '16 at 04:41
  • So, skip printing every other one. – Retired Ninja Dec 21 '16 at 04:43
  • 1
    Do not destroy your question after you have answers. That is absolutely not kosher on SO. – Jonathan Leffler Dec 21 '16 at 06:15
  • You can't ask for help and try to hide your question after. Your new edit don't change the meaning of your question. But this is really suspicious. If you put some private information you can read this https://stackexchange.com/legal/terms-of-service#15CopyrightPolicy. – Stargateur Dec 24 '16 at 11:30

1 Answers1

3

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);
selbie
  • 100,020
  • 15
  • 103
  • 173