Skip to content

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.

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

  • caesar the Caesar cipher: move each letter shift places along the alphabet, wrapping from z back to a
  • rot13 move each letter 13 places along the alphabet
  • atbash swap each letter with its mirror in the alphabet: a with z, b with y, and so on
  • vigenere 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, …)
  • unvigenere decrypt text encrypted with vigenere and the same key
  • caesar_solve every Caesar shift of s, the most English-looking first, as a table of shift and text
  • xor_solve find the repeating key that bytes were XORed with, assuming the plain text is English, and return {key, text}
  • substitution_solve 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}
  • affine the affine cipher: letter number x (a = 0) becomes a × x + b, wrapping around the alphabet
  • unaffine decrypt text encrypted with affine and the same a and b
  • rail_fence the rail fence cipher: write s in a zigzag down and up n rails, then read it off one rail at a time
  • unrail_fence decrypt text encrypted with rail_fence and the same number of rails
  • bacon Bacon’s cipher: each letter as five A’s and B’s, its place in the alphabet in binary (a = AAAAA, b = AAAAB, …)
  • unbacon decode Bacon’s cipher: the A’s and B’s in s, in groups of five, back to letters
  • morse text to Morse code, or Morse code back to text
  • braille text to Braille characters, one per letter (uncontracted Grade 1), or Braille back to text
  • freq how often each symbol appears in s, as a table of symbol, count and percent, the most common first
  • ngrams every run of n symbols in s with how often it appears, as a table of gram and count, the most common first
  • ioc the index of coincidence of s: the chance that two symbols picked from it at random are the same
  • chi_squared how far the letters of s are from English letter frequencies, as a chi-squared value, where lower is closer
  • friedman the length of a repeating key, estimated from the index of coincidence by Friedman’s formula
  • key_lengths 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
  • kasiski 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}
  • vigenere_solve 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}
  • analyze a quick look at the statistics of s, as a map of length, symbols, entropy, ioc, chi_squared, top and likely
  • nato spell text out in the NATO phonetic alphabet (Alfa, Bravo, Charlie, …), as used over the phone or radio
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"
Example outputCtrl+Enter to run

See also rot13, vigenere

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"
Example outputCtrl+Enter to run

See also caesar

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"
Example outputCtrl+Enter to run

See also caesar

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"
Example outputCtrl+Enter to run

See also unvigenere, caesar

unvigenere(s: str, key: str) -> str

decrypt text encrypted with vigenere and the same key

1
"lxfopv ef rnhr".unvigenere("lemon")"attack at dawn"
Example outputCtrl+Enter to run

See also vigenere

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"}
Example outputCtrl+Enter to run

See also caesar, 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

1
xor_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"}
Example outputCtrl+Enter to run

See also xor, hamming, caesar_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"
Example outputCtrl+Enter to run

See also caesar_solve, xor_solve

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"
Example outputCtrl+Enter to run

See also unaffine, caesar

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"
Example outputCtrl+Enter to run

See also affine

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"
Example outputCtrl+Enter to run

See also 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"
Example outputCtrl+Enter to run

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"
Example outputCtrl+Enter to run

See also unbacon, morse

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"
Example outputCtrl+Enter to run

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"
Example outputCtrl+Enter to run

See also 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"
Example outputCtrl+Enter to run

See also morse, nato

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}])
Example outputCtrl+Enter to run

See also ngrams, ioc, entropy

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}])
Example outputCtrl+Enter to run

See also freq, kasiski

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.0454042
Example outputCtrl+Enter to run

See also friedman, chi_squared, key_lengths

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.218
2
"abab".chi_squared({a: 1, b: 1})0
Example outputCtrl+Enter to run

See also ioc, freq, caesar_solve

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.60436
Example outputCtrl+Enter to run

See also key_lengths, kasiski, ioc

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}
Example outputCtrl+Enter to run

See also friedman, kasiski, vigenere_solve

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}])}
Example outputCtrl+Enter to run

See also key_lengths, friedman, ngrams

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"}
Example outputCtrl+Enter to run

See also vigenere, unvigenere, key_lengths, caesar_solve

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"}
Example outputCtrl+Enter to run

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"
Example outputCtrl+Enter to run

See also morse

1
# SOS in Morse
2
"sos".morse"... --- ..."
3
# and back
4
"... --- ...".morse"SOS"
5
# hide a spoiler
6
"the butler did it".rot13"gur ohgyre qvq vg"
7
# crack a Caesar cipher by trying every shift
8
(1..26).map(|k| "Wkh hdjoh odqgv".caesar(-k)).grep("eagle")["The eagle lands"]
9
# or let it rank the shifts
10
"Wkh hdjoh odqgv".caesar_solve[0].text"The eagle lands"
11
# crack any letter-for-letter substitution
12
"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 back
14
"attack at dawn".vigenere("lemon").unvigenere("lemon")"attack at dawn"
15
# break Vigenère without the key
16
t = "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 appears
18
"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 text
20
"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 Braille
22
"hello".braille"⠓⠑⠇⠇⠕"
23
# spell it over the phone
24
"zil 2".nato"Zulu India Lima / Two"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.