This next script will encrypt individual letters with the Caesar cipher. As-written it encrypts the letter M with a key of 5.
# 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()
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.