Tuesday, May 6, 2008

Project Euler p59 - breaking encryption

Problem definition:
------------------------------------------------
Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.

Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher1.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.
------------------------------------------------

C# brute-force solution, running in 42 seconds:



static byte[] ReadCipher(string file)
{
StreamReader sr = new StreamReader(file);
string[] buf = sr.ReadToEnd().Trim().Split(",".ToCharArray());
byte[] res = Array.ConvertAll(buf, s => { return byte.Parse(s); });
sr.Close();

return res;
}

static byte[] TryDecipher(byte[] cip, string key)
{
int ind = 0;
byte[] dec = new byte[cip.Length];

for (int i = 0; i < cip.Length; i++)
{
dec[i] = (byte)((int)cip[i] ^ (int)key[ind]);
ind = (ind >= 2) ? 0 : ind + 1;
}

return dec;
}

static int KeywordsInText(string text, string[] keywords)
{
int kc = 0;

for (int i = 0; i < keywords.Length; i++)
{
if (text.ToLower().IndexOf(" " + keywords[i] + " ") > -1) kc++;
}

return (kc*100) / keywords.Length;
}

static void Decipher(string file)
{
string[] keywords = new string[] {"the","be","is","are","was","to","of","and","a","in","into","that","have","has","had","I","it","for","not","on","with","he","as","you","do","did","done","at","this","but","his","by","from","they","we","say","her","she","or","an","will","shall","my","one","all","would","should","there","here","their","what","so","up","out","if","about","who","get","got","which","go","went","gone","me"};
string passchars = "qwertyuioplkjhgfdsazxcvbnm";
string curkey = "";
byte[] cipher = ReadCipher(file);
byte[] decoded = new byte[cipher.Length];
string dectext;
int keywordcount = 0;
int maxkeywords = 0;
string bestkey = "";
byte[] bestbytes = new byte[cipher.Length];
string besttext = "";
long bytesum = 0;

for (int i1 = 0; i1 < passchars.Length; i1++)
{
for (int i2 = 0; i2 < passchars.Length; i2++)
{
for (int i3 = 0; i3 < passchars.Length; i3++)
{
curkey = passchars[i1].ToString() + passchars[i2].ToString() + passchars[i3].ToString();
decoded = TryDecipher(cipher, curkey);
dectext = ASCIIEncoding.ASCII.GetString(decoded);
keywordcount = KeywordsInText(dectext, keywords);

if (keywordcount > maxkeywords)
{
maxkeywords = keywordcount;
bestkey = curkey;
bestbytes = decoded;
besttext = dectext;
}
}
}
}

for (int i = 0; i < bestbytes.Length; i++)
{
bytesum += (long)bestbytes[i];
}

Console.WriteLine("Byte sum: {0}\nPassword: {1}\nDecoded message 50 chars: {2}\nMessage has {3}% common words",
bytesum,bestkey,besttext.Substring(0,50),maxkeywords);
}

static void Main(string[] args)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Decipher("C:\\ProjectEuler\\cipher1.txt");
sw.Stop();
Console.WriteLine("Decryption time: {0} seconds",sw.Elapsed.Seconds);
Console.ReadLine();
}


No comments:

Post a Comment

Comment will be posted after comment moderation.
Thank you for your appreciation.