text.ciphers
classic ciphers, Morse code, Braille and the NATO spelling alphabet, plus statistics for breaking ciphers
Classic ciphers and alphabets, for puzzles and fun: letter-shifting ciphers, Morse code, Braille and the NATO alphabet. It also has the statistics codebreakers use on an unknown text: how often each symbol appears, how uneven that is, and where patterns repeat, which reveal the kind of cipher and the length of its key.
good to know
Section titled “good to know”None of these ciphers is secure, and all can be broken in seconds. caesar, rot13, atbash and vigenere change only letters, and leave spaces, digits and punctuation as they are.
The statistics take a string, a list of byte values or bytes, and treat every symbol as different, so A and a count apart and so do spaces. Clean the text first (with upper or grep) when that isn’t what you want. Their reference values are for English and about 26 letters, so they say less about other kinds of data.
Tags: crypto
caesarthe Caesar cipher: move each letter shift places along the alphabet, wrapping from z back to arot13move each letter 13 places along the alphabetatbashswap each letter with its mirror in the alphabet: a with z, b with y, and so onvigenerethe Vigenère cipher: a Caesar shift that changes with each letter, taken in turn from the letters of the key (a shifts by 0, b by 1, …)unvigeneredecrypt text encrypted with vigenere and the same keycaesar_solveevery Caesar shift of s, the most English-looking first, as a table of shift and textxor_solvefind the repeating key that bytes were XORed with, assuming the plain text is English, and return {key, text}substitution_solvefind the key of a simple substitution cipher (each letter always swapped for the same other letter) by hill climbing, assuming English, and return {key, text}affinethe affine cipher: letter number x (a = 0) becomes a × x + b, wrapping around the alphabetunaffinedecrypt text encrypted with affine and the same a and brail_fencethe rail fence cipher: write s in a zigzag down and up n rails, then read it off one rail at a timeunrail_fencedecrypt text encrypted with rail_fence and the same number of railsbaconBacon’s cipher: each letter as five A’s and B’s, its place in the alphabet in binary (a = AAAAA, b = AAAAB, …)unbacondecode Bacon’s cipher: the A’s and B’s in s, in groups of five, back to lettersmorsetext to Morse code, or Morse code back to textbrailletext to Braille characters, one per letter (uncontracted Grade 1), or Braille back to textfreqhow often each symbol appears in s, as a table of symbol, count and percent, the most common firstngramsevery run of n symbols in s with how often it appears, as a table of gram and count, the most common firstiocthe index of coincidence of s: the chance that two symbols picked from it at random are the samechi_squaredhow far the letters of s are from English letter frequencies, as a chi-squared value, where lower is closerfriedmanthe length of a repeating key, estimated from the index of coincidence by Friedman’s formulakey_lengthseach key length up to max with the average index of coincidence of the columns it splits s into, as a table of length and ioc, the highest firstkasiskithe Kasiski examination of s: runs of n symbols that appear more than once, and the key lengths their distances point to, as {repeats, factors}vigenere_solvefind the key of a Vigenère cipher by finding its length, then solving each Caesar shift by letter frequencies, assuming English, and return {key, text}analyzea quick look at the statistics of s, as a map of length, symbols, entropy, ioc, chi_squared, top and likelynatospell text out in the NATO phonetic alphabet (Alfa, Bravo, Charlie, …), as used over the phone or radio
Functions
Section titled “Functions”caesar
Section titled “caesar”caesar(s: str, shift: int) -> str
the Caesar cipher: move each letter shift places along the alphabet, wrapping from z back to a
A negative shift moves back, undoing it
1"hello".caesar(3)"khoor"2"khoor".caesar(-3)"hello"rot13(s: str) -> str
move each letter 13 places along the alphabet
Since that’s half of 26, doing it again gives back the original
1"hello".rot13"uryyb"See also caesar
atbash
Section titled “atbash”atbash(s: str) -> str
swap each letter with its mirror in the alphabet: a with z, b with y, and so on
1"wizard".atbash"draziw"See also caesar
vigenere
Section titled “vigenere”vigenere(s: str, key: str) -> str
the Vigenère cipher: a Caesar shift that changes with each letter, taken in turn from the letters of the key (a shifts by 0, b by 1, …)
1"attack at dawn".vigenere("lemon")"lxfopv ef rnhr"See also unvigenere, caesar
unvigenere
Section titled “unvigenere”unvigenere(s: str, key: str) -> str
decrypt text encrypted with vigenere and the same key
1"lxfopv ef rnhr".unvigenere("lemon")"attack at dawn"See also vigenere
caesar_solve
Section titled “caesar_solve”caesar_solve(s: str) -> table
every Caesar shift of s, the most English-looking first, as a table of shift and text
shift is the one that encrypted it, so each text is s.caesar(-shift)
1"Wkh hdjoh odqgv".caesar_solve[0]{shift: 3, text: "The eagle lands"}xor_solve
Section titled “xor_solve”xor_solve(b: bytes) -> map
find the repeating key that bytes were XORed with, assuming the plain text is English, and return {key, text}
Guesses the key length from the Hamming distance between blocks, then each key byte from letter frequencies. text is a string when it’s valid UTF-8, else bytes. Needs a few lines of text per key byte to be reliable
1xor_solve(xor(b"Burning 'em, if you ain't quick and nimble", "ICE")){key: b"ICE", text: "Burning 'em, if you ain't quick and nimble"}See also xor, hamming, caesar_solve
substitution_solve
Section titled “substitution_solve”substitution_solve(s: str) -> map
find the key of a simple substitution cipher (each letter always swapped for the same other letter) by hill climbing, assuming English, and return {key, text}
key is the 26 letters that a to z of the cipher text stand for. Needs a few sentences of text to be reliable, and may leave a rare letter or two wrong
1"Gur dhvpx oebja sbk whzcf bire gur ynml qbt naq gura eha njnl sebz gur sneznre".substitution_solve.text"Ica jugkq bdorn tof xulzy owad ica veps hom enh ican dun eres tdol ica tedlead"See also caesar_solve, xor_solve
affine
Section titled “affine”affine(s: str, a: int, b: int) -> str
the affine cipher: letter number x (a = 0) becomes a × x + b, wrapping around the alphabet
a must share no factor with 26, so that two letters never land on the same one
1"affine cipher".affine(5, 8)"ihhwvc swfrcp"unaffine
Section titled “unaffine”unaffine(s: str, a: int, b: int) -> str
decrypt text encrypted with affine and the same a and b
1"ihhwvc swfrcp".unaffine(5, 8)"affine cipher"See also affine
rail_fence
Section titled “rail_fence”rail_fence(s: str, n: int) -> str
the rail fence cipher: write s in a zigzag down and up n rails, then read it off one rail at a time
1"we are discovered".rail_fence(3)"wrivdeaedsoee cr"See also unrail_fence
unrail_fence
Section titled “unrail_fence”unrail_fence(s: str, n: int) -> str
decrypt text encrypted with rail_fence and the same number of rails
1"wrivdeaedsoeec r".unrail_fence(3)"wdeeraceid svore"See also rail_fence
bacon(s: str) -> str
Bacon’s cipher: each letter as five A’s and B’s, its place in the alphabet in binary (a = AAAAA, b = AAAAB, …)
Anything that isn’t a letter is dropped
1"hi".bacon"AABBB ABAAA"unbacon
Section titled “unbacon”unbacon(s: str) -> str
decode Bacon’s cipher: the A’s and B’s in s, in groups of five, back to letters
Upper and lower case count the same and other characters are skipped. A group past z decodes as ?
1"AABBB ABAAA".unbacon"hi"See also bacon
morse(s: str) -> str
text to Morse code, or Morse code back to text
Letters are separated by spaces and words by /
1"sos".morse"... --- ..."2".... .. / - .... . .-. .".morse"HI THERE"See also braille
braille
Section titled “braille”braille(s: str) -> str
text to Braille characters, one per letter (uncontracted Grade 1), or Braille back to text
1"hello world".braille"⠓⠑⠇⠇⠕⠀⠺⠕⠗⠇⠙"2"⠓⠊".braille"hi"freq(s: str|list|bytes) -> table
how often each symbol appears in s, as a table of symbol, count and percent, the most common first
Every symbol counts as it is, so A and a are different. s can be a string, a list of byte values or bytes
1"hello world".freqtable([{symbol: "l", count: 3, percent: 27.2727}, {symbol: "o", count: 2, percent: 18.1818}, {symbol: "h", count: 1, percent: 9.09091}, {symbol: "e", count: 1, percent: 9.09091}, {symbol: " ", count: 1, percent: 9.09091}, {symbol: "w", count: 1, percent: 9.09091}, {symbol: "r", count: 1, percent: 9.09091}, {symbol: "d", count: 1, percent: 9.09091}])ngrams
Section titled “ngrams”ngrams(s: str|list|bytes, n?: int) -> table
every run of n symbols in s with how often it appears, as a table of gram and count, the most common first
n is 2 by default. Runs overlap, so “aaa” has two of “aa”
1"the then there".ngrams(3)table([{gram: "the", count: 3}, {gram: " th", count: 2}, {gram: "he ", count: 1}, {gram: "e t", count: 1}, {gram: "hen", count: 1}, {gram: "en ", count: 1}, {gram: "n t", count: 1}, {gram: "her", count: 1}, {gram: "ere", count: 1}])ioc(s: str|list|bytes) -> float
the index of coincidence of s: the chance that two symbols picked from it at random are the same
English text and any cipher that only swaps letters for others score near 0.066, and text spread evenly over 26 letters near 0.038. Ciphers that use several alphabets, like Vigenère, fall in between
1"the quick brown fox jumps over the lazy dog".ioc0.0454042See also friedman, chi_squared, key_lengths
chi_squared
Section titled “chi_squared”chi_squared(s: str|list|bytes, reference?: map) -> float
how far the letters of s are from English letter frequencies, as a chi-squared value, where lower is closer
With no reference only the letters a to z are scored, without regard to case. Otherwise reference maps each symbol to how often it should appear, in any units, and symbols it doesn’t list are ignored
1"the quick brown fox".chi_squared122.2182"abab".chi_squared({a: 1, b: 1})0See also ioc, freq, caesar_solve
friedman
Section titled “friedman”friedman(s: str|list|bytes) -> float
the length of a repeating key, estimated from the index of coincidence by Friedman’s formula
Not rounded, since it is only a rough guess that works best on long texts of 26 letters. Check it against kasiski and key_lengths
1"lxfopvefrnhrlxfopvefrnhrlxfopvefrnhr".friedman0.60436See also key_lengths, kasiski, ioc
key_lengths
Section titled “key_lengths”key_lengths(s: str|list|bytes, max?: int) -> table
each key length up to max with the average index of coincidence of the columns it splits s into, as a table of length and ioc, the highest first
max is 20 by default. If the key had that length, each column was shifted by one letter, so its ioc is near 0.066 for English. Multiples of the real length score about as high, so prefer the shortest of the top ones
1"lxfopvefrnhrlxfopvefrnhrlxfopvefrnhr".key_lengths(8)[0]{length: 6, ioc: 0.4}See also friedman, kasiski, vigenere_solve
kasiski
Section titled “kasiski”kasiski(s: str|list|bytes, n?: int, max?: int) -> map
the Kasiski examination of s: runs of n symbols that appear more than once, and the key lengths their distances point to, as {repeats, factors}
n is 3 by default. repeats is a table of gram, positions (counting from 0) and gaps between them. In a Vigenère cipher, a repeat usually comes from the same word meeting the same part of the key, so the gaps are multiples of the key length. factors is a table of length and count, the number of gaps each length up to max (20 by default) divides, the highest first
1"abcabcabc".kasiski{repeats: table([{gram: "abc", positions: [0, 3, 6], gaps: [3, 3]}, {gram: "bca", positions: [1, 4], gaps: [3]}, {gram: "cab", positions: [2, 5], gaps: [3]}]), factors: table([{length: 3, count: 4}])}See also key_lengths, friedman, ngrams
vigenere_solve
Section titled “vigenere_solve”vigenere_solve(s: str, max?: int) -> map
find the key of a Vigenère cipher by finding its length, then solving each Caesar shift by letter frequencies, assuming English, and return {key, text}
max is the longest key to try, 20 by default, but never more than a twentieth of the letters, since fewer per key letter are too few to tell. Only letters are used
1"lxfopvefrnhrlxfopvefrnhrlxfopvefrnhrlxfopvefrnhr".vigenere_solve{key: "ej", text: "hobflmawnedihobflmawnedihobflmawnedihobflmawnedi"}See also vigenere, unvigenere, key_lengths, caesar_solve
analyze
Section titled “analyze”analyze(s: str|list|bytes) -> map
a quick look at the statistics of s, as a map of length, symbols, entropy, ioc, chi_squared, top and likely
symbols is how many different ones there are, top is the five most common, and chi_squared is nil without letters. likely is only a hint at what s could be, judged by its ioc and entropy, so check it with the other statistics
1"Wkh hdjoh odqgv dw plgqljkw".analyze{length: 27, symbols: 13, entropy: 3.58786, ioc: 0.0541311, chi_squared: 352.948, top: table([{symbol: " ", count: 4}, {symbol: "h", count: 3}, {symbol: "d", count: 3}, {symbol: "k", count: 2}, {symbol: "j", count: 2}]), likely: "flatter than English, perhaps a cipher with several alphabets such as Vigenère"}See also freq, ioc, chi_squared, entropy
nato(s: str) -> str
spell text out in the NATO phonetic alphabet (Alfa, Bravo, Charlie, …), as used over the phone or radio
1"SOS".nato"Sierra Oscar Sierra"2"b2b".nato"Bravo Two Bravo"See also morse
More examples
Section titled “More examples”ciphers
Section titled “ciphers”1# SOS in Morse2"sos".morse"... --- ..."3# and back4"... --- ...".morse"SOS"5# hide a spoiler6"the butler did it".rot13"gur ohgyre qvq vg"7# crack a Caesar cipher by trying every shift8(1..26).map(|k| "Wkh hdjoh odqgv".caesar(-k)).grep("eagle")["The eagle lands"]9# or let it rank the shifts10"Wkh hdjoh odqgv".caesar_solve[0].text"The eagle lands"11# crack any letter-for-letter substitution12"Gur dhvpx oebja sbk whzcf bire gur ynml qbt naq gura eha njnl sebz gur sneznre".substitution_solve.text"Ica jugkq bdorn tof xulzy owad ica veps hom enh ican dun eres tdol ica tedlead"13# encrypt with Vigenère and back14"attack at dawn".vigenere("lemon").unvigenere("lemon")"attack at dawn"15# break Vigenère without the key16t = "it was the best of times and it was the worst of times and the people of the city knew that the winter would be long and that the war would not end soon but they gathered in the square every evening to share what little bread they had and to tell stories of the days before"; t.vigenere("lemon").vigenere_solve{key: "lemon", text: "it was the best of times and it was the worst of times and the people of the city knew that the winter would be long and that the war would not end soon but they gathered in the square every evening to share what little bread they had and to tell stories of the days before"}17# how often each letter appears18"hello world".freqtable([{symbol: "l", count: 3, percent: 27.2727}, {symbol: "o", count: 2, percent: 18.1818}, {symbol: "h", count: 1, percent: 9.09091}, {symbol: "e", count: 1, percent: 9.09091}, {symbol: " ", count: 1, percent: 9.09091}, {symbol: "w", count: 1, percent: 9.09091}, {symbol: "r", count: 1, percent: 9.09091}, {symbol: "d", count: 1, percent: 9.09091}])19# size up an unknown text20"Wkh hdjoh odqgv dw plgqljkw".analyze{length: 27, symbols: 13, entropy: 3.58786, ioc: 0.0541311, chi_squared: 352.948, top: table([{symbol: " ", count: 4}, {symbol: "h", count: 3}, {symbol: "d", count: 3}, {symbol: "k", count: 2}, {symbol: "j", count: 2}]), likely: "flatter than English, perhaps a cipher with several alphabets such as Vigenère"}21# in Braille22"hello".braille"⠓⠑⠇⠇⠕"23# spell it over the phone24"zil 2".nato"Zulu India Lima / Two"Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.