2015-11-28 93 views
-1

這是我迄今爲止我會如何製作凱撒密碼解密器?

namespace CaesarDecrypter 
{ 
class Reader 
{ 
    static void Main(string[] args) 
    { 
     //Display Welcome Message 
     Console.WriteLine("Welcome To The Caesar Cypher Decryption Program"); 
     Console.WriteLine("***********************************************"); 
     Console.WriteLine("Beggining Decryption:"); 
     Console.WriteLine("\n\n Content:\n\n"); 
     //Loop the program till given a command. 
     bool Loop = true; 

     while (Loop == true) 
     { 
      //Read in Text From File 
      String text = File.ReadAllText(@"C:\Users\Grimswolf\Desktop\Text Folder\caesarShiftEncodedText.txt"); 

      //Display The File Text 
      foreach (char c in text) 
      { 

       //Not Sure What to do here? 
       Console.WriteLine(text); 
       Console.WriteLine("\n\n"); 
      } 
      Console.WriteLine("Do you with to continue? Enter no to exit application."); 
      //Enter "no" to exit the loop 
      String Answer = Console.ReadLine(); 
      if (Answer == "no") 
      { 
       // set loop bool to false so it exits the program. 
       Loop = false; 
      } 

     } 






    } 
} 


} 

我需要它不斷通過字母前進,找到一個方法來轉移X的地方破解暗號。例如輸出看起來解密移

0 exxego前srgi

1 dwwdfn DW rqfh

2 cvvcem CV qpeg

3個buubdl BU podf

4攻擊一次

+0

你有沒有decyphered文本?你怎麼知道X是否正確?無論是一個人都需要看它,或者你需要一個詞頻數據庫。 – vidstige

回答

0

對於克拉k這個密碼你需要統計每封信出現的可能性,例如'a'18%'b'4%等(你可以在網上找到這樣的列表)和一個大文本。你計算這些字母,在你的文字上找到他們的編號,因此可以匹配它們(例如'u'17.3%最有可能是'a'根據我們的統計。結果,並決定自己的(不是真的你會叫decyphering) 你知道至少有一兩句話的情況下,使其返回只decyphered詞匹配你的猜測,按字母順序

1

我會使用此代碼:

public string Decrypt(string cipherString, int shift) 
{ 
    var alphabet = 
     Enumerable 
     .Concat(
      Enumerable.Range((int)'a', 26), 
      Enumerable.Range((int)'A', 26)) 
     .Select(i => (char)i) 
     .ToArray(); 

    var map = 
     alphabet 
      .Zip(
       alphabet.Concat(alphabet).Concat(alphabet).Skip(alphabet.Length + shift), 
       (c0, c1) => new { c0, c1 }) 
      .ToDictionary(x => x.c0, x => x.c1); 

    return String.Join("", cipherString.Select(x => map.ContainsKey(x) ? map[x] : x)); 
} 

那麼你可以運行這段代碼:

for (var i = 0; i < 5; i++) 
{ 
    Console.WriteLine("{0} {1}", i, Decrypt("exxego ex srgi", -i)); 
} 

你得到這樣的輸出:

0 exxego ex srgi 
1 dwwdfn dw rqfh 
2 cvvcem cv qpeg 
3 buubdl bu podf 
4 attack at once