Skip to main content
Basic Classic Symmetric Beginner

The Columnar Transposition Cipher

Learn about the columnar transposition cipher, a classic cipher that scrambles the order of letters instead of replacing them. Learn why combining it with substitution led to some of the toughest hand ciphers ever used.

PL
Pashalis Laoutaris
August 21, 2026
12 min read

Interactive Columnar Transposition Visualizer

🔐 Columnar Transposition Cipher Visualizer

5
Enter text and click a button to start!
WEAREDISCOVEREDFLEEATONCE

The Columnar Transposition Cipher: Same Letters, Different Order

Introduction

Every cipher covered so far in this series, Caesar, Atbash, Affine, and monoalphabetic substitution, belongs to the same family: substitution ciphers, which replace each letter with a different one. The columnar transposition cipher does something completely different. It doesn’t touch the letters at all. It only rearranges their order, writing the message into a grid and reading it back out in a different sequence determined by a keyword.

Table of Contents

How the Columnar Transposition Cipher Works

The columnar transposition cipher works in two stages:

  1. Write the plaintext into a grid, row by row, with as many columns as there are letters in the keyword.
  2. Read the grid back out column by column, but not left to right. Instead, columns are read in the order determined by sorting the keyword’s letters alphabetically.

Nothing about any individual letter changes; only where it ends up in the final message changes. This is the defining trait of a transposition cipher, as opposed to the substitution ciphers covered earlier in this series. As with every cipher in this series, spaces and punctuation are conventionally stripped out before the message is written into the grid. The worked example below uses a continuous letter string for exactly this reason.

Interactive Visualizer

Try it above. Enter a keyword to see its columns numbered by reading order, then encrypt or decrypt text and watch the grid fill and drain in real time.

Determining Column Order from a Keyword

Each letter of the keyword gets a rank, based on its alphabetical position relative to the other letters in the keyword (ties between repeated letters are broken left to right). That rank becomes the order in which the column beneath that letter gets read.

Example with keyword “ZEBRA”:

Column:   1   2   3   4   5
Letter:   Z   E   B   R   A
Rank:     5   3   2   4   1

A is the earliest letter alphabetically, so column 5 (under A) is read first; B is next, so column 3 is read second; and so on, ending with Z’s column (column 1) read last.

“ZEBRA” happens to have no repeated letters, so the tie-breaking rule never actually comes into play there. A keyword like “LEVEL” shows it in action:

Column:   1   2   3   4   5
Letter:   L   E   V   E   L
Rank:     3   1   5   2   4

Both E’s are tied alphabetically, but the leftmost one (column 2) gets the earlier rank (1) and the rightmost one (column 4) gets the later rank (2). The same left-to-right tie-break applies to the two L’s, giving column 1 rank 3 and column 5 rank 4.

A Worked Example

Using keyword “ZEBRA” (5 columns) and the plaintext “WEAREDISCOVEREDFLEEATONCE” (25 letters, filling the grid exactly with 5 rows):

Z  E  B  R  A
-  -  -  -  -
W  E  A  R  E
D  I  S  C  O
V  E  R  E  D
F  L  E  E  A
T  O  N  C  E

Reading the columns in rank order (A, B, E, R, Z → columns 5, 3, 2, 4, 1):

  • Column 5 (A): E O D A E
  • Column 3 (B): A S R E N
  • Column 2 (E): E I E L O
  • Column 4 (R): R C E E C
  • Column 1 (Z): W D V F T

Concatenating: “EODAEASRENEIELORCEECWDVFT”

Decryption: Rebuilding the Grid

Decryption reverses the process precisely: knowing the keyword tells you both the grid’s width and the column reading order. That lets you split the ciphertext back into columns of the correct lengths, place them into the grid in rank order, and then read the grid back out row by row to recover the original message.

Concretely, with the 25-letter ciphertext “EODAEASRENEIELORCEECWDVFT” and a 5-column, 5-row grid, every column is exactly 5 letters long. Since column 5 (under A) was read first during encryption, the first 5 ciphertext letters, “EODAE”, go back into column 5, top to bottom. The next 5 letters, “ASREN”, go into column 3 (read second), and so on for the remaining three columns, before the grid is read back out row by row to recover the plaintext.

Handling Ragged Grids

The worked example above divides evenly (25 letters ÷ 5 columns = exactly 5 rows), but that’s not always the case. When the plaintext length isn’t a multiple of the keyword length, the last row is incomplete. That means the leftmost columns (the ones filled first while writing row by row) end up one letter longer than the rest. Both encryption and decryption need to account for this: the visualizer above computes each column’s exact length automatically, whether or not the grid divides evenly. That way partially filled grids still encrypt and decrypt correctly.

