Secret messages
A laptop that encodes messages with the Caesar cipher: every letter moves a few places on in the alphabet, so with the key 3, Meet me at the Nether portal! becomes Phhw ph dw wkh Qhwkhu sruwdo!. Your friend decodes it with the same key on their laptop.
What you need
A trigger block with a button and a laptop. The program has no parameters and no results.
The idea
To a computer, a character is a number: its code point. ASC gives the code point of a character, CHR the character of a code point. The capital letters A to Z are 65 to 90, the small letters a to z are 97 to 122, one after the other.
So moving a letter on by 3 is arithmetic: subtract the code of A to get its place in the alphabet (0 to 25), add 3, take the remainder of the division by 26 so that Z wraps around to C, and add the code of A again. Everything that is not a letter – spaces, digits, ! – stays as it is. Decoding is the same with the key turned around: -3.
The program
PROGRAM caesar
NOOP
INCLUDE c0de/strings
NOOP
FUNCTION shiftLetter
PARAMETER letter CHAR
PARAMETER key INTEGER
RESULT result CHAR
BEGIN
VARIABLE code INTEGER
VARIABLE first INTEGER
ASSIGN result letter ;
CALL c0de/strings/ASC
PARAMETER char letter ;
RESULT result code
IF code ≥ 65 AND code ≤ 90 ;
BEGIN
ASSIGN first 65 ;
END IF
IF code ≥ 97 AND code ≤ 122 ;
BEGIN
ASSIGN first 97 ;
END IF
IF first > 0 ;
BEGIN
CALL c0de/strings/CHR
PARAMETER code first + ( code - first + key \ 26 + 26 ) \ 26 ;
RESULT result result
END IF
END FUNCTION
NOOP
FUNCTION encode
PARAMETER text STRING
PARAMETER key INTEGER
RESULT result STRING
BEGIN
VARIABLE length INTEGER
VARIABLE letter CHAR
CALL LEN
PARAMETER value text ;
RESULT result length
FOR i INTEGER 0 ; length - 1 ;
BEGIN
CALL shiftLetter
PARAMETER letter text[i] ;
PARAMETER key key ;
RESULT result letter
ASSIGN result result ⊕ letter ;
END FOR
END FUNCTION
NOOP
FUNCTION laptop
BEGIN
VARIABLE message STRING
VARIABLE key INTEGER
VARIABLE secret STRING
VARIABLE back STRING
CALL CLS
CALL PRINT
PARAMETER value "Message?" ;
CALL INPUT
PARAMETER variable message ;
CALL PRINT
PARAMETER value "Key?" ;
CALL INPUT
PARAMETER variable key ;
CALL encode
PARAMETER text message ;
PARAMETER key key ;
RESULT result secret
CALL encode
PARAMETER text secret ;
PARAMETER key - key ;
RESULT result back
CALL PRINT
PARAMETER value "Secret: " ⊕ secret ;
CALL PRINT
PARAMETER value "Decoded: " ⊕ back ;
END FUNCTIONBuilding and running it
- Place the laptop, right-click it, click the buttons until they show
caesarandlaptop, and click ✔. - Set the trigger block to the same program and function and press the button.
- Open the laptop. Type
Meet me at the Nether portal!into the input line and press Enter, then the key3.
The screen shows:
Message?
Meet me at the Nether portal!
Key?
3
Secret: Phhw ph dw wkh Qhwkhu sruwdo!
Decoded: Meet me at the Nether portal!
To read a message from a friend, run the program again, type the secret text and the key with a minus: Phhw ph dw wkh Qhwkhu sruwdo! and -3 give Secret: Meet me at the Nether portal!.
The cursor moves one line per tick and walks through shiftLetter for every character, so a message of 30 characters takes about 20 seconds per direction. Watch the cursor run through the loop on the sheet.
How it works
encodegoes through the text with FOR from 0 tolength - 1.text[i]reads one character of a STRING as a CHAR, andresult ⊕ letteradds the shifted character to the end of the result.shiftLetterstarts withresultset to the letter itself, so anything that is not a letter comes back unchanged.- ASC gives the code. The two IFs set
firstto 65 for a capital letter and to 97 for a small one; for anything else it stays 0. - CHR turns the shifted code back into a CHAR. The code inside is
first + ( code - first + key \ 26 + 26 ) \ 26:\is the remainder.key \ 26brings any key into -25 to 25, and the+ 26makes the sum positive, because the remainder of a negative number is negative in c0de (-7 \ 26is -7). So the key -3 works like 23, and 29 like 3. laptopencodes the message and then decodes the secret with- key, so you can see that the way back works.
Try it yourself
- ROT13 is the Caesar cipher with the key 13. What is special about it? Try
Uryyb Fgrirwith the key 13.
Solution
The program writes Secret: Hello Steve. 13 is half of 26, so the key 13 encodes and decodes: the same key twice brings every letter back to where it was, and Decoded is Uryyb Fgrir again.
- Hide numbers too: shift the digits
0to9(code points 48 to 57) by the key, so that the coordinates of your base are secret.
Solution
PROGRAM caesardigits
NOOP
INCLUDE c0de/strings
NOOP
FUNCTION shiftLetter
PARAMETER letter CHAR
PARAMETER key INTEGER
RESULT result CHAR
BEGIN
VARIABLE code INTEGER
VARIABLE first INTEGER
ASSIGN result letter ;
CALL c0de/strings/ASC
PARAMETER char letter ;
RESULT result code
IF code ≥ 65 AND code ≤ 90 ;
BEGIN
ASSIGN first 65 ;
END IF
IF code ≥ 97 AND code ≤ 122 ;
BEGIN
ASSIGN first 97 ;
END IF
IF first > 0 ;
BEGIN
CALL c0de/strings/CHR
PARAMETER code first + ( code - first + key \ 26 + 26 ) \ 26 ;
RESULT result result
END IF
IF code ≥ 48 AND code ≤ 57 ;
BEGIN
CALL c0de/strings/CHR
PARAMETER code 48 + ( code - 48 + key \ 10 + 10 ) \ 10 ;
RESULT result result
END IF
END FUNCTION
NOOP
FUNCTION encode
PARAMETER text STRING
PARAMETER key INTEGER
RESULT result STRING
BEGIN
VARIABLE length INTEGER
VARIABLE letter CHAR
CALL LEN
PARAMETER value text ;
RESULT result length
FOR i INTEGER 0 ; length - 1 ;
BEGIN
CALL shiftLetter
PARAMETER letter text[i] ;
PARAMETER key key ;
RESULT result letter
ASSIGN result result ⊕ letter ;
END FOR
END FUNCTION
NOOP
FUNCTION laptop
BEGIN
VARIABLE message STRING
VARIABLE key INTEGER
VARIABLE secret STRING
VARIABLE back STRING
CALL CLS
CALL PRINT
PARAMETER value "Message?" ;
CALL INPUT
PARAMETER variable message ;
CALL PRINT
PARAMETER value "Key?" ;
CALL INPUT
PARAMETER variable key ;
CALL encode
PARAMETER text message ;
PARAMETER key key ;
RESULT result secret
CALL encode
PARAMETER text secret ;
PARAMETER key - key ;
RESULT result back
CALL PRINT
PARAMETER value "Secret: " ⊕ secret ;
CALL PRINT
PARAMETER value "Decoded: " ⊕ back ;
END FUNCTIONA third IF does the same for digits with 10 instead of 26. With the key 3, Base at 120 64 -300 becomes Edvh dw 453 97 -633, and the key -3 brings it back.
Read more
- c0de/strings: ASC, CHR
- Types: CHAR and STRING; TYPE_STRING: reading one character with an index
- Operators:
\, the remainder - The cursor: one line per tick