コード例 #1
0
ファイル: Decode.cs プロジェクト: Ecclesiast/Crypto
        static bool IsComparable(string baseFre, string currentFre)
        {
            if (string.IsNullOrEmpty(baseFre) || string.IsNullOrEmpty(currentFre) || baseFre.Length != currentFre.Length)
            {
                return(false);
            }
            Cyphers caesar = new Cyphers();

            int[] baseT      = caesar.GetInputCode(baseFre);
            int[] currentT   = caesar.GetInputCode(currentFre);
            int   difference = 0;

            if (baseT[0] <= currentT[0])
            {
                difference = currentT[0] - baseT[0];
            }
            else
            {
                difference = caesar.GeneralLength - baseT[0] + currentT[0];
            }

            string compared = caesar.DecryptCaesar(currentFre[1].ToString(), difference.ToString());

            char[] comparedLetter = compared.ToCharArray();
            if (baseFre[1] == comparedLetter[0])
            {
                return(true);
            }
            return(false);
        }
コード例 #2
0
ファイル: Decode.cs プロジェクト: Ecclesiast/Crypto
        public string DecodeCaesar(string input)
        {
            LoadBaseTable();
            CryptoCyphers decode = new CryptoCyphers();
            TextBox       tetet  = new TextBox();


            Dictionary <string, int> currentFrequencyTable = AnalyseText(input);
            List <string>            currentFrequencyList  = currentFrequencyTable.Keys.OrderByDescending(k => currentFrequencyTable[k]).ToList();

            int baseMax    = 20;
            int currentMax = 20;

            string output = "";

            Cyphers caesar = new Cyphers();

            for (int i = 0; i < Math.Min(baseMax, currentFrequencyList.Count); i++)
            {
                for (int j = 0; j < Math.Min(currentMax, baseFrequencyList.Count); j++)
                {
                    if (IsComparable(baseFrequencyList[j], currentFrequencyList[i]))
                    {
                        int[] baseT    = caesar.GetInputCode(baseFrequencyList[j]);
                        int[] currentT = caesar.GetInputCode(currentFrequencyList[i]);
                        int   key      = 0;
                        if (baseT[0] <= currentT[0])
                        {
                            key = currentT[0] - baseT[0];
                        }
                        else
                        {
                            key = caesar.GeneralLength - baseT[0] + currentT[0];
                        }

                        // output = "There are all variants:";
                        output += string.Format("\n\r\n{0}\n\r\n", caesar.DecryptCaesar(input, key.ToString()));
                    }
                }
            }

            return(output);
        }