mirror of
https://github.com/Smaug123/ClassicalCiphers.jl
synced 2025-10-10 03:48:43 +00:00
Add crack_caesar functionality
This commit is contained in:
@@ -43,6 +43,12 @@ decrypt_caesar("Khoor, Zruog!", 3)
|
|||||||
|
|
||||||
Likewise, `decrypt_caesar` turns everything lower-case, but retains symbols.
|
Likewise, `decrypt_caesar` turns everything lower-case, but retains symbols.
|
||||||
|
|
||||||
|
Automatically crack the same text:
|
||||||
|
```julia
|
||||||
|
crack_caesar("Khoor, Zruog!")
|
||||||
|
# outputs ("hello, world!", 3)
|
||||||
|
```
|
||||||
|
|
||||||
### Monoalphabetic cipher
|
### Monoalphabetic cipher
|
||||||
|
|
||||||
Encrypt the text "Hello, World!" with the same Caesar cipher, but
|
Encrypt the text "Hello, World!" with the same Caesar cipher, but
|
||||||
|
@@ -9,7 +9,7 @@ include("vigenere.jl")
|
|||||||
include("solitaire.jl")
|
include("solitaire.jl")
|
||||||
|
|
||||||
export encrypt_monoalphabetic, decrypt_monoalphabetic,
|
export encrypt_monoalphabetic, decrypt_monoalphabetic,
|
||||||
encrypt_caesar, decrypt_caesar,
|
encrypt_caesar, decrypt_caesar, crack_caesar,
|
||||||
encrypt_vigenere, decrypt_vigenere,
|
encrypt_vigenere, decrypt_vigenere,
|
||||||
encrypt_solitaire, decrypt_solitaire,
|
encrypt_solitaire, decrypt_solitaire,
|
||||||
string_fitness
|
string_fitness
|
||||||
|
@@ -24,3 +24,16 @@ function decrypt_caesar(ciphertext, key::Integer)
|
|||||||
key = ((key-1) % 26) + 1
|
key = ((key-1) % 26) + 1
|
||||||
lowercase(encrypt_caesar(ciphertext, 26-key))
|
lowercase(encrypt_caesar(ciphertext, 26-key))
|
||||||
end
|
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
|
||||||
|
8
test/caesar.jl
Normal file
8
test/caesar.jl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using ClassicalCiphers
|
||||||
|
using Base.Test
|
||||||
|
|
||||||
|
@test encrypt_caesar("THIS CODE WAS INVENTED BY JULIUS CAESAR", 3) == "WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU"
|
||||||
|
|
||||||
|
@test decrypt_caesar("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU", 3) == "this code was invented by julius caesar"
|
||||||
|
|
||||||
|
@test crack_caesar("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU") == ("this code was invented by julius caesar", 3)
|
@@ -9,7 +9,3 @@ using Base.Test
|
|||||||
@test decrypt_monoalphabetic("5@coD", Dict{Char, Char}('a' => '5', 'B' => '@', 'b' => 'o', 'D' => 'D')) == "aBcbD"
|
@test decrypt_monoalphabetic("5@coD", Dict{Char, Char}('a' => '5', 'B' => '@', 'b' => 'o', 'D' => 'D')) == "aBcbD"
|
||||||
|
|
||||||
@test decrypt_monoalphabetic("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU", "DEFGHIJKLMNOPQRSTUVWXYZABC") == lowercase("THIS CODE WAS INVENTED BY JULIUS CAESAR")
|
@test decrypt_monoalphabetic("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU", "DEFGHIJKLMNOPQRSTUVWXYZABC") == lowercase("THIS CODE WAS INVENTED BY JULIUS CAESAR")
|
||||||
|
|
||||||
@test encrypt_caesar("THIS CODE WAS INVENTED BY JULIUS CAESAR", 3) == "WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU"
|
|
||||||
|
|
||||||
@test decrypt_caesar("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU", 3) == lowercase("THIS CODE WAS INVENTED BY JULIUS CAESAR")
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
using ClassicalCiphers
|
using ClassicalCiphers
|
||||||
|
|
||||||
tests = ["vigenere", "monoalphabetic", "solitaire"]
|
tests = ["vigenere", "monoalphabetic", "solitaire", "caesar"]
|
||||||
|
|
||||||
println("Running tests:")
|
println("Running tests:")
|
||||||
|
|
||||||
@@ -8,4 +8,4 @@ for t in tests
|
|||||||
test_fn = "$t.jl"
|
test_fn = "$t.jl"
|
||||||
println(" * $test_fn")
|
println(" * $test_fn")
|
||||||
include(test_fn)
|
include(test_fn)
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user