The ADFGVX Cipher¶
In the spring of 1918, near the end of World War I, the German army began sending radio messages in a new cipher designed by Lieutenant Fritz Nebel. The first version, used from March 1918, was called ADFGX. On 1 June 1918 it was replaced by a bigger version, ADFGVX. The names come from the only letters that ever appear in the ciphertext. They were chosen because they sound very different from one another in Morse code, so radio operators were less likely to mix them up.
ADFGVX isn’t a block cipher, but it shows three of the ideas from the introduction to block ciphers:
Confusion. Each plaintext character is replaced using a secret table.
Fractionation. Each plaintext character becomes two ciphertext symbols, and a transposition then pulls the two symbols apart.
Diffusion. Because the two symbols end up in different parts of the ciphertext, each plaintext character affects more than one place in the message.
Encryption has two stages: a substitution with a Polybius square, then a columnar transposition with a keyword. The key is the square and the keyword together.
The sections below go through each stage.
Stage 1: The Polybius Square¶
A Polybius square is a grid with a letter label on each row and each column. ADFGVX uses a 6×6 square labelled A D F G V X, which has room for the 26 letters and the 10 digits. (ADFGX used a 5×5 square with I and J sharing a cell, so it had no digits.) The arrangement of the 36 symbols is part of the secret key.
Here is the square for an encryption example.
| A | D | F | G | V | X | |
|---|---|---|---|---|---|---|
| A | P | 0 | 4 | 2 | R | 9 |
| D | C | U | W | 5 | A | E |
| F | O | H | 3 | L | Q | M |
| G | N | 7 | S | B | Y | G |
| V | T | I | J | 1 | V | Z |
| X | 6 | K | X | 8 | F | D |
To substitute a character, find it in the square and write its row label, then its column label. For example, M is in row F, column X, so it becomes FX. The 4 is in row A, column F, so it becomes AF.
| Plaintext | M | A | T | H | 4 | L | I | F | E |
|---|---|---|---|---|---|---|---|---|---|
| Substitution | FX | DV | VA | FD | AF | FG | VD | XV | DX |
So MATH4LIFE becomes FXDVVAFDAFFGVDXVDX, which is twice as long as the plaintext.
In Python, store the square as one string of 36 characters, read row by row. A character at position p in the string sits in row p // 6 and column p % 6.
LABELS = 'ADFGVX'
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
# The encryption example's square, row by row
SQUARE = 'P042R9' + 'CUW5AE' + 'OH3LQM' + 'N7SBYG' + 'TIJ1VZ' + '6KX8FD'
def print_square(square):
print(' ', ' '.join(LABELS))
for row in range(6):
print(LABELS[row], ' ', ' '.join(square[6 * row: 6 * row + 6]))
def check_square(square):
# A valid square holds each of the 36 symbols exactly once
return sorted(square) == sorted(SYMBOLS)
print_square(SQUARE)
print('valid square:', check_square(SQUARE)) A D F G V X
A P 0 4 2 R 9
D C U W 5 A E
F O H 3 L Q M
G N 7 S B Y G
V T I J 1 V Z
X 6 K X 8 F D
valid square: True
def polybius_encode(text, square):
# Replace each character with its row label, then its column label
text = text_clean(text, SYMBOLS)
result = ''
for char in text:
position = square.find(char)
result += LABELS[position // 6] + LABELS[position % 6]
return result
def polybius_decode(text, square):
# Read the symbols in pairs: the first gives the row, the second gives the column
result = ''
for i in range(0, len(text), 2):
row = LABELS.find(text[i])
column = LABELS.find(text[i + 1])
result += square[6 * row + column]
return result
polybius_encode('MATH4LIFE', SQUARE)'FXDVVAFDAFFGVDXVDX'Stage 2: Columnar Transposition¶
The second stage is a keyed columnar transposition, covered in detail on the columnar transposition page in Chapter 2. In short: write the symbols from stage 1 into a grid under the keyword, one row at a time (the last row may be short), then read the columns from top to bottom in alphabetical order of the keyword’s letters.
The example uses the keyword TIME. The grid has 4 full rows and a short last row. Reading the columns in the order E, I, M, T gives VDGV, XAFDX, DFFX and FVAVD, so the ciphertext is VDGVXAFDXDFFXFVAVD.
The functions below do the transposition in Python. They assume the keyword has no repeated letters, like all of the keywords on this page.
def print_grid(text, keyword):
# Show the text written in rows under the keyword
print(' '.join(keyword))
for i in range(0, len(text), len(keyword)):
print(' '.join(text[i:i + len(keyword)]))
def columnar_transpose(text, keyword):
# Column i holds every len(keyword)-th symbol, starting at position i
columns = [''] * len(keyword)
for i in range(len(text)):
columns[i % len(keyword)] += text[i]
# Read the columns in alphabetical order of the keyword's letters
result = ''
for letter in sorted(keyword):
result += columns[keyword.find(letter)]
return result
def columnar_untranspose(text, keyword):
full_rows = len(text) // len(keyword)
extra = len(text) % len(keyword) # this many columns, from the left, have one extra symbol
columns = [''] * len(keyword)
position = 0
for letter in sorted(keyword):
i = keyword.find(letter)
length = full_rows + 1 if i < extra else full_rows
columns[i] = text[position:position + length]
position += length
# Read across the rows
result = ''
for row in range(full_rows + 1):
for column in columns:
if row < len(column):
result += column[row]
return result
print_grid('FXDVVAFDAFFGVDXVDX', 'TIME')
print()
print(columnar_transpose('FXDVVAFDAFFGVDXVDX', 'TIME'))T I M E
F X D V
V A F D
A F F G
V D X V
D X
VDGVXAFDXDFFXFVAVD
Putting both stages together gives the whole cipher. Like the other encryption functions in this book, it returns uppercase ciphertext in blocks of 5, and decryption returns lowercase plaintext.
def adfgvx(text, square, keyword, decrypt=False):
keyword = keyword.upper()
if decrypt:
text = text_clean(text, LABELS)
return polybius_decode(columnar_untranspose(text, keyword), square).lower()
return text_block(columnar_transpose(polybius_encode(text, square), keyword))
adfgvx('MATH4LIFE', SQUARE, 'TIME')'VDGVX AFDXD FFXFV AVD'Decryption¶
To decrypt, undo the two stages in reverse order. First undo the transposition, which means working out how long each column is (see deciphering a columnar transposition). Then look up each pair of symbols in the square, row label first.
This decryption example uses the ciphertext DGDDDAGDDGAFADDFDADVDVFAADVX, the keyword PRIVACY, and the square below.
| A | D | F | G | V | X | |
|---|---|---|---|---|---|---|
| A | N | A | 1 | C | 3 | H |
| D | 8 | T | B | 2 | O | M |
| F | E | 5 | W | R | P | D |
| G | 4 | F | 6 | G | 7 | I |
| V | 9 | J | 0 | K | L | Q |
| X | S | U | V | X | Y | Z |
The ciphertext has 28 symbols and the keyword has 7 letters. Since , every column has 4 symbols.
Undoing the transposition gives
ADDDDDADAGVGADDDAFDGVFVFADDX.Look up the pairs in the square:
ADisA,DDisT,DDisT,ADisA, and so on.
The message is ATTACKAT1200AM, “attack at 12:00 am.”
# The decryption example's square, row by row
SQUARE2 = 'NA1C3H' + '8TB2OM' + 'E5WRPD' + '4F6G7I' + '9J0KLQ' + 'SUVXYZ'
print('valid square:', check_square(SQUARE2))
print(columnar_untranspose('DGDDDAGDDGAFADDFDADVDVFAADVX', 'PRIVACY'))
print(adfgvx('DGDDDAGDDGAFADDFDADVDVFAADVX', SQUARE2, 'PRIVACY', decrypt=True))valid square: True
ADDDDDADAGVGADDDAFDGVFVFADDX
attackat1200am
The TIME example has a short last row: , so the columns under T and I, the first two letters of the keyword, have 5 symbols, and the columns under M and E have 4. columnar_untranspose handles that, so the whole cipher decrypts it too.
adfgvx('VDGVX AFDXD FFXFV AVD', SQUARE, 'TIME', decrypt=True)'math4life'Fractionation and Diffusion¶
Change one character of the plaintext, MATH4LIFE to MATH5LIFE, and compare the two ciphertexts.
ciphertext1 = columnar_transpose(polybius_encode('MATH4LIFE', SQUARE), 'TIME')
ciphertext2 = columnar_transpose(polybius_encode('MATH5LIFE', SQUARE), 'TIME')
print(ciphertext1)
print(ciphertext2)
changed = [i + 1 for i in range(len(ciphertext1)) if ciphertext1[i] != ciphertext2[i]]
print('changed positions:', changed, 'out of', len(ciphertext1))VDGVXAFDXDFFXFVAVD
VDGVXAGDXDFFXFVDVD
changed positions: [7, 16] out of 18
The 4 became AF and the 5 became DG. Both of those symbols changed, but the transposition sent them to positions 7 and 16, far apart. One plaintext change still affects only 2 of the 18 ciphertext symbols. That’s far short of the block cipher goal of changing about half, but it’s more than any stream cipher does.
The bigger win is against frequency analysis. Every cipher before this one kept a simple link: one plaintext letter in, one ciphertext letter out, in a predictable place. Counting ciphertext letters was really counting plaintext letters in disguise. ADFGVX breaks that link in two ways.
The ciphertext only ever contains six symbols, so counting them tells you very little.
Each plaintext character is split into two symbols, and the transposition puts them in different columns. No single ciphertext position stands for a single plaintext character.
The order of the two stages matters. If you transposed first and substituted second, you would just be shuffling whole characters and then substituting them consistently, and frequency analysis would still work. The Bifid cipher also fractionates: it splits each letter into its row and column numbers in a square and mixes them before turning them back into letters.
Breaking ADFGVX¶
ADFGVX was attacked by Lieutenant Georges Painvin, a cryptanalyst in the French army. He broke ADFGX messages in the spring of 1918, and in early June 1918 he solved an ADFGVX message that showed where the German army planned to attack next, during General Erich Ludendorff’s spring offensive. Historians still debate how much this helped the French hold off the attack.
Painvin’s methods needed a lot of traffic. They relied on finding several messages sent with the same key that began in the same, predictable way (stereotyped beginnings), and on patterns in the ciphertext positions that had come from the same column of the transposition. So they worked only on days with heavy radio traffic. Those were also the days when the most important messages were sent.
Questions to Think About¶
Try making your own square and keyword, encrypting a short message, and decrypting it again. As you work, think about these questions.
Solution to Exercise 1
There are 36 choices for the first cell, 35 for the second, and so on, so there are squares.
Solution to Exercise 2
A longer keyword makes more columns, and each column is shorter. With a one-letter keyword, the grid has a single column, and reading it from top to bottom gives back the symbols in their original order, so the transposition does nothing. With a keyword longer than the message, the grid has one short row, so each column holds at most one symbol. The transposition then just reorders the symbols by the alphabetical order of the keyword letters above them.