private static string ConstructCurrentDeciphering(string[] selections,
                                                          int[] selectionShifts)
        {
            string[] currentSelections = new string[selections.Length];
            for (int i = 0; i < currentSelections.Length; i++)
            {
                currentSelections[i] = FrequencyAnalysis.DecipherTextByMapping(selections[i],
                                                                               ProgramMath.GetCharacterMappingByCaesarCipherOffset(selectionShifts[i]));
            }

            return(FrequencyAnalysis.ReconstructTextFromOffsetSelections(currentSelections));
        }
Пример #2
0
        static void DebugTextSplitAndReconstruct()
        {
            Console.Write("Text> ");
            string text = Console.ReadLine().ToUpper();

            Console.Write("Offset> ");
            int offset = int.Parse(Console.ReadLine());

            string[] newTexts = FrequencyAnalysis.SplitTextByOffset(text, offset);

            foreach (string newText in newTexts)
            {
                Console.WriteLine(newText);
            }

            string reconstruction = FrequencyAnalysis.ReconstructTextFromOffsetSelections(newTexts);

            Console.WriteLine(reconstruction);
        }
Пример #3
0
        static void ReleaseMain(string[] args)
        {
            Console.Write("Text> ");
            string text;

            string input = Console.ReadLine().ToUpper().Replace(" ", "").ToUpper();

            if (input[0] == '\\')
            {
                text = GetTextFromFile(input.Substring(1)).ToUpper();
            }
            else
            {
                text = input;
            }

            foreach (char c in text)
            {
                if (!validCharacters.Contains(c))
                {
                    Console.WriteLine($"Error: Invalid character in text ({c})");
                    return;
                }
            }

            int keyLengthSelection = KeyLengthSelection(text);

            Console.Write("Automatically decipher text (0/1)?> ");
            bool autoDecipher = int.Parse(Console.ReadLine()) != 0;

            if (autoDecipher)
            {
                string[] offsetTextSelections = FrequencyAnalysis.SplitTextByOffset(text, keyLengthSelection);

                string[] decipheredStrings = new string[offsetTextSelections.Length];
                int[]    shiftAmounts      = new int[offsetTextSelections.Length];

                for (int i = 0; i < decipheredStrings.Length; i++)
                {
                    string selection = offsetTextSelections[i];

                    double _;
                    Dictionary <char, double> selectionProportions = FrequencyAnalysis.CharFrequencyToCharProportion(FrequencyAnalysis.GetTextCharFrequency(selection));
                    Dictionary <char, char>   optimalMapping       = FrequencyAnalysis.GetOptimalCharacterMapping(selectionProportions,
                                                                                                                  EnglishLetterFrequency.GetLetterProportions(),
                                                                                                                  out _,
                                                                                                                  out shiftAmounts[i]);

                    decipheredStrings[i] = FrequencyAnalysis.DecipherTextByMapping(selection, optimalMapping);
                }

                string fullDeciphering = FrequencyAnalysis.ReconstructTextFromOffsetSelections(decipheredStrings);

                Console.WriteLine("Keyword: " + ProgramMath.GetKeywordFromOffsets(shiftAmounts));
                Console.WriteLine("Deciphered Text:");
                Console.WriteLine(fullDeciphering);
            }
            else
            {
                string keyword;
                string fullDeciphering = ManualMappingDeciphering.RunDeciphering(text,
                                                                                 keyLengthSelection,
                                                                                 out keyword);

                Console.WriteLine("Keyword: " + keyword);
                Console.WriteLine("Deciphered Text:");
                Console.WriteLine(fullDeciphering);
            }
        }