Example #1
0
        public string Analyse(string plainText, string cipherText)
        {
            MatrixOP r = new MatrixOP();
            //throw new NotImplementedException();
            Dictionary <char, int> DT = new Dictionary <char, int>();
            Dictionary <int, char> CT = new Dictionary <int, char>();
            int cnt = 0;

            for (char c = 'a'; c <= 'z'; c++)
            {
                DT[c]   = cnt;
                CT[cnt] = c;
                cnt++;
            }
            string[] split = new string[plainText.Length / 2 + (plainText.Length % 2 == 0 ? 0 : 1)];
            for (int i = 0; i < split.Length; i++)
            {
                split[i] = plainText.Substring(i * 2, i * 2 + 2 > plainText.Length ? 1 : 2);
            }
            string[] splitc = new string[cipherText.Length / 2 + (cipherText.Length % 2 == 0 ? 0 : 1)];
            for (int i = 0; i < splitc.Length; i++)
            {
                splitc[i] = cipherText.ToLower().Substring(i * 2, i * 2 + 2 > cipherText.Length ? 1 : 2);
            }
            List <string> PermKey = new List <string>();
            List <string> ciph    = new List <string>();

            for (int i = 0; i < split.Length; i++)
            {
                string perm = "";
                string ch   = "";
                for (int j = i + 1; j < split.Length; j++)
                {
                    perm += split[i] + split[j];
                    ch   += splitc[i] + splitc[j];
                    PermKey.Add(perm);
                    ciph.Add(ch);
                    perm = "";
                    ch   = "";
                }
            }
            List <int> CalInverse = new List <int>();

            for (int i = 0; i < PermKey.Count; i++)
            {
                CalInverse.Add(r.calculateInverse(PermKey[i]));
            }
            int cnterr = 0;

            for (int i = 0; i < CalInverse.Count; i++)
            {
                if (CalInverse[i] < 0)
                {
                    cnterr++;
                }
            }
            if (cnterr == CalInverse.Count)
            {
                throw new InvalidAnlysisException();
            }
            int    invMul = 0;
            string pl     = "";
            string ct     = "";

            for (int i = 0; i < CalInverse.Count; i++)
            {
                if (CalInverse[i] > 0)
                {
                    invMul = CalInverse[i];
                    pl     = PermKey[i];
                    ct     = ciph[i];
                    break;
                }
            }
            ////////////////////plaintext/////////////////////
            int[,] keymat = new int[2, 2];
            int idx = 0;

            for (int i = 0; i < 2; i++)
            {
                int cc = idx;
                for (int j = 0; j < 2; j++)
                {
                    keymat[i, j] = DT[pl[idx]];
                    idx++;
                    cc++;
                }
                cc = idx;
            }
            ////////////////////ciphertext//////////////////
            int[,] cimat = new int[2, 2];
            idx          = 0;
            for (int i = 0; i < 2; i++)
            {
                int cc = idx;
                for (int j = 0; j < 2; j++)
                {
                    cimat[j, i] = DT[ct[idx]];
                    idx++;
                    cc++;
                }
                cc = idx;
            }
            ///////////////////cofactors//////////////////
            int A = keymat[0, 0];
            int B = keymat[0, 1];
            int C = keymat[1, 0];
            int D = keymat[1, 1];

            keymat[0, 0] = invMul * D;
            keymat[1, 1] = invMul * A;
            keymat[1, 0] = invMul * (B * -1);
            keymat[0, 1] = invMul * (C * -1);
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    while (keymat[i, j] < 0)
                    {
                        keymat[i, j] += 26;
                    }
                    while (keymat[i, j] > 26)
                    {
                        keymat[i, j] -= 26;
                    }
                }
            }
            int[,] Mulapp = new int[2, 2];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    int sum = 0;
                    for (int k = 0; k < 2; k++)
                    {
                        sum += cimat[i, k] * keymat[k, j];
                    }
                    Mulapp[i, j] = sum % 26;
                }
            }
            string key = "";

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    key += CT[Mulapp[i, j]];
                }
            }
            return(key);
        }