This “leave the last row short” approach is technically the irregular columnar transposition. It’s the version this article and visualizer implement throughout. A second historical variant, the regular columnar transposition, instead pads the incomplete row with filler letters (nulls, often X or Q) until the grid divides evenly. That trades a slightly longer ciphertext for a grid that’s never ragged. Both variants use identical column-ordering logic; they only differ in how they handle the leftover cells.

Pros and Cons Analysis

Advantages Disadvantages
Preserves Letter Frequencies Differently: Since letters aren’t replaced, simple single-letter frequency analysis (which breaks every substitution cipher in this series) doesn’t directly apply Anagram-Level Weakness: The ciphertext is, by definition, just an anagram of the plaintext. An attacker who suspects transposition can look for grid widths that produce readable text
A Genuinely Different Attack Surface: Requires different cryptanalysis techniques than substitution ciphers, historically forcing attackers to adapt Small Keyspace for Short Keywords: A keyword of length n has only n! possible column orderings. An 8-letter keyword has just 40,320 arrangements, trivial to brute-force
Combines Well with Substitution: Historically paired with substitution ciphers (see below) to defeat frequency analysis entirely Digraph/Trigraph Patterns Leak Structure: Even without knowing the keyword, common letter pairs and grid-width guessing narrow the search dramatically
Easy to Compute by Hand: Just a grid-filling and grid-reading exercise, no arithmetic required No Real-World Security: Modern computers solve small-to-medium transposition puzzles almost instantly

Cryptanalysis and Breaking Columnar Transposition

Because transposition ciphers don’t change letter identities, single-letter frequency analysis is useless against them on its own. The ciphertext contains exactly the same letters, in exactly the same proportions, as the plaintext. But that’s also exactly the giveaway: if a suspicious ciphertext has a letter-frequency profile that matches ordinary English (rather than the flattened profile substitution produces), an analyst can immediately suspect transposition rather than substitution.

From there, cryptanalysis typically proceeds by:

  1. Guessing the grid width (the keyword length), often narrowed down by the ciphertext’s total length and its factors.
  2. Anagramming columns: testing different column reorderings and checking whether digraphs (common two-letter sequences like TH, HE, IN) start appearing at row boundaries. Concretely, trying one candidate column order might cause “T” at the end of one row to sit directly above an “H” at the start of the next reconstructed row, forming “TH” across that boundary. If this keeps happening at several row boundaries with other common digraphs, that’s a strong signal the trial ordering is close to (or exactly) correct. Random column arrangements essentially never produce that many plausible digraphs by chance.
  3. Refining with word-pattern recognition, similar to substitution cipher attacks, once partial structure emerges.

For short keywords, this is fast enough to do by hand. For longer ones, it’s a straightforward brute-force search for any modern computer, since a keyword of length n has only n! possible orderings, far smaller than the 26! keyspace of general substitution.

Substitution vs. Transposition

These two cipher families solve completely different problems, which is exactly why historical cryptographers often combined them:

  • Substitution ciphers (Caesar, Atbash, Affine, monoalphabetic substitution) change what each letter is, but not where it sits.
  • Transposition ciphers (columnar transposition, Rail Fence) change where each letter sits, but not what it is.

Combining both, encrypting with a substitution cipher, then scrambling the result with a transposition cipher (or vice versa), defeats simple frequency analysis far more effectively than either technique alone. That’s because the substitution step changes letter identities, while the transposition step destroys the positional patterns (like repeated digraphs) an attacker would otherwise rely on. This combination principle is a direct ancestor of the confusion and diffusion design goals that underpin every modern block cipher, including AES.

Modern Relevance

Columnar transposition itself hasn’t secured anything sensitive in a very long time, but it remains valuable as:

  • A teaching tool for the crucial substitution-vs-transposition distinction, and for introducing the idea of combining primitives to cover each other’s weaknesses.
  • Genuine historical significance: variants of columnar transposition (notably the double transposition cipher, which applies the process twice with different keywords) were used by several militaries, including Germany, well into the 20th century. That’s precisely because they resist basic frequency analysis.
  • A bridge concept toward understanding why modern ciphers like AES explicitly separate their operations into substitution-like steps (SubBytes) and diffusion/permutation-like steps (ShiftRows, MixColumns) within every round.

Python Implementation

The two ideas covered above (rank the keyword’s letters, then write-by-row/read-by-column) are all this needs. get_column_order does the ranking; encryption and decryption share it:

