Sunday, 19 April 2009


Link to Project Euler problem 59

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.

Read in the string, split up the letters in a string array, generate every possible password and do the XOR (^) on the relevant chars, adding them to a List. Make a string of this and see if it contains " and " (with the spaces). Took a gamble on this and I'm sure there are better RegEx's to check for intelligent text, but as it's sunday today I'll write it off to divine inspiration (clue). Only one of the 17576 possible results seemed to contain " and ". The program prints it out (the password is interesting too ;-)).

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace project_euler
class Program
static void Main()
//Problem 59
//17576 possible combinations of letters
DateTime start = DateTime.Now;
StreamReader sr = new StreamReader(@"../../cipher1.txt");
int sum = 0;
List<List<char>> decodedValues = new List<List<char>>();
string s = sr.ReadToEnd();
string[] letters = s.Split(',');
Regex test = new Regex(" and ");
for (int i = 97; i < 123; i++)
for (int j = 97; j < 123; j++)
for (int k = 97; k < 123; k++)
List<char> decodedString = new List<char>();
int n = 0;
while (n < letters.Length)
decodedString.Add((char) (int.Parse(letters[n]) ^ i));
if (n + 1 < letters.Length)
decodedString.Add((char) (int.Parse(letters[n + 1]) ^ j));
if (n + 2 < letters.Length)
decodedString.Add((char) (int.Parse(letters[n + 2]) ^ k));
n += 3;
StringBuilder sb = new StringBuilder();
foreach (char c in decodedString)
string t = sb.ToString();
if (test.IsMatch(t))
string decoded="";
foreach (List<char> words in decodedValues)
foreach (char ch in words)
decoded += ch.ToString();
sum += ch;
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Triple-click for answer: 107359

No comments: