Reputation: 3
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.
Upvotes: 0
Views: 10857
Reputation: 11357
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.
Upvotes: 1
Reputation: 7360
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.
Upvotes: 2
Reputation: 1657
Properly encrypting it will generally create large values that don't really suit your needs. You could look at something like converting the value e.g 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.
Upvotes: 1
Reputation: 1034
First of all, I would recommend you to implement a a 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.
Upvotes: 0