简体   繁体   中英

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.

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.

Properly encrypting it will generally create large values that don't really suit your needs. You could look at something like converting the value eg to base 36

http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/decimal-to-base-36/

For example take a time of 10:50 you could translate this to something like 9991050, giving a user code of 5Y556. You could then convert it back to decimal and parse it accordingly.

This isn't really encryption as such, but it may suit your needs.

Here are some variants on the Rot13 idea. The Rot39 also scrambles only a subset of the characters, and it is not simple arithmetic like Caesar.

public static string Rot47(string input)
{
    return !string.IsNullOrEmpty(input) ? new string(input.Select(x =>
        (x >= 33 && x <= 126) ? (char)((x + 14) % 94 + 33) : x).ToArray()) : input;
}


public static string Rot13(string input)
{
    return !string.IsNullOrEmpty(input) ? new string(input.Select(x =>
        (x >= 'a' && x <= 'z') ? (char)((x - 'a' + 13) % 26 + 'a') :
        (x >= 'A' && x <= 'Z') ? (char)((x - 'A' + 13) % 26 + 'A') : x).ToArray()) : input;
}


public static string Rot39(string input)
{
    // This string contains 78 different characters in random order.
    var mix = "QDXkW<_(V?cqK.lJ>-*y&zv9prf8biYCFeMxBm6ZnG3H4OuS1UaI5TwtoA#Rs!,7d2@L^gNhj)EP$0";
    var result = (input ?? "").ToCharArray();
    for (int i = 0; i < result.Length; ++i)
    {
        int j = mix.IndexOf(result[i]);
        result[i] = (j < 0) ? result[i] : mix[(j + 39) % 78];
    }
    return new string(result);
}

It would probably take a good hacker about 15 minutes to figure out Rot39 using black box testing.

First of all, I would recommend you to implement aa mechanisms, that sends you the information needed through a secure connection automatically. So the user is not involved in this and there for not (easily) able to manipulate something.

It this is not possible for some kind of reason and you only want to avoid that everyone is able manipulate it, there are some easy to implement possibilities: Eg Rot13 algorithm or a variation. Or you can also use some other simple "replace one digit/character by an other" algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM