From 29ebc22cd2c952eed7e94fb360ccc1052625c352 Mon Sep 17 00:00:00 2001 From: "Jake W. Ireland" Date: Mon, 11 Jan 2021 17:30:40 +1300 Subject: [PATCH] Updated Caesar method to fall back to traditionally used shift, and changed one test case for a more accurate test statement... --- src/caesar.jl | 8 ++++++++ test/caesar.jl | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/caesar.jl b/src/caesar.jl index fa98961..59cdb91 100644 --- a/src/caesar.jl +++ b/src/caesar.jl @@ -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. diff --git a/test/caesar.jl b/test/caesar.jl index e3d3b1d..b34d91c 100644 --- a/test/caesar.jl +++ b/test/caesar.jl @@ -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)) \ No newline at end of file + crack_caesar(singh) == (singh_ans, 7))