public string Analyse3By3Key(string plainText, string cipherText) { //throw new NotImplementedException(); MatrixOP r = new MatrixOP(); 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 / 3 + (plainText.Length % 3 == 0 ? 0 : 1)]; for (int i = 0; i < split.Length; i++) { split[i] = plainText.Substring(i * 3, i * 3 + 3 > plainText.Length ? 1 : 3); } string[] splitc = new string[cipherText.Length / 3 + (cipherText.Length % 3 == 0 ? 0 : 1)]; for (int i = 0; i < splitc.Length; i++) { splitc[i] = cipherText.ToLower().Substring(i * 3, i * 3 + 3 > cipherText.Length ? 1 : 3); } 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 + 2; j < split.Length; j++) { perm += split[i] + split[j - 1] + split[j]; ch += splitc[i] + splitc[j - 1] + splitc[j]; PermKey.Add(perm); ciph.Add(ch); perm = ""; ch = ""; } } List <int> In = new List <int>(); for (int i = 0; i < PermKey.Count; i++) { In.Add(r.Inversekey(PermKey[i])); } int cnterr = 0; for (int i = 0; i < In.Count; i++) { if (In[i] < 0) { cnterr++; } } if (cnterr == In.Count) { throw new InvalidAnlysisException(); } int invMul = 0; string pl = ""; string ct = ""; for (int i = 0; i < In.Count; i++) { if (In[i] > 0) { invMul = In[i]; pl = PermKey[i]; ct = ciph[i]; break; } } double[,] keymat = new double[3, 3]; int[,] Ciphermat = new int[3, 3]; int rp = 0; for (int i = 0; i < 3; i++) { int cc = rp; for (int j = 0; j < 3; j++) { keymat[i, j] = DT[pl[cc]]; rp++; cc++; } cc = rp; } rp = 0; for (int i = 0; i < 3; i++) { int cc = rp; for (int j = 0; j < 3; j++) { Ciphermat[i, j] = DT[ct[cc]]; rp++; cc++; } cc = rp; } /////////////////////////cofactors/////////////////////////////// int[,] matl = new int[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { matl[i, j] = int.Parse(Math.Floor((Math.Pow(-1, i + j) * (int.Parse(Matrix.Determinant(r.CreateSmallerMatrix(keymat, i, j)).ToString())))).ToString()); while (matl[i, j] < 0) { matl[i, j] += 26; } while (matl[i, j] > 26) { matl[i, j] -= 26; } } } ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// int[,] matKeyinverse = new int[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { matKeyinverse[i, j] = matl[j, i]; matKeyinverse[i, j] = (invMul * matKeyinverse[i, j]) % 26; } } //////////////////////////////////inverse/////////////////////////////////// int[,] matRes = new int[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int sum = 0; for (int k = 0; k < 3; k++) { sum += matKeyinverse[i, k] * Ciphermat[k, j]; } matRes[i, j] = sum % 26; } } string key = ""; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { key += CT[matRes[j, i]]; } } return(key); }