Saturday, March 16, 2013

Living my LiveCode Dream: Encryption Version 3.0



As I mentioned at the end of my previous post, the most obvious thing to explore next is how to write some code that reveals one of the target word's characters at random. There are many gaming possibilities to this technique, not the least of which would be a "Wheel of Fortune-like" version where a code word or phrase is slowly revealed.

Here's a screen shot of the finished version for us to explore using the word "Nowhere" as the target:

First, you'll notice that there are two buttons on the screen. The first, "Reset Target Word," takes whatever word or phrase is written in the target field and encrypts it with one asterisk per character, then puts this string of asterisks into the result field at the bottom of the screen. Here is the code for this button:

-----------------------------------------------------------------------------------------------

global varChar, varLength, varWord, varWordEncrypt, varEncrypt

on mouseUp
   put empty into varWordEncrypt
   put line 1 of field "target" into varWord
   put the length of varWord into varLength
   repeat with varChar = 1 to varLength
      put varWordEncrypt & "*" into varWordEncrypt
   end repeat
   put varWordEncrypt into line 1 of field "result"
end mouseUp

-----------------------------------------------------------------------------------------------

The variable varWordEncrypt will ultimately contain as many asterisks as there are characters in the target word, so Line 2 first empties that variable (important to do given that we can change the target word as often as we like).

Line 3 puts whatever is typed in the target field into a variable, varWord, that will hold the target information in its raw, unencrypted form.

Line 4 determines the length of varWord and puts this number into the variable varLength.

Lines 5-7 defines a repeating loop that puts as many asterisks into varWordEncrypt as there are characters.

Finally, line 8 puts varWordEncrypt into the result field toward the bottom of the screen.

It's important to remember at this point that the unencrypted version of the word or phrase remains contained in varWord. Let's move on to consider the script of the button "Reveal Random Letter":

-----------------------------------------------------------------------------------------------
 global varChar, varLength, varWord, varRandomChar, varWordEncrypt, varEncrypt

on mouseUp
   put random (varLength) into varRandomChar
   repeat for varLength times
      if char varRandomChar of varWordEncrypt = "*" then
         put char varRandomChar of varWord into char varRandomChar of varWordEncrypt
         exit repeat
      else
         add 1 to varRandomChar
         if varRandomChar > varLength then put 1 into varRandomChar
      end if
   end repeat
   put varWordEncrypt into line 1 of field "result"
end mouseUp

----------------------------------------------------------------------------------------------- 

Line 2 selects a random number between 1 and the total number of characters in the target, a number stored in varLength. It puts this random number into another variable named varRandomChar. So, to understand how all this works, let's just pretend that the first number chosen is 3.

Lines 3-11 defines a repeat loop that will replace the asterisk at the random spot (3 in our imaginary case) with the actual character from the target. Let's walk through how this happens.

Line 3 says to repeat the loop for as many times as there are characters in the target. The reason for this will become clearer later, if you haven't already surmised why this is.

Lines 4-10 define an if/then/else condition. Line 4 looks to see if the randomly chosen character is actually an asterisk. Of course, the very first time a character is chosen, we are guaranteed that it will be an asterisk. But take a moment to look into the future. What would happen if 3 is chosen again at randomly, just coincidentally, the next time the user clicks the button to reveal the next character? Well, we would like the computer to find another asterisk (i.e. another character to reveal). The strategy used in this if/then/else script ensures that one of the remaining asterisks will be chosen. Line 5 puts

So, let's continue with our hypothetical situation where 3 is chosen the first time. Keep in mind that varRandomChar would be equal to 3. Line 4 looks to see if it is an asterisk and it is, so then the next line takes over. Line 5 puts char 3 of varWord in char 3 of varWordEncrypt, a "w." The next line, still part of the "then" script, simply says OK, you're done, so exit the repeat loop and so it does. Line 12 tells the computer to put the current contents of varWordEncrypt into the text field "result." Now, keep in mind that varWordEncrypt now contains "**w****".

Now, imagine that the user clicks the "Reveal Random Letter" a second time and, amazingly, the computer again chooses the number 3. (Hey, the computer could conceivably, though improbably,  choose the number 3 every time.) Line 4 looks to see if the character 3 in varWordEncrypt is an asterisk. At this point, it is not, it is a "w." Therefore, line 5-6 are skipped and the "else" part of the script kicks in with line 8:

add 1 to varRandomChar

This simply adds one to 3, making varRandomChar equal to 4. The repeat loop then repeats a second time, doing exactly the same thing. It looks to see if character 4 of varRandomChar is an asterisk, and indeed it is. Consequently, this character - an 'h' -is revealed and the computer exits the repeat loop. The variable varWordEncrypt now equals "**wh***".

Now if you follow the logic of the if/then/else script within this repeat loop, you will understand that whatever random number is chosen, the next asterisk to come at that point or after will be chosen. Line 9 assures that we will stay within the boundary of the length of the target. So, if one is added to varRandomChar and it goes over varLength, then we simply tell the computer to go back to the first character of the target and continue on from there.

This is a very efficient strategy in that a character is revealed each time a random number is chosen, even if the same random number has been chosen previously. It would be tempting to use the strategy of telling the computer to keep picking a random number until it chooses one that has not been chosen before, but that could slow down the program considerably, especially if the word or phrase is long and all but 1 or 2 characters have been revealed.

I'm very motivated to try to make a little game based on this third encryption script. Check back soon to see what I come up with.