xSF.AzraeL xSF.AzraeL - 3 months ago 20
C# Question

C# very simple string encryption for game

I am making a small game on Unity using c#. It is basically a 2d side scrolling runner type game. I measure the time taken by each player to finish the game and it is stored on a float. I would like to put this in a string along with a salt word, and display an encrypted code to the player.

The code has to be small enough to be comfortably written down by the player. I should be able to decrypt the code which the player would send me, and figure out how much time he/she took to complete the game.

Please tell me how I should proceed in doing this. most of the algorithms i saw online are very complex and generate large blocks of encrypted text.

Thanks in advance.

Answer

Just do something like the Caesar cipher. For example:

for (int i = 0; i < message.Length(); i++)
{
    if (message[i] < 123 && message[i] > 96)
    {
        // 97 to 122 are 'a' to 'z'
        message[i] += 3;

        if (message[i] > 122)
        {
            message[i] -= 26;
        }
    }
}

EDIT: XOR-encryption:

string codeword = "word";
string res = "";
for (int i = 0; i < message.Length(); i++)
{
    res += (char)(message[i] ^ codeword[i % codeword.Length()]);
}

To decrypt it, just run the encryption again on the encrypted message.

Comments