mirror of
https://github.com/Smaug123/ClassicalCiphers.jl
synced 2025-10-05 09:28:44 +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.
|
||||
|
@@ -1,9 +1,11 @@
|
||||
using ClassicalCiphers
|
||||
using Test
|
||||
|
||||
@test encrypt_caesar("THIS CODE WAS INVENTED BY JULIUS CAESAR", 3) == "WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU"
|
||||
@test encrypt_caesar("This code was not strictly invented by Julius Caesar, but it was reported by Roman historian Suetonius that Caesar used it, and hence the cipher was named.", 3) == "WKLV FRGH ZDV QRW VWULFWOB LQYHQWHG EB MXOLXV FDHVDU, EXW LW ZDV UHSRUWHG EB URPDQ KLVWRULDQ VXHWRQLXV WKDW FDHVDU XVHG LW, DQG KHQFH WKH FLSKHU ZDV QDPHG."
|
||||
@test encrypt_caesar("The Caesar cipher was traditionally used by shifting each letter exactly three positions down.") == "WKH FDHVDU FLSKHU ZDV WUDGLWLRQDOOB XVHG EB VKLIWLQJ HDFK OHWWHU HADFWOB WKUHH SRVLWLRQV GRZQ."
|
||||
|
||||
@test decrypt_caesar("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU", 3) == "this code was invented by julius caesar"
|
||||
@test decrypt_caesar("wkh fdhvdu flskhu zdv wudglwlrqdoob xvhg eb vkliwlqj hdfk ohwwhu hadfwob wkuhh srvlwlrqv grzq.") == "the caesar cipher was traditionally used by shifting each letter exactly three positions down."
|
||||
|
||||
@test crack_caesar("WKLV FRGH ZDV LQYHQWHG EB MXOLXV FDHVDU") == ("this code was invented by julius caesar", 3)
|
||||
|
||||
@@ -14,4 +16,4 @@ using Test
|
||||
@test (
|
||||
singh = "MHILY LZA ZBHL XBPZXBL MVYABUHL HWWPBZ JSHBKPBZ JHLJBZ KPJABT HYJHUBT LZA ULBAYVU";
|
||||
singh_ans = lowercase("FABER EST SUAE QUISQUE FORTUNAE APPIUS CLAUDIUS CAECUS DICTUM ARCANUM EST NEUTRON");
|
||||
crack_caesar(singh) == (singh_ans, 7))
|
||||
crack_caesar(singh) == (singh_ans, 7))
|
||||
|
Reference in New Issue
Block a user