コード例 #1
0
        public string Analyze()
        {
            for (int a = 1; a < M; a++)
            {
                if (SimpleMaths.GCD(a, M) != 1)
                {
                    continue;
                }

                for (int b = 0; b < M; b++)
                {
                    AffineCipher affine = new AffineCipher(a, b);
                    string       text   = affine.DecryptText(cipherText);

                    double score = NgramStatistics.CountTextScore(text);

                    if (score > rate)
                    {
                        rate = score;
                        this.decryptedText = text;
                    }
                }
            }

            return(decryptedText);
        }
コード例 #2
0
        public string Analyze()
        {
            int size = (int)Math.Sqrt(text.Count);

            for (int i = 0, period = 2; i < 10; i++, period++)
            {
                double ic = CountCoincidence(period);
                lengths.Add(ic, period);
            }

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

            string decryptedText = "";
            double score         = double.MinValue;

            foreach (var length in lengths.Take(5))
            {
                int T = length.Value;

                StringBuilder[] subs = new StringBuilder[T];

                for (int i = 0; i < T; i++)
                {
                    subs[i] = new StringBuilder();
                }

                for (int i = 0; i < text.Count; i++)
                {
                    subs[i % T].Append(text[i]);
                }

                string[] decrypted = new string[T];

                for (int i = 0; i < T; i++)
                {
                    CaesarAnalyst analyst = new CaesarAnalyst(subs[i]);
                    decrypted[i] = analyst.Analyze();
                }

                StringBuilder str = new StringBuilder();

                for (int i = 0; i < text.Count; i++)
                {
                    str.Append(decrypted[i % T][i / T]);
                }

                double currentScore =
                    NgramStatistics.CountTextScore(str.ToString());

                if (currentScore > score)
                {
                    score         = currentScore;
                    decryptedText = str.ToString();
                }
            }

            return(decryptedText);
        }
コード例 #3
0
        public string Analyze()
        {
            List <char> bestKey             = FrequencySortedAlphabet(cipherText);
            SimpleSubstitutionCipher cipher = new SimpleSubstitutionCipher(sortedAlphabet, bestKey);

            string decryptedText = cipher.DecryptText(cipherText);
            double score         = NgramStatistics.CountTextScore(decryptedText);

            for (int k = 0; k < 7; k++)
            {
                Random random = new Random();
                parentKey = alphabet.Shuffle(random).ToList();

                cipher = new SimpleSubstitutionCipher(alphabet, parentKey);

                string text = cipher.DecryptText(cipherText);
                rate = NgramStatistics.CountTextScore(text);

                for (int t = 0; t < times; t++)
                {
                    List <char> key = alphabet.Shuffle(random).ToList();

                    cipher = new SimpleSubstitutionCipher(alphabet, key);

                    string nextText    = cipher.DecryptText(cipherText);
                    double currentRate = NgramStatistics.CountTextScore(nextText);

                    if (currentRate > rate)
                    {
                        parentKey = key;
                        text      = nextText;
                        rate      = currentRate;

                        for (int fail = 0; fail < 10000;)
                        {
                            int i = random.Next(alphabet.Count),
                                j = random.Next(alphabet.Count);

                            List <char> newKey = parentKey.Swap(i, j).ToList();

                            cipher = new SimpleSubstitutionCipher(alphabet, newKey);

                            string newText = cipher.DecryptText(cipherText);
                            double newRate = NgramStatistics.CountTextScore(newText);

                            if (newRate > rate)
                            {
                                parentKey = newKey;
                                text      = newText;
                                rate      = newRate;

                                fail = 0;
                            }
                            else
                            {
                                fail++;
                            }
                        }
                    }
                }

                if (rate > score)
                {
                    score         = rate;
                    decryptedText = text;
                    bestKey       = parentKey;
                }
            }

            return(decryptedText);
        }