Columnar Transposition Cipher

Implement a program that encrypts messages using the columnar transposition cipher.

$ ./column test
plaintext:  now is the time to act.
ciphertext: otiowhmanstttieec

Background

A transposition cipher is a method of encryption that rearranges the order of plaintext characters to create ciphertext without changing the actual letters themselves. This is achieved through a predetermined pattern or a “key,” such as writing the message in a zigzag or a grid, and then reading it out in a different order. A columnar transposition cipher uses a keyword to determine the order of columns in a grid. Unlike substitution ciphers, transposition ciphers preserve the frequency of letters in the ciphertext, which can be a weakness for cryptanalysts.

Columnar transposition involves writing the plaintext out in rows, and then reading the ciphertext off in columns. The key determines the number of columns and the order in which they are read. The plaintext is written row by row into a grid, and then the columns are reordered alphabetically by the keyword letters before being read out.

For example, given a key of hello and a message of This is a secret message. you would construct a grid like this:

H E L L O
T h i s i
s a s e c
r e t m e
s s a g e

The columns will then be read out in order based on the letters of the key: 1, 0, 2, 3, 4 resulting in the ciphertext:

haesTsrsistasemgicee

Your task

Write a program called column that enables you to encrypt messages using a columnar transposition cipher. At the time the user executes the program, they should decide, by providing a command-line argument, on what the key should be in the secret message they’ll provide at runtime.

Here is an example of how the program might work. For example, if the user inputs a key of hello and a plaintext of This is a test.:

$ ./column hello
plaintext:  This is a test.
ciphertext: haesTsrsistasemgicee

Notice that punctuation and spaces were ignored by the cipher. Only consider alphabetical characters! Notice, too, that the case of the original message has been preserved. Lowercase letters remain lowercase, and uppercase letters remain uppercase.

Whether the characters in the key itself are uppercase or lowercase doesn’t matter. A key of hello is functionally identical to a key of HELLO (as is, for that matter, HeLlO).

And what if a user doesn’t provide a valid key?

$ ./column h
Key must be at least 3 characters.
$ ./column he3
Key must be only alphabetic characters.

Or really doesn’t cooperate?

$ ./column
Usage: ./column key

Specification

Implement a program, column, that encrypts messages using a columnar transposition cipher.

How to Test Your Code

Execute the below to check the correctness of your code using check50. But be sure to compile and test it yourself as well!

check50 scienceacademy/problems/2025ap/column

How to Submit

Execute the below, logging in with your GitHub username and password when prompted.

submit50 scienceacademy/problems/2025ap/column