Ciphers
Playfair
Letter pairs through a keyed 5×5 table. J folds into I, doubled pairs are split with X, odd length is padded with X.
- create
- create("playfair")
- family
- Digraph
- options
- --key
- self-inverse
- no
- keyspace
- 25! ≈ 1.5×10²⁵
- try it
- ciphers playfair "HIDE THE GOLD" --key 'PLAYFAIR EXAMPLE'
The key fills a 5×5 table, duplicates dropped, rest of the alphabet after it, J folded into I to fit 25 cells. The text is stripped to letters and cut into pairs. Same row - move right. Same column - move down. Otherwise each letter takes the other's column. Two identical letters in a pair get split with an X, and an odd final letter is paired with X. Output is uppercase without spaces, because the input has been reshaped anyway.
const playfair = create("playfair");
playfair.encode("HIDE THE GOLD IN THE TREE STUMP", { key: "PLAYFAIR EXAMPLE" }).text;
// "BMEAZBXDNABEKUDMUIXMMOUVIF"
playfair.decode("BMEAZBXDNABEKUDMUIXMMOUVIF", { key: "PLAYFAIR EXAMPLE" }).text;
// "HIDETHEGOLDINTHETREXESTUMP"
The round trip keeps the X that split TREE. That's the cipher, not a bug, and every Playfair implementation that claims otherwise is guessing. Key is required and must contain letters. The Wikipedia vector above is the one the test suite checks.