Loopy Secret Messages

RestrictedData on flickr

Improve your encryption skills with Unicode!

Every letter, digit, and punctuation mark in every human language (and a few non-human ones!) has a corresponding number to represent it inside the computer. In the old days these were ASCII codes, which ranged from 0 to 127, and mostly only worked for English. Nowadays we use a much larger set called Unicode. Unicode has over 140 thousand characters, but you can still find the ASCII set within it: codes 0 to 127 are exactly the same as the old ASCII set (see Table 1).

Table 1. Unicode codes 32 through 127 (the printable ASCII set).

We can use these numeric codes to easily encode and decode secret messages. One well-known way to do this is called ROT-13, because it “rotates” the alphabet 13 places, mapping A to N, B to O, and so on. If you go past the end of the alphabet, you just wrap around: so M maps to Z, N maps to A, and O maps to B. Because our alphabet has 26 letters, and 13 is exactly half of 26, if you apply this mapping twice, you get back the original message.

You can make your own secret message encoder/decoder using MiniScript! If you have Mini Micro already, you can use edit and run to create the program in Listing 1. If not, you can do it just as well on the Try It! page at https://miniscript.org/tryit/ . In either case, type the program in carefully, being sure to match the capitalization and punctuation shown.

msg = input("Message: ")
result = ""
for c in msg
num = c.code
if num >= 97 and num <= 122 then num = num + 13 if num > 122 then num = num - 26
else if num >= 65 and num <= 90 then num = num + 13 if num > 90 then num = num - 26
end if
result = result + char(num)
end for
print result

Listing 1. ROT-13 encoder/decoder

When the program runs, it will prompt you to type in a message. Enter anything you like, and if it’s plain language, it will return it as ROT-13 gibberish. Send that to a friend or save it as a note to yourself. Then, when you type or paste in ROT-13 text, it will convert it back into plain text.

The program works by looping over every character in the message (lines 3-13). It uses the code method to get the Unicode value that corresponds to each character on line 4, and store this in a variable called num. Then — referring to Table 1 — we use an if/else structure to check if the character is a lowercase letter (lines 5-7) or an uppercase letter (lines 8-10). If it’s one of these, we add 13 to the number, and if it goes past the end of the alphabet, subtract 26 to get it back in range. Finally, the numeric code is converted back into a character with the char function, and added to the result on line 12.

Want to take this program further? Try adding a while true/end while loop around the whole thing, so that you can encrypt/decrypt many messages without having to run the program for each one. Happy coding!

Learn More



What is ROT-13?


ROT-13 Cipher


Numeric Codes


What is a substitution cipher?


Substitution Cipher video


Substitution Cipher Kids


ASCII codes




What is Unicode?


Unicode explained


Also In The October 2022 Issue

A collection fo gift ideas for the holidays and any time of year!

Begin your 3D design journey with Blender!

Technology is here to stay, so we should talk about how to be safe when using it.

Experience a new level of futuristic luxury with your own robot butler.

Find out how your country compares to the rest of the world in CS education

Collection of interesting stories about technology from October 2022

Collection of the Learn More links from all of the October 2022 stories, in one place.

— Edger Dijkstra

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!