• Register
Post news RSS Devlog #006: XOR-encryption and how it works

I always thought encryption was a beast of complexity, but XOR-encryption is not so bad. Read about my latest developments and learn about XOR-encryption in the process.

Posted by on

I've been working on encryption.
I want to be sure the level and save data cannot easily be modified in a text editor. Currently the stored level data is completely human readable and can easily be modified in notepad.
To mask the data I've made an encryption and decryption function that will be called before writing all data to the file.
It's very interesting to see all different kinds of encryption, and I'm not afraid to tell you I've chosen for a simple XOR encryption.

Encryption always sounded so complicated, as if it would require hugely complex mathemetical functions, but this was not too bad.
I even took the time to write a small separate tool for en/de-crypting files!

Just to share my learnings, here's an explanation of how XOR-encryption works basically:

Say you want to encrypt a simple string "hello". This string is a group of letters which we as humans can read and understand. A computer however, cannot.
For a computer to understand data, everything in computerland is eventually represented by 1's and 0's.

This is because the computer consists of hardware that stores everything in states of either 'present' or 'not present', '1' or '0'. In hard drives, for example, these are represented by magnetized areas. Each one of those areas can be independently magnetized (to store a 1) or demagnetized (to store a 0).
Magnetism is used in computer storage because it goes on storing information even when the power is switched off. Metal keeps it's magnetized state.

Back to our example, the string "hello" would in computerland be represented by 01101000 01100101 01101100 01101100 01101111. Here each byte (8 bits) is representing a letter or character of the string.
To encrypt this with XOR encryption, we would first need a key string. Let's, for the sake of simplicity, choose "crypt" as key.
Now, when XOR encrypting, we simply change each bit of each letter based on the letter in the same position of the key string. But before we look at that result, let's first see what a XOR operator does.

XOR stands for exclusive OR, which compares 2 bits and only returns a 1 when one of both bits is 1.
These are the results of all possible XOR operations:

xor table

Source: Dyclassroom.com

Our text "hello" will be XOR'ed with the key "crypt":


The encrypted text 00001011 00010111 00010101 00011100 00011011 cannot be represented by standard ascii characters and will probably be represented in a text by " ".
So we've successfully encrypted our string, yay! However, we probably at some point want to use the stored information which is now unusable. We must first decrypt it!
This is where the simplicity of XOR-encryption shines. When we XOR our encrypted text with the key "crypt" again, we actually get back our original text!


Neat, huh!
Now, XOR-encryption is not the safest type of encryption by a long run, but for a simple game file it does the job well enough.

A level file in my game now looks like this:

Encrypted data

After some hard work I've gotten en- and decryption working and also integrated this with writing and reading from files through my generic engine classes.
Now I'll focus my efforts on storing a user settings file (which will not be encrypted) and linking the options menu to use this file.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.