Steganography
A message hidden in the spaces at the end of the lines of any text. It was the best-loved exercise in our evenings with the students of the ITIS Marconi: you understand it in five minutes and program it in an hour.
Cryptography makes a message unreadable, but whoever intercepts it understands there is a secret. Steganography does the opposite: it leaves the message readable and hides that it exists. A letter about the weather, a holiday photo, the header of a technical document: whoever doesn't know where to look finds nothing to decipher, and has no reason to try.
The trick of the spaces
In the 2018 developer course the exercise asked for this: take a text file and a secret word, and write the word at the end of the lines, one letter per line.1 Each letter becomes a number, the number eight binary digits, and each digit a character you can't see: a space is 0, a tab is 1. A is the number 65, in binary 01000001: a space, a tab, five spaces, a tab.
Editors don't show the spaces at the end of a line, and neither does a printout. The text stays identical, letter for letter, and carries the message with it.
You need JavaScript for the experiment.
The cover text is the beginning of BIP-32: the header is the same one the 2018 notebook used. In the notebook's copy some lines already had spaces at the end, left over from a copy and paste: that is why the program first removes the spaces it finds, and only then writes its own.
The 2018 flaw
The program written back then worked with ciao, the word tried in the notebook. It also worked with perché: the é is character 233, which fits in eight binary digits. With 5€ it didn't. The euro is character 8364 and needs fourteen digits: the program wrote them all, the decoder read eight, and the message came back as 5¬. With an emoji, the smiling face 🙂 (character 128578), it came back as a B.
The experiment above goes one step further: it first turns the message into bytes with UTF-8, the encoding almost every program uses, then writes one byte per line. The euro takes three lines, but comes back as the euro.
Hidden doesn't mean protected
Cryptography and steganography put security in two opposite places.
A good cipher holds even when the enemy knows the method. Auguste Kerckhoffs wrote it in 1883: the system must be able to fall into the adversary's hands without harm, and the secret must lie entirely in the key.2 That is why SHA-256, AES and Bitcoin's signatures are public in every detail, studied by thousands of people, and still hold: the key is a number too large to guess. Whoever intercepts an encrypted message knows there is a secret, but can't read it.
Steganography, instead, holds only as long as nobody knows where to look. Its secret is the method: whoever knows the trick of the spaces reads every message hidden that way, with no key to find. And it has two weaknesses that cryptography doesn't have.
- It gets discovered. Eight spaces or tabs at the end of every line are odd, to anyone looking for them. The same goes for the more refined tricks: a message hidden in the pixels of a photo changes its statistics, and there are programs that measure them. Once found, a hidden message that isn't encrypted is a message in the clear.
- It breaks. All it takes is a mail program that strips spaces at the end of lines, a chat that recompresses photos, an editor that tidies the file. The message vanishes, the sender doesn't know, and the receiver finds nothing.
That is exactly what makes it fascinating: a secret that doesn't look like a secret and passes under the eyes of whoever is looking for it. And for the same reason it is fragile. Whoever is serious uses the two together: first encrypt, so the message that's found can't be read; then hide, so nobody knows there's anything to read.
In Python
Write two functions, with the standard library only: hide(text, secret), which returns the text with the message inside, and find(text), which pulls it out. Then open the “Find” tab, press “The page's bottle” and download the text with “Download”: your program must find the same message the page finds.
A solution
def hide(text, secret):
lines = [r.rstrip(" \t") for r in text.split("\n")]
data = secret.encode("utf-8")
if len(data) > len(lines):
raise ValueError(f"{len(data)} lines needed, the text has {len(lines)}")
for i, byte in enumerate(data):
lines[i] += format(byte, "08b").replace("0", " ").replace("1", "\t")
return "\n".join(lines)
def find(text):
data = bytearray()
for line in text.split("\n"):
tail = line[len(line.rstrip(" \t")):]
if len(tail) == 8:
data.append(int(tail.replace(" ", "0").replace("\t", "1"), 2))
return data.decode("utf-8", errors="replace")
with open("bottiglia.txt", encoding="utf-8") as f:
print(find(f.read()))
Three questions for afterwards:
- Does the message arrive whole if you send the text in a chat or by mail? Try it with the programs you use.
git diffmarks spaces at the end of lines in red. Why would a programmer notice the trick at once?- How would you make sure the message can't be read even when it's found?
Two and a half thousand years of hidden messages
Herodotus tells two stories that are still studied. Histiaeus, held at the Persian court, wants to push his son-in-law Aristagoras to revolt without being discovered: he shaves the head of a trusted slave, tattoos the message on his scalp and waits for the hair to grow back. The slave reaches Aristagoras with a single sentence to report: shave me and look.3 Demaratus, an exile in Persia, wants to warn Sparta that Xerxes is preparing for war: he scrapes the wax off a tablet, writes on the wood and covers it with wax again, so the tablet looks empty. In Sparta nobody understands, until Gorgo, the wife of King Leonidas, guesses that the wax must be scraped away.4
The word, though, comes from a book. Around 1499 Johannes Trithemius, a German Benedictine abbot, wrote the Steganographia: from the Greek steganós, covered, and gráphein, to write. It looks like a manual for sending messages far away with the help of spirits, with their names and the formulas to call them.5 It stayed in manuscript for more than a century, was printed in Frankfurt in 1606 and placed on the Index of Forbidden Books in 1609. That the first two books were ciphers was understood at once, because the key came out with the printing; the third, the one on the spirits of the planets, was only deciphered in 1996 by Thomas Ernst and in 1998 by Jim Reeds: there too the incantations were enciphered texts.6
The trick of the spaces even has a program of its own: SNOW, by Matthew Kwan, already around in 1998. The name stands for Steganographic Nature Of Whitespace, and the logo is a white bear in a snowstorm, because finding spaces at the end of lines is like looking for a polar bear in the snow. Unlike the 2018 exercise, SNOW can also encrypt the message before hiding it: the two things together, as said above.7
A bitcoin payment is a handful of bytes, about 250: as much as a tweet. In 2015 Amadori wrote that it can be sent in any way that 250 bytes can pass, “even through a Skype smiley encoding”, and in 2017 he used smileys to explain how hard it is to block Bitcoin.8 The same game works for hiding: a row of emoji in a WhatsApp or Telegram message, the recovery words in the background of a holiday photo, a private key inside a painting. Whoever looks sees smileys, a beach, a landscape. But everything above applies: whoever knows the trick finds it, and a chat that recompresses the photo wipes it out. For real bitcoin, the passphrase first, which makes the words useless to whoever finds them, and only then the hiding place.
And Bitcoin? Signatures and hashes hide nothing: they are seen, and exist precisely to be seen. But a close relative of steganography does exist. With Taproot, active since 2021, an output can hold an entire tree of scripts folded into its public key, and if it is spent with a single signature nobody will ever know those scripts were there.9 It is the same principle as RGB's seals, told in the last exercise of Sealed envelope.
In class
Twenty minutes, in pairs, with phones. The part without programming works for middle school too.
- Hide. One of the two writes a short message in the “Hide” tab, copies the text and sends it to their partner, in the class chat or by mail.
- Find. The other pastes it in the “Find” tab. Sometimes the message arrives, sometimes it doesn't: it depends on what the program that carried it strips out, and finding out which one strips it is part of the game.
- At the board. What are the red dots and arrows? Why does a letter need eight digits and the euro twenty-four?
For a whole hour on Bitcoin there is In class; for the exercises with the answers sealed in a hash, Sealed envelope.
-
bcademy course, “Steganografia” notebook from the materials, November 2018; inbitcoin archive. The notebook contains the exercise brief, the test text with the BIP-32 header and a student's program, tried with the word
ciao. ↩ -
Auguste Kerckhoffs, “La cryptographie militaire”, Journal des sciences militaires, 1883: the second of his six principles. ↩
-
Herodotus, Histories, book V, chapter 35. ↩
-
Herodotus, Histories, book VII, chapter 239. ↩
-
It is related to the Steganotheophany, but the margin on this site is too small to go into it. ↩
-
Jim Reeds, “Solved: The Ciphers in Book III of Trithemius's Steganographia”, Cryptologia 22, no. 4 (1998), pp. 291-317; Thomas Ernst, “Schwarzweiße Magie”, Daphnis 25, no. 1 (1996). The dates of printing, of the key and of the Index are summarised in the Wikipedia entry Steganographia, read on 20 November 2026. ↩
-
The SNOW Home Page, by Matthew Kwan, read on 20 November 2026. The page mentions an error corrected on 22 November 1998, and the program has been free software under the Apache 2.0 licence since 2013. ↩
-
Amadori, Facebook comment, 1 April 2015; and at Trentino Sviluppo, “Bitcoin e blockchain”, 13 December 2017, min. 21. ↩
-
BIP-341, read on 20 November 2026: the output key is the internal key “tweaked” with the commitment to the script tree. Active from block 709,632, November 2021. ↩