Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static int DetermineKeyLength(string ciphertext)
- {
- double difference = double.MaxValue;
- int keyLength = 0;
- for (int i = 1; i < MAXKEYSIZE; i++)
- {
- if (i >= ciphertext.Length) break;
- double current = IndexOFCoincidence(ciphertext, i) - EXPECTEDENGLISHIOC;
- if (current < difference)
- {
- difference = current;
- keyLength = i;
- }
- }
- return keyLength;
- }
- public static double IndexOFCoincidence(string ciphertext, int shift)
- {
- double ioc = 0;
- for (int i = 0; i < ciphertext.Length; i++)
- {
- if (ciphertext[i] == ciphertext[(i + shift) % ciphertext.Length])
- {
- ioc++;
- }
- }
- return ioc/ciphertext.Length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement