If you are designing a quiz project in Scratch, you will check if the user could answer the questions correctly. However, since all the published projects share the code, your user could always “see inside” of the project and get the answer, like the following code snippet. How could you test your user without getting cheated by them? We could encrypt the correct answer, so it is much more difficult for the user to “cheat” on the quiz.
The following code snippet uses the block “Encrypt” to encrypt the user’s answer. After that, it compares the encrypted result with the expected answer (also encrypted).
Not only to encrypt number, we could also encrypt alphabet characters, but need a little bit different method to implement. All the encryption is done by “Encrypt” block. Now, let’s take a look at how to write the encryption functionality in our Scratch project.
First, we need to create a block, e.g. Encrypt. It has one input parameter “user_input”, which represents the value of “answer” block (you could find this block in Sensing tab). We set two variables “result” and “k”. The initial value of “result” is empty, and the initial value of “k” is 0.
The following step is to repeat “length of user_input”. The purpose is to encrypt each number one by one. For example, if the user inputs the answer “87”, the loop will repeat 2 times because the input contains two numbers. Through calling “letter k of user_input”, the program retrieves each number from the input. For the first iteration, it gets “8” and stores it in variable “char”. If char is equal to 0, the encrypted value is item 10 of number_list, which is “j” in this case. For other numbers 1 to 9, the encrypted values are a to i in number_list.
Through the above code, we could encrypt the number “87” to “hg”. The next step is to compare the encrypted user input with the expected encrypted value. Using the encryption, even the user sees the code and inputs “hg” as answer, the encrypted result is not “hg”, so user fail to cheat on the quiz game. That is what we want “Encrypt” function to do.
The above example is illustrated below.
Let’s continue explaining the code of “Encrypt” block. Different from the number, the input of alphabet characters could not be converted directly into the item value in a list. We need an extra step to match the input character with encrypted one.
First, we create one list called “alphabet list”. The list structure is a bit tricky. Item 1 to 26 are filled with some special characters, as shown in the below diagram. Item 27 to 30 keep empty (or any other characters). Item 31 to Item 56 are filled with alphabet characters a to z.
When the user inputs some characters, for example, “fici”, in the first iteration, the program gets the first character “f” and stores it into the variable “char”. “item # of char in alphabet list” expression return the index of the “f” in alphabet list, which is 36. Since the encrypted values are stored in item 1 to 26 of the list, we use 36 – 30 and store the value 6 in variable “alphabet_pos”. The program then calls “item alphabet_pos of alphabet list” to get the #6 item value, which is “^”.
Using the same method, the program gets all the encrypted value of the other three input characters “ici” and store them to the variable “result”. The next step is to compare the encrypted user input with the expected result “^(#(“.
That is all the code to implment encryption. Hope you are not overwhelmed. If you are keen to watch the Scratch code by yourself, here is the project link: https://scratch.mit.edu/projects/648234418/editor/. Enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.