Skip to content

Instantly share code, notes, and snippets.

@dpick
Created March 16, 2011 19:20
Show Gist options
  • Save dpick/873128 to your computer and use it in GitHub Desktop.
Save dpick/873128 to your computer and use it in GitHub Desktop.
Algorithm to decrypt ciphertext created using a vigenere cipher
key_length = key_length_guess(ciphertext)
dot_values, shift_values = [ ], [ ]
1.upto(key_length).each do |start_letter|
letter_frequency_array = ciphertext.every_nth_letter(key_length, start_letter).letter_frequencies
dot_values << letter_frequency_array.dot_product(ENGLISH_FREQUENCIES)
1.upto(ENGLISH_FREQUENCIES.size - 1).each do |i|
dot_values << letter_frequency_array.dot_product(ENGLISH_FREQUENCIES.rotate!)
end
ENGLISH_FREQUENCIES.rotate!
shift_values << dot_values.find_index(dot_values.max)
dot_values = [ ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment