Category Archives: C#

Storing encrypted strings in memory

Whenever you want to store values as string in memory, whilst at the same time prevent external tools from either finding the value and changing it. e.g. changing high score values. You can use the built in System.Security namespace to do so, the following code shows an example of this.

using System.Security;
using System.Runtime.InteropServices;

SecureString secureStr = new SecureString();

int Score
{
    get {
        var bStr = Marshal.SecureStringToBSTR(secureStr);
        var scoreStr = Marshal.PtrToStringBSTR(bStr);
        Marshal.FreeBSTR(bStr);
        var score = int.Parse(scoreStr);
        return score;
    }
    set {
        secureStr.Clear();
        var chars = value.ToString().ToCharArray();

        foreach (var c in chars)
        {
            secureStr.AppendChar(c);
        }
    }
}

The caveat to this is that its only supported currently in desktop, so if you’re planning on using this code inside unity it wont work for mobile and web as they dont have Marshalling available within their JIT compilers.

If desktop is not an option, alternatively you can hide the value using simple crytography. In its simplest form this can be achieved by means of XOR’ing the value. To allow it to work, a random secret private key is chosen and this is then used to encrpyt and decrypt the value. The following code snippet shows how

private int key = 9999;

int score;

int Score
{
    get { return score ^ key; }
    set { score = value ^ key; }
}

An even better way is to randomise the key when you store the value, as shown below

int Score
{
    get { return score ^ key; }
    set { 
        key = GetRandom(1, 9999999);
        score = value ^ key; 
    }
}

int GetRandom(int min, int max)
{
    Random rnd = new Random(); 
    return rnd.Next(min, max);
}

This way even if they do stumble across the value, the next time they look for the same value it will have changed. Its fast and its also available on most if not all platforms.

Yr1 final assignment – game prototyping

The prerequisite of the final exam is that we form small teams of 2-3 and create a game prototype, this ideally could turn into a full fledged game later on if we decided to pursue it, although the assignment was not designed for this, it was indeed possible. Time was allocated at the beginning to go off for a few days to design and document the requirements of the game and return to present it in front of both classes and teachers. On those first few days we decided it would be best if we each proposed an idea on paper and then present this to one another and the best idea would then be further developed and presented to class. I suggested we create a 1990’s mario kart type game, mainly because I find the technology of mode 7 interesting and it wasn’t out of my capabilities at this stage of our learning. However, others in my group didnt have the strong programming experience I had acquired and were unsure it was even possible given the time frame of six weeks, which is understandable so I tried alleviating their concerns by creating a bare bones prototype. They were impressed and it did sway them towards it more.

However, after all members presented their game ideas one game stood out as both feasible and did not discriminate individual group abilities, so it was chosen. The game itself was to be a 2D platformer based on similar mechanics as n-game where time was a factor in completing the game successfully. In devising the architecture we decided to instead of using SDL again a newer framework discussed recently in class called SMFL was chosen for the engine. With enough programmers on the team and having completed the C# waypoint editor we decided to also create a tile editor using C# to aid us when designing the levels.

Upon presenting the game to class our teachers provided encouraging feedback highlighting that this game was both feasible and that others should follow this concept when creating their games. This was very motivating to hear and with that I was placed in designing the editor and the others would work on the SMFL. I decided early to place aside no more than a week or two worth of development time in creating the editor, any more seems meaningless as the editor was not the final product. The sooner I completed my part the sooner I could then assist with the SMFL side of the game.

It has now been one week coming into two since the assignment was handed to us. I can report that the editor is now working, it is reading from an external file, tiles are then updatable via the tool and finally changes are being saved out to the same or new file. There are a few notable bugs to iron out, nothing major, at this point I dont see them as showstoppers as the tool is working as expected. I am now starting to see what the others have done on the SFML side of things. From what I can gather it looks fairly good. We have a working menu and a moving player and now all thats needed is the level class to read from my editor level file. After that is done, the next milestone will be implementing collision detection.