Advertisement
Guest User

Untitled

a guest
May 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. private static int DetermineKeyLength(string ciphertext)
  2.         {
  3.             double difference = double.MaxValue;
  4.             int keyLength = 0;
  5.  
  6.             for (int i = 1; i < MAXKEYSIZE; i++)
  7.             {
  8.                 if (i >= ciphertext.Length) break;
  9.                 double current = IndexOFCoincidence(ciphertext, i) - EXPECTEDENGLISHIOC;
  10.                 if (current < difference)
  11.                 {
  12.                     difference = current;
  13.                     keyLength = i;
  14.                 }
  15.             }
  16.             return keyLength;
  17.         }
  18.  
  19.         public static double IndexOFCoincidence(string ciphertext, int shift)
  20.         {
  21.             double ioc = 0;
  22.             for (int i = 0; i < ciphertext.Length; i++)
  23.             {
  24.                 if (ciphertext[i] == ciphertext[(i + shift) % ciphertext.Length])
  25.                 {
  26.                     ioc++;
  27.                 }  
  28.             }  
  29.  
  30.             return ioc/ciphertext.Length;
  31.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement