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
        public static Dictionary <char, char> GetOptimalCharacterMapping(Dictionary <char, double> textCharProportion,
                                                                         Dictionary <char, double> targetCharProportion,
                                                                         out double mappingDifference,
                                                                         out int mappingShiftAmount)
        {
            /*Mappings are { inputTextChar : actualChar }*/

            double minDifference = double.MaxValue;
            Dictionary <char, char> optimalMapping = null;
            int optimalShiftAmount = 0;

            for (int i = 0; i < 26; i++)
            {
                /*Create Character Mapping*/

                Dictionary <char, char> mapping = ProgramMath.GetCharacterMappingByCaesarCipherOffset((26 - i) % 26);

                /*Test character mapping*/

                Dictionary <char, double> mappedCharProportions = new Dictionary <char, double>();

                foreach (char key in textCharProportion.Keys)
                {
                    mappedCharProportions[mapping[key]] = textCharProportion[key];
                }

                double difference = ProgramMath.GetKeyFrequencyDifference(mappedCharProportions, targetCharProportion);

                /*Compare mapping to best*/

                if (difference < minDifference)
                {
                    minDifference      = difference;
                    optimalMapping     = new Dictionary <char, char>(mapping);
                    optimalShiftAmount = i;
                }
            }


            mappingDifference  = minDifference;
            mappingShiftAmount = optimalShiftAmount;
            return(optimalMapping);
        }