def get_column_order(keyword):
    """Column indices sorted by keyword letter, ties broken left to right."""
    return sorted(range(len(keyword)), key=lambda i: (keyword[i], i))

def columnar_encrypt(text, keyword):
    text = ''.join(c for c in text.upper() if c.isalnum())
    keyword = ''.join(c for c in keyword.upper() if c.isalpha())
    cols = len(keyword)
    column_order = get_column_order(keyword)

    grid = ['' for _ in range(cols)]
    for i, char in enumerate(text):
        grid[i % cols] += char  # writing row by row is the same as this column-major fill

    return ''.join(grid[col] for col in column_order)

def columnar_decrypt(ciphertext, keyword):
    keyword = ''.join(c for c in keyword.upper() if c.isalpha())
    cols = len(keyword)
    rows, extra = divmod(len(ciphertext), cols)
    column_order = get_column_order(keyword)
    col_lengths = [rows + (1 if i < extra else 0) for i in range(cols)]

    columns, index = [''] * cols, 0
    for col in column_order:
        length = col_lengths[col]
        columns[col] = ciphertext[index:index + length]
        index += length

    result = ''
    for row in range(rows + 1):
        for col in range(cols):
            if row < len(columns[col]):
                result += columns[col][row]
    return result

if __name__ == "__main__":
    keyword = "ZEBRA"
    plaintext = "WEAREDISCOVEREDFLEEATONCE"

    ciphertext = columnar_encrypt(plaintext, keyword)
    recovered = columnar_decrypt(ciphertext, keyword)

    print(f"Plaintext:  {plaintext}")
    print(f"Ciphertext: {ciphertext}")
    print(f"Recovered:  {recovered}")

This reproduces the worked example above exactly: WEAREDISCOVEREDFLEEATONCEEODAEASRENEIELORCEECWDVFTWEAREDISCOVEREDFLEEATONCE. get_column_order’s (keyword[i], i) sort key is doing precisely the “rank alphabetically, break ties left to right” rule from earlier in this article.

Limitations

This implementation covers the irregular variant only, as described above:

  • No “regular” (padded) variant. As the Handling Ragged Grids section explains, this code always leaves the last row short rather than padding it with null letters; supporting the padded historical variant would need an explicit filler character and a fixed total length.
  • No keyword validation. An empty keyword makes cols zero and crashes on the modulo in columnar_encrypt; there’s no check guarding against it.
  • Digits pass through, other symbols don’t. Like the visualizer, the plaintext keeps A-Z and 0-9 but strips everything else, including spaces, so original formatting doesn’t survive a round trip.
  • Not intended for real security. As the Cryptanalysis section above shows, a keyword’s n! possible orderings are small enough to brute-force for any reasonably short keyword, no matter how carefully this code is written.

Conclusion

The columnar transposition cipher is the clearest illustration in this classic-cipher series of an entirely different strategy from substitution: instead of relabeling letters, it simply reorders them according to a keyword-derived column sequence. On its own, it’s no more secure against modern cryptanalysis than any other classic cipher. But the idea it embodies, combining substitution and transposition to cover each other’s weaknesses, became one of the foundational design principles of modern cryptography.

FAQ

What is the columnar transposition cipher?

A cipher that writes the plaintext into a grid row by row (with as many columns as the keyword has letters), then reads the grid back out column by column in an order determined by sorting the keyword’s letters alphabetically.

How is column reading order determined?

Each keyword letter is ranked by its alphabetical position relative to the other letters in the keyword (repeated letters are ranked left to right). The column under the alphabetically-first letter is read first, and so on.

What happens when the plaintext doesn’t fill the grid evenly?

The last row is left incomplete, which means the leftmost columns (filled first) end up one letter longer than the rest. Both encryption and decryption must track each column’s exact length to handle this correctly.

Is columnar transposition more secure than substitution ciphers?

Not fundamentally. It has its own weaknesses (small keyspace for short keywords, vulnerability to anagram-style analysis) that are just as breakable as frequency analysis, only requiring different techniques.

Why were substitution and transposition often combined historically?

Because they attack different aspects of the plaintext’s structure: substitution hides letter identities, while transposition hides letter positions. Combined, they defeat both frequency analysis and pattern-based attacks far more effectively than either alone. That’s a principle that carries directly into modern block cipher design.

References

  1. Wikipedia. “Transposition cipher.” Available at: https://en.wikipedia.org/wiki/Transposition_cipher

  2. Practical Cryptography. “Columnar Transposition Cipher.” Available at: http://practicalcryptography.com/ciphers/columnar-transposition-cipher/

  3. Singh, Simon. “The Code Book.” Doubleday, 1999.