Encrypt a Single Letter
This next script will encrypt individual letters with the Caesar cipher. As-written it encrypts the letter M with a key of 5.

Example script: caesar_encrypt_letter
- Change project’s name from find_letter_in_alphabet to caesar_encrypt_letter.
- Update it to match the script below
- Save the modified script.
- Click the Send to micro:bit button.
# caesar_encrypt_letter
from microbit import *
sleep(1000)
key = 5
letter = "M"
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
index = alpha.find(letter)
print("key =", key)
print("letter =", letter, ", index =", index)
index = index + key
index = index % 26
result = alpha[index]
print("new index =", index, ", result =", result)
print()
- Check the results in the serial monitor, and verify that:
- The plaintext letter is M and its index is 12
- The new index is 17 with the resulting ciphertext letter R.
Decrypt with Caesar Cipher
What if a script receives a Caesar encrypted character and a key? Do you need to write another script? The answer is no. To decrypt a ciphertext letter to plaintext, just use the negative of the key that encrypted it. So, if a ciphertext character was encrypted with 5, it can be decrypted with -5.
Remember how a plaintext letter M encrypted to a ciphertext letter R when the key was 5? To decrypt a ciphertext letter of R with a key of 5, just run it through the same Caesar cipher with a key of -5. The result will be the plaintext character M.
- If you don’t already have it open, reopen caesar_encrypt_letter.
- Set the key to -5 and the plaintext letter to “R”.
- Flash the modified script into the micro:bit.
- Verify that the ciphertext letter (which is really the plaintext letter) result is “M”.
