mirror of
https://github.com/Smaug123/ClassicalCiphers.jl
synced 2025-10-07 02:18:39 +00:00
Enforce lower/uppercase consistency; expand readme
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
"""
|
||||
Encrypts the given plaintext according to the Caesar cipher.
|
||||
The key is given as an integer, being the offset of each character;
|
||||
so encrypt_caesar("abc", 1) == "bcd".
|
||||
so encrypt_caesar("abc", 1) == "BCD".
|
||||
|
||||
Converts the input to lowercase.
|
||||
Converts the input to uppercase.
|
||||
"""
|
||||
function encrypt_caesar(plaintext, key::Integer)
|
||||
# plaintext: string; key: integer offset, so k=1 sends "a" to "b"
|
||||
key = ((key-1) % 26) + 1
|
||||
encrypt_monoalphabetic(plaintext, join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))]))
|
||||
keystr = join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))])
|
||||
encrypt_monoalphabetic(plaintext, keystr)
|
||||
end
|
||||
|
||||
"""
|
||||
@@ -19,7 +20,7 @@ so decrypt_caesar("abcd", 1) == "zabc".
|
||||
Converts the input to lowercase.
|
||||
"""
|
||||
function decrypt_caesar(ciphertext, key::Integer)
|
||||
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "A"
|
||||
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "a"
|
||||
key = ((key-1) % 26) + 1
|
||||
encrypt_caesar(ciphertext, 26-key)
|
||||
lowercase(encrypt_caesar(ciphertext, 26-key))
|
||||
end
|
||||
|
Reference in New Issue
Block a user