Add crack_caesar functionality

This commit is contained in:
Smaug123
2016-01-04 12:46:55 +00:00
parent 46ba92fcc7
commit d0bda1d6e7
6 changed files with 30 additions and 7 deletions

View File

@@ -24,3 +24,16 @@ function decrypt_caesar(ciphertext, key::Integer)
key = ((key-1) % 26) + 1
lowercase(encrypt_caesar(ciphertext, 26-key))
end
"""
Cracks the given ciphertext according to the Caesar cipher.
Returns (plaintext, key::Integer), such that encrypt_caesar(plaintext, key)
would return ciphertext.
Converts the input to lowercase.
"""
function crack_caesar(ciphertext)
texts = [(decrypt_caesar(ciphertext,key), key) for key in 1:26]
texts = sort(texts, by=(x -> string_fitness(first(x))))
texts[end]
end