Building a simple crypto code maker in Scratch is actually pretty quick! The cipher built here is also known as a Caesar Cipher. Basically you give a shift amount and a letter and the program moves through the alphabet by the shift amount, looping around to A if it gets to Z, and gives you the secret letter. For example, if you wanted to tell your young coder the plaintext word “hi,” you could use a shift to move each letter to a new position in the alphabet, obscuring the original message. With a shift of 6, the plaintext, “hi” becomes the ciphertext word “NO.”
It might be a good idea to play this game with a paper and pencil with your young coder before you start coding, because then you have an idea of what needs to be built! You can even have your young coder write the steps that they use to figure it out as they play with the unplugged version!
To build the crypto code maker in Scratch, follow these steps:
- Create a list called “letters” in Scratch that contains all 26 letters.
- Create four variables that will keep track of the ciphertext (output), plaintext (input), shift, and position of the counter used during the algorithm.
Your list and these four variables are visible under the Data category.
The four variables and one list you need in your crypto code maker code.
Make sure your ciphertext, plaintext, and shift amount variables and your letters list are visible on the stage by selecting the check box next to them. Because the position variable is keeping track of the position as you iterate through the alphabet, it doesn’t have to be visible on the stage, so make sure you uncheck the box next to it. Your stage should look something like this.
The stage of your crypto code maker should be similar.
- Ask the user for the plaintext letter and the shift amount. Be sure to save the answers from the user in the appropriate variables.
The completed code for the Scratch crypto code maker.
- Starting at position 1 in the list (A), iterate through the list of letters until you reach the letter that the user submitted.
When this loop ends, the variable position holds the number that corresponds with the plaintext letter given by the user (for example, 1 is A, 2 is B, and so on).
- Add the shift amount to the position, then mod by 26.You have to mod by 26 to allow the cipher to start back at A if it reaches Z. For example, if the plaintext letter the user submits is X and the shift is 6, the ciphertext is D. You can evaluate this mathematically as well: X is at position 24. 24 + 6 is 30. 30 % 26 is 4. 4 is the position of letter D.
- Get the ciphertext letter from the list, and display it to the user.
As a bonus challenge, can you have your young coder figure out how to make a reverse version of this program? See if he can build a version to convert ciphertext into plaintext!