示例#1
0
        static void OpenMode(Mode mode)
        {
            using (StreamReader inReader = new StreamReader("in.txt"))
            {
                string openText = inReader.ReadLine();

                using (StreamReader keyReader = new StreamReader(mode.ToString() + "\\" + "key.txt"))
                {
                    IEncoder encoder;

                    try
                    {
                        switch (mode)
                        {
                        case Mode.Affine:
                            int a, b;
                            ReadAffineKeys(keyReader, out a, out b);
                            encoder = new AffineCipher(a, b);

                            break;

                        case Mode.Simple:
                            Dictionary <char, char> alphabet = ReadTable(keyReader);
                            encoder = new SimpleSubstitutionCipher(alphabet);

                            break;

                        default:
                            string keyWord = ReadKeyWord(keyReader);
                            encoder = new VigenereCipher(keyWord);

                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }

                    string cipher = encoder.EncryptText(openText);

                    using (StreamWriter cryptWriter = new StreamWriter(mode.ToString() + "\\" + "crypt.txt"))
                    {
                        cryptWriter.WriteLine(cipher);
                    }

                    string decryptText = encoder.DecryptText(cipher);

                    using (StreamWriter decryptWriter = new StreamWriter(mode.ToString() + "\\" + "decrypt.txt"))
                    {
                        decryptWriter.WriteLine(decryptText);
                    }
                }
            }
        }
        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);
        }