Apply Caesar Cipher to Words from Terminal
This project 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.
Example project: caesar_terminal_words
- Enter caesar_terminal_words, then flash it into the micro:bit.
- Open the terminal, make sure local echo is checked, and follow the prompts. Valid keys are from -25 to 25.
- Try encrypting HELLO with the key set to 5.
- Try decrypting MJQQT with the key set to -5.
- Write a short message for your friend or lab partner and give them the key to decrypt it.
How caesar_terminal_words Works
Inside the forever loop, the project stores a number you enter into an int variable named key. Then, it stores a word you type in a string variable named word.
In addition to the alpha (alphabet) string, a second empty string named result is created to store the 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 a few blocks. The first finds the index of the new character with set index to (remainder of ((alpha) find index of (letter) + key ) / 26). After that, there’s an if… loop that makes sure it isn’t a negative index. The last block adds the character to whatever is already in the ciphertext string with set result to (join (result) (char from (alpha) at (index))).
Before repeating the loop, the ciphertext is printed to the terminal.