# Hack This Site Realistic 6

I’ve been working my way through the missions on hackthissite.org, and for realistic 6 I needed to write a simple program to decrypt messages encoded using the XECryption algorithm.

I must admit that I had no idea how to go about decrypting the message, so I got a hint from here and then wrote my own javascript powered web page to decode the message.

I’ve put my script up here, not so that you can cheat, but in the hope that you might be able to learn something by viewing the source and testing it below, but first some notes:

• It is a quick and dirty script with no sanitising or error checking.
• It assumes that the most common character in the message is a space, so it will not work in all cases (e.g. single words), but should work in most realistic cases.
• It is not here so that you can cheat, it is here in order that you might be able to learn by looking at the JavaScript source code.

The source:

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 function XUcrypt(XEcryptString) { var XEcryptValues = XEcryptString.substring(1).split("."); //remove first "." character and put numbers into array var XEcryptChars = []; //create array for encrypted characters var modeMap = {}; //create map of array occurrences var maxCount = 1; //create count var for tracking highest var mode; //create mode var to keep track of which is the highest occurring character var decoded = ""; //create decoded var for the decoded string /*loop adds sum of each group of three numbers to array and creates a map of the values and the number of times they occur in order to calculate the mode-average which _should_ be the space character*/ for (var i = 0; i < XEcryptValues.length / 3; i++) { var j = 0; for (var k = 0; k < 3; k++) { j += parseInt(XEcryptValues[k+3*i]); } XEcryptChars[i] = j; if (modeMap[j] == null) { modeMap[j] = 1; } else { modeMap[j]++; if (modeMap[j] > maxCount) { maxCount = modeMap[j]; mode = j; } } } var key = mode-32; //the key is the number of the mode common encrypted charater minus the ASCII code for a space for (var i=0; i<XEcryptChars.length; i++) { //for every array entry, type the decoded ascii character decoded += String.fromCharCode(XEcryptChars[i]-key); } return decoded; }```

The demo:

Encoded message:

Decoded message: