Example #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);
        }
Example #2
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);
                    }
                }
            }
        }