Modifying the key = and letter = statements to encrypt and re-flashing a script to get a ciphertext character isn’t very efficient. It would be much easier for your script to pass a function, a key, and a word to a function and let it encrypt or decrypt the whole string. Getting there can be broken into three steps:
Another reason for setting it up this way is that you can replace a Caesar cipher function with a different encryption function, and the main script might only need one line modified to get it to work.
In this example, the terminal will prompt you for a key and a letter, and then display the Caesar cipher result. Previously, the encryption key was hard-coded as 5. With this script, you can enter 5, or 13 for ROT-13, or any other value you decide to use. Next, enter the letter to encrypt, and the script displays the ciphertext result in the terminal.
# caesar_terminal_letters from microbit import * sleep(1000) print("Set your keyboard to CAPS LOCK.") print() while True: text = input("Enter key: ") key = int(text) letter = input("Enter a letter: ") letter = letter.upper() alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" index = alpha.find(letter) index = index + key index = index % 26 result = alpha[index] print("result =", result) print()
The techniques for building this script make use of:
Instead of hard coding key = 5 and letter = "M", like in caesar_cipher_letter, this script makes it work during runtime with:
text = input("Enter key: ") key = int(text) letter = input("Enter a letter: ")
Before doing the Caesar cipher, it uses letter = letter.upper() which changes any lower-case letter to upper-case. …just in case you forgot to set your keyboard’s CAPS LOCK.
These steps from the previous example sketch can be performed in one line:
index = alpha.find(letter) index = index + key index = index % 26
Here’s how it would look:
index = ( alpha.find(letter) + key ) % 26
This more compact format will be used in the next example script.
This script places the Caesar cipher in a loop. This will allow you to type entire words for encrypting/decrypting! It still also works with single characters if that’s all you want to encrypt.
# caesar_terminal_words from microbit import * sleep(1000) print("Set your keyboard to CAPS LOCK.") print() while True: text = input("Enter key: ") key = int(text) word = input("Enter character(s) in A...Z range: ") alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" result = "" for letter in word: letter = letter.upper() index = ( alpha.find(letter) + key ) % 26 result = result + alpha[index] print("result:", result) print()
Inside the while True loop, the script stores a number you enter into an int variable named key. Then, it stores a word you type in a string variable named word.
text = input("Enter key: ") key = int(text) word = input("Enter character(s) in A...Z range: ")
In addition to the alpha (alphabet) string, a second empty string named result is created to store the result.
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" result = ""
After that, a for… loop goes through each letter in the plaintext string. After making sure the letter is upper-case, it applies the Caesar cipher with two lines. The first finds the index of the new character with index = ( alpha.find(letter) + key ) % 26. The second adds the character to whatever is already in the ciphertext string with result = result + alpha[index].
for letter in word: letter = letter.upper() index = ( alpha.find(letter) + key ) % 26 result = result + alpha[index]
Before repeating the loop, the ciphertext is printed to the terminal.
print("result: ", result)
Why move the Caesar cipher routine to a function? One advantage would be that you can swap it out with other, better encryption functions, or even function/method calls to a module. As an example, in the next activity, you will replace the caesar function with another one called ascii_shift. After the function swap, your script will only need one line changed!
# caesar_cipher_function from microbit import * ''' Function converts plaintext to ciphertext using key ''' def caesar(key, word): alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" result = "" for letter in word: letter = letter.upper() index = ( alpha.find(letter) + key ) % 26 result = result + alpha[index] return result ''' Script starts from here... ''' sleep(1000) print("Set your keyboard to CAPS LOCK.") print() while True: text = input("Enter key: ") key = int(text) letters = input("Enter character(s) in A...Z range: ") result = caesar(key, letters) print("result:", result) print()
The functionality is identical to the previous example (caesar_terminal_words).
See how all that script has to do is use result = caesar(key, letters) to encrypt the word?
Your script now gets the ciphertext from this one line:
result = caesar(key, word)
The caesar function has two parameters, key and word. Keep in mind that word could also be ciphertext that you are converting back with a negative key value. Inside the function, it does all the same steps as caesar_terminal_words, and then returns the result.
def caesar(key, word): alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" result = "" for letter in word: letter = letter.upper() index = ( alpha.find(letter) + key ) % 26 result = result + alpha[index] return result
Links
[1] http://learn.parallax.com/tutorials/robot/cyberbot/computer-microbit-talk/input-messages
[2] http://learn.parallax.com/tutorials/robot/cyberbot/writing-microbit-programs/count-and-repeat
[3] https://learn.parallax.com/node/2269