We can use Sage to encrypt and decrypt messages with a Caesar cipher! This makes it easier to work with long messages.
The code below can be used to decrypt ciphertext that was encrypted using a Caesar cipher. Adjust the value for "key" to determine the correct value for the secret key k. You can also change the ciphertext message! Copy and paste the code in the cell below.
alphabet = AlphabeticStrings()
ciphertext = alphabet.encoding("EMTKW UMBWJ ZWWST GVJMT QMDMB PMPG XM")
system = ShiftCryptosystem(alphabet)
key = 5
plaintext = system.deciphering(key, ciphertext)
plaintext
The code below can be used to encrypt plaintext using a Caesar cipher with a shift of your choice. Adjust the value for "key" to create different ciphertexts. You can also change the plaintext message! Copy and paste the code in the cell below.
alphabet = AlphabeticStrings()
plaintext = alphabet.encoding("You have to spend some energy and effort to see the beauty of math.")
system = ShiftCryptosystem(alphabet)
key = 7
ciphertext = system.enciphering(key, plaintext)
ciphertext