Note that in Sage webpages, you must evaluate the cells in order for the demonstration to work.
Here we define an alphabet:
xxxxxxxxxx
alphabet = AlphabeticStrings()
alphabet.gens()
Here we ‘encode’ our message in the alphabet:
xxxxxxxxxx
plaintext = alphabet.encoding("We are the champions.")
plaintext
Now we define the Shift Cryptosystem (i.e. Caesar cipher):
xxxxxxxxxx
system = ShiftCryptosystem(alphabet)
system
Now we can create a ciphertext out of our plaintext and a key. Here we use key=1, i.e. a shift by 1 character:
xxxxxxxxxx
key = 1
ciphertext = system.enciphering(key, plaintext)
ciphertext
Now we can decipher using the same key:
xxxxxxxxxx
plaintextrecovered = system.deciphering(key, ciphertext)
plaintextrecovered
Here’s a sandbox for you to play with:
xxxxxxxxxx
alphabet = AlphabeticStrings()
plaintext = alphabet.encoding("We are the champions.")
system = ShiftCryptosystem(alphabet)
key = 1
ciphertext = system.enciphering(key, plaintext)
plaintextrecovered = system.deciphering(key, ciphertext)
plaintextrecovered
Encryption
xxxxxxxxxx
alphabet = AlphabeticStrings()
plaintext = alphabet.encoding("We are the zucchini.")
system = ShiftCryptosystem(alphabet)
key = 1
ciphertext = system.enciphering(key, plaintext)
ciphertext
Decryption
xxxxxxxxxx
alphabet = AlphabeticStrings()
ciphertext = alphabet.encoding("XFBSFUIFAVDDIJOJ")
system = ShiftCryptosystem(alphabet)
key = 1
plaintext = system.deciphering(key, ciphertext)
plaintext