Category Archives: Windows

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.

Advertisements

Windows 7 Ultimate x64 Black Screen [fix]

So after working to the early mornings on my 6th and final assignment for first years, I awoke to find my macbook pro would no longer display the desktop, normally any movement of the mouse or a keypress and the desktop would fire up instantly, not today. I could see that the hard drive was still on and the keyboard lights had come on, the differences I noticed were that the famous Apple logo behind the monitor did not illuminate and of course the screen was black. I forced a reboot and jumped back into bootcamp, the windows logo would appear and then I would see the black screen. What has happened I thought.

I retried again this time booting into Mac OSX, things were ok here. Next I tried booting into Windows safe mode and I was able to get in and the screen desktop appeared. So again, I tried normally booting and the same issue remained.

Back into safe mode I decided to check the event logs. Looking for anything unusal I started in Applications. I did not identify anything in Applications so I continued looking in System. Here I noticed a repeated bunch of Adobe Flash issues suggesting shutdowns had occurred, as they were reoccurring logs I dismissed these as the issue causing the black screen. I continued scouring the logs further and I found an entry that suggested an automatic update had occured at around 3:30am.

Event Log screenshot

The entry was deemed critical and automatic:

Windows Defender automatic update KB15597 (Definition 1.139.936.0)

As this time was during my absence from my computer and after the last time the monitor was working correctly, I concluded that this event was deemed important, so I decided to run a system restore in hope to get Windows back to its previous state. Firing up system restore I found that a restore point had occured at exactly the same time the update had been logged in the event log. So things were looking promising.

System restore screenshot

As see from the screen above, I chose the 3:27 restore and let windows do it things. After waiting about 3-4 mins, the system rebooted and I got back into bootcamp, the windows logo appeared again and after it disappeared the win logon did appear this time and the screen was no longer black. Woohoo!