using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace CrackCode { class Program { static Regex simpleword = new Regex(@"^[A-Za-z]+$", RegexOptions.Singleline); static string code = @"TLYRHCHGTIYZLTEEHGDTBDNTRHJDNYCMDRPRVCPZPRATODCGGJYTAYHDQJYZGJZYYLJPESZYRJYZYLYPWYHGJYQDEEDBPRAZYMEFPQFDCNCEGPMEFGJYPZTAYHFDCAYGHYWYRGFGBDQCZGJYZNDZYGJYHCNDQGJYPZTAYHPHGJYJDCHYRCNOYZGJYLYRHCHGTIYZEDDIHTGGJYSDDZTRSLDRHPSYZHGJYHPGCTGPDRQDZTNDNYRGPSDRGGJPRIPLTRBDZIPGDCGQZDNGJTGLTRFDCAPWYNYHDNYNDZYPRQDZNTGPDRMEYTHWJYTHIHHCZYGJPRAZYMEPYHGJYBDNTRGJTGHNFDESYHGLJPESCMHGTPZHMETFPRAGJYMPTRDGJYLYRHCHGTIYZHNPEYHGJTRIHGJYBDNTRTRSBTEIHTBTFJTMMFJDBDESTZYGJYLJPESZYR"; static string[] dict = null; static Node root = null; static void Main(string[] args) { dict = (from word in File.ReadAllLines("brit-a-z.txt") where simpleword.IsMatch(word) select word) .Where(w => w.Length != 1 || (w.Length == 1 && (w.ToLower() == "i" || w.ToLower() == "a"))) .ToArray(); Trie t = new Trie(); foreach (string word in dict) { t.Insert(word); } root = t.Root; Dictionary mappings = new Dictionary(); mappings.Add('Y', 'e'); mappings.Add('T', 'a'); mappings.Add('Z', 'r'); //mappings.Add('L', 'c'); //mappings.Add('P', 'i'); //mappings.Add('E', 'l'); //mappings.Add('R', 'n'); //mappings.Add('S', 'd'); //mappings.Add('G', 't'); //mappings.Add('J', 'h'); Recurse(0, mappings, root); } static int last_level = 1; static DateTime last_print = DateTime.Now; static void Recurse( int start, Dictionary mapping, Node state, string decoded = "", int level = 0) { if( last_print.AddSeconds(3) < DateTime.Now ) { PrintConsole(start, decoded, mapping); } if (start == code.Length ) { if (state.Last) { PrintConsole(start, decoded, mapping); } return; } if (state.Last) { var chars = decoded.ToCharArray(); /// heuristic break condition if ( !(decoded.Length >= 4 && chars[decoded.Length - 2] == ' ' && chars[decoded.Length - 4] == ' ' || decoded.Length == 3 && chars[1] == ' ' )) { /// special case for smaller string Recurse(start, mapping, root, String.Format("{0} ", decoded), level); } } char cyp = code[start]; if (mapping.ContainsKey(cyp)) { char pln = mapping[cyp]; if (state.ChildNode(pln) == null) { return; } Recurse( start + 1, mapping, state.Children[pln], String.Format("{0}{1}", decoded, pln), level + 1); } else { foreach (char pln in state.Children.Keys) { // does it break the mapping? if (mapping.Values.Contains(pln)) // unmappable, someone pinched it { continue; } Dictionary copy = new Dictionary(mapping); copy[cyp] = pln; Recurse( start + 1, copy, state.Children[pln], String.Format("{0}{1}", decoded, pln), level + 1); } } } private static void PrintConsole(int start, string decoded, Dictionary mapping ) { Console.ForegroundColor = ConsoleColor.White; string encoded = String.Join( "",code.ToCharArray().Select( (e)=> mapping.ContainsKey(e) ? mapping[ e ] : '_' ) ); Console.WriteLine(encoded); last_print = DateTime.Now; } } }