ROT13
ROT13 replaces each letter with the one 13 places later in the alphabet. The demo runs this on every keystroke.
One character at a time
flowchart TD
A["Take the next character"] --> B{"Is it a letter?"}
B -- no --> C["Copy it unchanged"]
B -- yes --> D["Find its position: A is 0, B is 1, ... Z is 25"]
D --> E["Add 13"]
E --> F["Wrap around with the remainder after dividing by 26"]
F --> G["Turn the new position back into a letter, keeping the case"]
C --> H["Move to the next character"]
G --> H
Why the same function decodes
The alphabet has 26 letters, so shifting by 13 twice is a shift by 26, which lands back on the starting letter. The first half of the alphabet swaps with the second half, and the swap is its own inverse.
| A | B | C | D | E | F | G | H | I | J | K | L | M |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
ROT13 offers no security. Anyone can reverse it, which is why it is used for hiding spoilers and punchlines instead of secrets.