Challenge 40

🤴

"Encryption" - Caesar Cipher

Open in Code Editor or Copy Instructions

During the Roman empire, Julius Caesar needed a secure way to transmit messages to and from his military generals

In this encryption algorithm, a shift amount is required - for example, if the shift amount is 3, then each character would be shifted 3 places. See below for examples:

It's worth noting, that today, cracking a simple Caesar cipher is trivial using a brute force approach

You should implement functions to both encrypt and decrypt

Spaces should remain unchanged

Examples:

encrypt("ab c de", 1) would return "bc d ef"

decrypt("bc d ef", 1) would return "ab c de"

encrypt("abc", 3) would return "def"

decrypt("def", 3) would return "abc"

encrypt("xyz", 3) would return "{|}"

decrypt("{|}", 3) would return "xyz"

Note: in this simple example, wrap-around isn't required

Extension:

Implement wrap-around - i.e. after reaching z, we go back to a. This should work for both uppercase and lowercase letters. For example encrypt("xyz XYZ") should return "abc ABC"

Take an encrypted message, then brute force all possible shift amounts, outputting the result, for you to visually inspect and determine what must have been the correct shift amount (consider how you could also detect the correct shift amount in code, rather than viewing all decryptions by eye)