Back to Top

Learn escape games programming

Uncovering secret messages

messages

What's an escape game without secret messages? Fortunately, we can easily encrypt and decrypt regular texts by making use of the many libraries that are available on the web. However, I wanted you to see the inner works of a simple, real-life encryption/decryption system that's built from scratch.

We are going to use only two functions. One of them will read an unencrypted text from a file, and then will output the encrypted version of it. Then, the second function will read the encrypted text from the file and will decrypt it, writing the unscrambled information to a text file.

Everything starts with a bunch of var and STRING* definitions, as always.

var number_of_characters;

var string_index;

var temp_value;

var fh_open_encrypt, fh_encrypt, fh_open_decrypt, fh_decrypt, fh_open, fh_decrypt;

var filehandle;

var eofile_reached = 0;

STRING* initial_str = "#300";

STRING* encrypted_str = "#300";

STRING* decrypted_str = "#300";

STRING* temp1_str =  "#300";

STRING* temp2_str =  "#300";

Function Encrypter() opens the MyData.txt file, which contains the unencrypted texts. You want to keep this piece of code and file for yourself; it stores the unencrypted information, so it will not be needed by the game code.

function Encrypter()

{

fh_open_encrypt = file_open_read("MyData.txt");

We wait for 3 frames (about 0.01 seconds), and then we start reading the records in the txt file until the end of file is reached (no more data records are available).

wait (3);

while (1)

{

eofile_reached = file_str_readto(fh_open_encrypt, initial_str, "\r\n", 1000);

if (eofile_reached != -1)

{

It's time to get the length of the string; we will go through all its characters, one at a time, by clipping the string from its beginning and cutting the remaining characters from its back. In other words, to access the 4th character in a string that's got a total of 10 characters, we will cut the first 3 chars and the last 6.

number_of_characters = str_len(initial_str);

string_index = 0;

str_cpy(encrypted_str, "");

while (string_index < number_of_characters)

{

str_cpy (temp1_str, initial_str);

str_clip(temp1_str, string_index);

str_trunc(temp1_str, number_of_characters - string_index - 1);

The first line of code below encrypts the current string character by adding 124 to its ASCII value. Since only the first 128 ASCII characters are needed for regular texts, you can add any value that ranges from 1 to 128 here. Don't forget to use the same value (124, in this example) in the decryption routine as well.

temp_value = str_to_asc(temp1_str) + 124;

str_for_asc(temp1_str, temp_value);

Once that the character has been set to its new ASCII value, we will recreate the encrypted string by adding the encrypted characters back to it, one at a time.

str_cat(encrypted_str, temp1_str);

string_index += 1;

}

Now that the encrypted string is safely stored in encrypted_str, we can write it to MyEncryptedData.txt – that's the file that should be shipped with your game.

fh_encrypt = file_open_append("MyEncryptedData.txt");

file_str_write (fh_encrypt, encrypted_str);

file_asc_write (fh_encrypt, 13);

file_asc_write (fh_encrypt, 10);

file_close(fh_encrypt); // close the file

}

else

{

break;

}

wait (1);

}

We also use the ASCII 13 and ASCII 10 codes (carriage return) to move on to the new data record in the file.

file_str_write (fh_encrypt, encrypted_str);

wait (1);

file_close(fh_encrypt); // close the file

wait(3);

file_close(fh_open_encrypt);

}

Finally, we close the file. That's all there is to know about the encryption mechanism, really!

Function Decrypter() opens the MyEncryptedData.txt file, which contains the encrypted information.

function Decrypter()

{

fh_open = file_open_read("MyEncryptedData.txt ");

while (1)

{

Then, it starts reading the encrypted records until the end of file is reached, using data chunks of 1000 bytes at a time. You can easily increase this value if your data records are bigger, of course.

eofile_reached = file_str_readto(fh_open, temp2_str, "\r\n", 1000);

if (eofile_reached != -1)

{

If the end of file hasn't been reached yet, the code gets the length of the data record, and then goes through each character, just like we did with the encryption code.

number_of_characters = str_len(temp2_str);

string_index = 0;

str_cpy(decrypted_str, "");

while (string_index < number_of_characters)

{

str_cpy (temp1_str, temp2_str);

str_clip(temp1_str, string_index);

str_trunc(temp1_str, number_of_characters - string_index - 1);

It's time to retrieve the correct ASCII values, so we'll subtract the same value that was added during the encryption stage. Then, we convert the resulting numerical value to its proper ASCII representation.

temp_value = str_to_asc(temp1_str) - 124;

str_for_asc(temp1_str, temp_value);

The resulting characters will then be added to the decrypted_str string, one at a time. In the end, the content of each decrypted_str row will be written to the MyDecryptedData.txt file.

str_cat(decrypted_str, temp1_str);

string_index += 1; string

}

fh_decrypt = file_open_append("MyDecryptedData.txt");

file_str_write (fh_decrypt, decrypted_str);

file_asc_write (fh_decrypt, 13);

file_asc_write (fh_decrypt, 10);

wait (1);

file_close(fh_decrypt);

}

else

{

break;

}

wait (1);

}

file_close(fh_open);

}