コード例 #1
0
        public string DecryptText(string cipherText)
        {
            int revA = SimpleMaths.ReverseElement(a, M);

            StringBuilder decryptedText = new StringBuilder();

            foreach (char c in cipherText)
            {
                if (c == ' ')
                {
                    decryptedText.Append(' ');
                    continue;
                }

                int
                    code = c - 'A',
                    x    = (revA * (code - b)) % M;

                if (x < 0)
                {
                    x += M;
                }

                char next = (x == 26) ? ' ' : (char)(x + 'A');
                decryptedText.Append(next);
            }

            return(decryptedText.ToString());
        }
コード例 #2
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);
        }
コード例 #3
0
        public AffineCipher(int a, int b)
        {
            if (SimpleMaths.GCD(a, M) != 1)
            {
                throw new ArgumentException("Error:"
                                            + " \"a\" parameter and modulus M should be coprime.");
            }

            this.a = a;
            this.b = b;
        }