We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Caesar Cipher

Example of Caesar cipher with right shift 3:

Alphabet:  ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shifted:   DEFGHIJKLMNOPQRSTUVWXYZABC

Each letter in the plaintext alphabet maps directly to its corresponding match in the cipher alphabet.

For example:

"A" -> "D"
"B" -> "E"

Assignment

To demonstrate the superiority of our modern ciphers at Passly, we've been asked to implement a Caesar cipher. It's so the marketing team can show off how much better our algorithms are than the ones used by the Romans. Go figure.

Complete the encrypt, decrypt, crypt, and getOffsetChar functions. The encrypt and decrypt functions do what you would expect, crypt and getOffsetChar are helper functions.

encrypt

Should call and return the result of crypt().

decrypt

Should call and return the result of crypt(), but should pass in the negative value of key so that the offset is reversed.

crypt

This is where the meat of the encryption/decryption happens. Iterate over each character in the text, and use getOffsetChar to get the proper substitution character. Return a string of all of the substitution characters.

getOffsetChar

This function takes a single character (as a rune) and an offset. It should return the character (as a string) that is offset positions away from the given character. The alphabet has been provided for you.

For example, when offset = 5:

a -> f
b -> g
...
x -> c
z -> e

Or when offset = 1:

a -> b
b -> c
...
x -> y
z -> a

Remember that offset can be negative, and remember that the alphabet wraps around. For example, when offset = -5:

a -> v
b -> w
c -> x
d -> y
e -> z

If you can't find the character in the alphabet, return an empty string.