mirror of
https://github.com/Smaug123/ClassicalCiphers.jl
synced 2025-10-08 19:08:41 +00:00
Updated Caesar method to fall back to traditionally used shift, and changed one test case for a more accurate test statement...
This commit is contained in:
@@ -4,6 +4,9 @@ The key is given as an integer, being the offset of each character;
|
||||
so encrypt_caesar("abc", 1) == "BCD".
|
||||
|
||||
Converts the input to uppercase.
|
||||
|
||||
Traditionally, the Caesar cipher was used with a shift of 3, so this is the method it will
|
||||
fall back to if only given plaintext.
|
||||
"""
|
||||
function encrypt_caesar(plaintext, key::T) where {T <: Integer}
|
||||
# plaintext: string; key: integer offset, so k=1 sends "a" to "b"
|
||||
@@ -11,6 +14,7 @@ function encrypt_caesar(plaintext, key::T) where {T <: Integer}
|
||||
keystr = join(vcat(collect(Char(97 + key):'z'), collect('a':Char(97 + key - 1))))
|
||||
return encrypt_monoalphabetic(plaintext, keystr)
|
||||
end
|
||||
encrypt_caesar(plaintext) = encrypt_caesar(plaintext, 3)
|
||||
|
||||
"""
|
||||
Decrypts the given ciphertext according to the Caesar cipher.
|
||||
@@ -18,12 +22,16 @@ The key is given as an integer, being the offset of each character;
|
||||
so decrypt_caesar("abcd", 1) == "zabc".
|
||||
|
||||
Converts the input to lowercase.
|
||||
|
||||
Traditionally, the Caesar cipher was used with a shift of 3, so this is the method it will
|
||||
fall back to if only given plaintext.
|
||||
"""
|
||||
function decrypt_caesar(ciphertext, key::T) where {T <: Integer}
|
||||
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "a"
|
||||
key = ((key - 1) % 26) + 1
|
||||
return lowercase(encrypt_caesar(ciphertext, 26 - key))
|
||||
end
|
||||
decrypt_caesar(plaintext) = decrypt_caesar(plaintext, 3)
|
||||
|
||||
"""
|
||||
Cracks the given ciphertext according to the Caesar cipher.
|
||||
|
Reference in New Issue
Block a user