public static string Decrypt(string settingsFileName, string digraphFileName, string message)
        {
            MonthlySettings _monthlySettings = null;

            FileInfo fi = new FileInfo(settingsFileName);

            if (fi.Exists)
            {
                if (fi.Extension.ToLower() == ".xml")
                {
                    _monthlySettings = MonthlySettings.Open(settingsFileName);
                }
                else
                {
                    string fileContent = fi.OpenText().ReadToEnd();
                    _monthlySettings = MonthlySettings.Parse(fileContent);
                }
            }

            string[]        lines   = BreakMessageIntoGroups(message);
            string[]        headers = GetHeaders(lines);
            List <string[]> parts   = GetParts(headers, lines);

            string[,] digraphs = Utility.ParseDigraphTable(digraphFileName);

            List <string> decrypts = new List <string>();

            for (int i = 0; i < headers.Length; i++)
            {
                decrypts.Add(DecryptPart(headers[i], _monthlySettings, parts[i], digraphs));
            }

            return(string.Concat(decrypts));
        }
Example #2
0
        public void CheckMonthlySettingParsingAndEquality()
        {
            MonthlySettings monSet = MonthlySettings.Random(2000, 1, MachineType.M3);
            MonthlySettings check  = MonthlySettings.Parse(monSet.ToString());

            Assert.IsTrue(monSet == check);

            monSet = MonthlySettings.Random(2000, 1, MachineType.M3K);
            check  = MonthlySettings.Parse(monSet.ToString());
            Assert.IsTrue(monSet == check);

            monSet = MonthlySettings.Random(2000, 1, MachineType.M4K);
            check  = MonthlySettings.Parse(monSet.ToString());
            Assert.IsTrue(monSet == check);
        }
        public NavyMessage(string settingsFileName, string digraphFileName, string input, int day, string callSign)
        {
            MonthlySettingsFileName = settingsFileName;
            PlainText = input;
            Day       = day;

            List <NavyMessagePart> _innerParts = new List <NavyMessagePart>();

            FileInfo fi = new FileInfo(MonthlySettingsFileName);

            if (fi.Exists)
            {
                if (fi.Extension.ToLower() == ".xml")
                {
                    _monthlySettings = MonthlySettings.Open(MonthlySettingsFileName);
                }
                else
                {
                    using (StreamReader rdr = fi.OpenText())
                    {
                        string fileContent = rdr.ReadToEnd();
                        _monthlySettings = MonthlySettings.Parse(fileContent);
                    }
                }
            }

            string cleanInput  = Utility.CleanString(input);
            string paddedInput = Utility.GetPaddedString(cleanInput, 4);

            string[] parts = Utility.GetMessageParts(paddedInput, 4, 96);
            string[,] digs = Utility.ParseDigraphTable(digraphFileName);

            Settings s = _monthlySettings.DailySettings.FirstOrDefault(ss => ss.Day == Day);

            if (s == null)
            {
                s = _monthlySettings.DailySettings.Last();
            }

            for (int i = 0; i < parts.Length; i++)
            {
                _innerParts.Add(new NavyMessagePart(s, callSign, parts[i], i, parts.Length, digs));
            }

            Parts = _innerParts;
        }
        public ArmyMessage(string settingsFileName, string input, int day)
        {
            MonthlySettingsFileName = settingsFileName;
            PlainText = input;
            Day       = day;

            List <ArmyMessagePart> _innerParts = new List <ArmyMessagePart>();

            FileInfo fi = new FileInfo(MonthlySettingsFileName);

            if (fi.Exists)
            {
                if (fi.Extension.ToLower() == ".xml")
                {
                    _monthlySettings = MonthlySettings.Open(MonthlySettingsFileName);
                }
                else
                {
                    string fileContent = fi.OpenText().ReadToEnd();
                    _monthlySettings = MonthlySettings.Parse(fileContent);
                }
            }

            string cleanInput  = Utility.CleanString(input);
            string paddedInput = Utility.GetPaddedString(cleanInput, 5);

            string[] parts = Utility.GetMessageParts(paddedInput, 5, 49);

            Settings s = _monthlySettings.DailySettings.FirstOrDefault(ss => ss.Day == Day);

            if (s == null)
            {
                s = _monthlySettings.DailySettings.Last();
            }

            for (int i = 0; i < parts.Length; i++)
            {
                _innerParts.Add(new ArmyMessagePart(s, parts[i], i, parts.Length));
            }

            Parts = _innerParts;
        }