It takes a while to well and truly shuffle the characters in an alphabet by hand, so why not use a script?
- Enter, name, save, and flash cryptabet_autogenerate
- If the serial monitor isn't already open, click Show serial.
- Wait while it builds the cryptabet with an approach that kind of resembles a brute force attack!
If you want the scramble to be repeatable, you can add a statement like random.seed(10) anywhere above while n < size: For each different seed, you will get a different, but repeatable, result. Without a manually entered seed value, the micro:bit uses the system timer to get a more random seed to determine the sequence.
Example script: cryptabet_autogenerate
# cryptabet_autogenerate from microbit import * import random sleep(1000) alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" size = len(alpha) print("size: ", size) print("alpha:", alpha) sleep(1500) crypta = "" n = 0 while n < size: sleep(50) r = random.randint(0, size-1) c = alpha[r] print("r:", r, ", c:", c) if crypta.find(c) == -1: crypta = crypta + c n = n + 1 print("n:", n, ", crypta:", crypta) sleep(1000) print("crypta:", crypta) sleep(100) print()
How It Works
The cryptabet_auto_generator script starts with alpha, which is set to the upper-case alphabet for the sake of example. It repeatedly generates random numbers in the 0…25 range, and then grabs the character at that index from alpha. Then, it checks if that character is already in crypta. If yes, then it discards the character and tries again. If no, then it appends the character to crypta and starts adding the next character.
Sleep statements were added at various points to give the online editor’s terminal enough time to keep up with the stream of text data.
The script uses the length of the alpha string to build the crypta string. So there would be no problem using a custom alphabet, like alpha = "vLR{}:,’01234 56789", for example. That particular alphabet would support dictionaries from the Terminal Control - Go Wireless activity.