Rot13 (Sed)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Forth | Haskell | Java | Python | Scheme | Sed

A sed script implementation of a Rot13 en-/decoding filter.


Rot13 shifts each element of the english alphabet forward (equivalently, backward) by 13 places. As this alphabet contains 26 letters, 26 / 13 = 2, exactly, and hence this function is an involution — like a hallway light-switch, it is its own inverse.

Question: an involution obeys the equation ff = e, where e is the identity. What are a few examples of command line filters that are projections — i.e. they are idempotent: ff = f?

Question: What is an example of a common command line filter that is both an involution and a projection?

Question: Why is it desirable for ciphers to act more like permutations, less like projections? Also, why do involutions make weak ciphers?


As there are only 52 characters involved, it is easier to provide a translation table than to bother calculating offsets at runtime.

Question: Which x86 opcode implements single-byte translation tables?

Rather than use the traditional tr a-zA-Z n-za-mN-ZA-M presentation, we choose a presentation to make the involution clear.

#/bin/sed -f

Question: What would a program to generate this lookup table look like? Can these strings be written as a functional expression in your favorite language?

wrapping up

Finally, we take some sample text from Rot13 (Java)

Now is the Winter of our Discontent,
Made glorious summer by this Son of Yorke:

to check that we generate the same output

% rot13.sed < rot13.txt
Abj vf gur Jvagre bs bhe Qvfpbagrag,
Znqr tybevbhf fhzzre ol guvf Fba bs Lbexr:

and that (at least for this case) we do indeed have an involution

% rot13.sed < rot13.txt | rot13.sed
Now is the Winter of our Discontent,
Made glorious summer by this Son of Yorke:
Download code