static void Main(string[] args) { string cipherText0 = "UIF RVJDL CSPXO GPY KVNQT PWFS 13 MBAZ EPHT."; // 1 Console.WriteLine("Test Cipher Text: " + CaesarCipher.decrypt(cipherText0, 1)); //string cipherText1 = "KVXUAOGRRDRBGFTPDVWRMCDWTELUAWXILKNZGVXTYHPEMQVHVETIABPSMVHXYIGFMBNLLPOPDAENTAGNLRETMSTIABPHXVAEMSICSLKOGCTXNYTPDXNOJWEGVLRCNWER"; //string cipherText1 = "ASTZHSSJNSBZFPSSJESNTBPZHNLREYUWCFWYUAXSJEGSTHDDCIPCBEVADINCCQYREYUBLFIDVWJOJOKDPOPQFKXHDPTWCASTZHMSOZZZLBZEVASSJDONPHKWRPSOPEVA"; string cipherText1 = "FWURLERNWEAIFLXFNTIEUVCHDIGXMHIEIIOGFUGTEWRNWZTLVEFJTARTMYEREOPFPIFSXVAVYOMYEKFNXMEKPPKFPHJAEJNHNBTCLLVLHHUXECXRLEFWLYIFKOVFNMIO"; Console.WriteLine("CipherText: " + cipherText1); Console.WriteLine("\nPlain Text: " + VigenereCipher.decrypt(cipherText1, 3)); Console.ReadLine(); }
//Enum.GetName(typeof(pos), i) : get enum name based on value public static string decrypt(string cipherText, int keyLength) { string plainText = string.Empty; int lengthText = cipherText.Length; string[] cipherGroup = new string[keyLength]; string cipher1 = string.Empty, cipher2 = string.Empty, cipher3 = string.Empty; //Sepearate the cipherText into 3 groups based on their modulo for (int i = 0; i < lengthText; i++) { if (i % keyLength == 0) { cipher1 += cipherText[i]; } if (i % keyLength == 1) { cipher2 += cipherText[i]; } if (i % keyLength == 2) { cipher3 += cipherText[i]; } } Console.WriteLine("Plain 1: " + cipher1); Console.WriteLine("Plain 2: " + cipher2); Console.WriteLine("Plain 3: " + cipher3); //Counted Text and print Dictionary <int, int> countRepeated1 = countRepeatedKeys(cipher1); Dictionary <int, int> countRepeated2 = countRepeatedKeys(cipher2); Dictionary <int, int> countRepeated3 = countRepeatedKeys(cipher3); Console.WriteLine("-------Counted Repeating Key-----------\n"); Console.WriteLine("Normal English repeated key"); Console.WriteLine("A B C D E F G H I G K L M N O P Q R S T U V W X Y Z"); Console.WriteLine("8 2 3 4 13 2 2 6 7 0 1 4 2 7 8 2 0 6 6 9 3 1 2 0 2 0"); for (int i = 0; i < 26; i++) { Console.Write(Enum.GetName(typeof(pos), i) + " "); } Console.WriteLine("\ncipher1"); for (int i = 0; i < 26; i++) { Console.Write(countRepeated1[i] + " "); } Console.WriteLine("\ncipher2"); for (int i = 0; i < 26; i++) { Console.Write(countRepeated2[i] + " "); } Console.WriteLine("\ncipher3"); for (int i = 0; i < 26; i++) { Console.Write(countRepeated3[i] + " "); } //Loop through all possible decrypted texts for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { for (int k = 0; k < 25; k++) { string plainText1 = CaesarCipher.decrypt(cipher1, i); //P is the highest probability in text1, it should be E in normal English, shift back 11/2 string plainText2 = CaesarCipher.decrypt(cipher2, j); //P is the highest probability in text1, it should be E in normal English, shift back 22 string plainText3 = CaesarCipher.decrypt(cipher3, k); //P is the highest probability in text1, it should be E in normal English, shift back 7 / 15 plainText = string.Empty; for (int l = 0; l < plainText1.Length; l++) { plainText += plainText1[l]; plainText += plainText2[l]; if (l < plainText3.Length) //Clumpsy code { plainText += plainText3[l]; } } //Console.WriteLine("----" + i + "," + j + "," + k + ":" + plainText); if (plainText.Contains("THE") || plainText.Contains("AND")) { Console.WriteLine("KEY is : " + Enum.GetName(typeof(pos), i) + Enum.GetName(typeof(pos), j) + Enum.GetName(typeof(pos), k)); Console.WriteLine("----" + Enum.GetName(typeof(pos), i) + Enum.GetName(typeof(pos), j) + Enum.GetName(typeof(pos), k) + " : " + i + "," + j + "," + k + ":" + plainText); Console.ReadKey(); } /*if (i == 2 && j == 0 && k == 19) * { * Console.WriteLine("KEY is : " + Enum.GetName(typeof(pos), i) + Enum.GetName(typeof(pos), j) + Enum.GetName(typeof(pos), k)); * string shortPlainText = string.Empty; * for (int m = 0; m < 40; m++) * { * shortPlainText += plainText[m]; * } * Console.WriteLine("First 40 words of plainText: " + shortPlainText); * return plainText; * }*/ } } } return(plainText); }