Today I thought we could talk a little bit about Columnar Transposition Ciphers (hereby called CTC for short), let’s start with discussing what a CTC is.

A CTC is a simple encryption method using a shared key between the participants, where the characters in a clear text message gets shifted around in a specific predetermined way in a matrix.

Let’s say that Alice wants to send Bob a message about a secret meeting place. They’ve previously decided on a shared key, QUIET. She then writes her message “meet me at the cafe on main street tomorrow at noon”. Next step is to create the matrix based on their key.

QUIET
meetm
eatth
ecafe
onmai
nstre
ettom
orrow
atnoo
nspeq

The letters at the end are randomized to to pad the matrix. Next Alice write out the columns in lexicographical order based on the key. With this key would be in the order of E, I, Q, T, and U. The encrypted message would then look like this: ttfaroooe etamttrnp meeoneoan mheiemwoq eacnstrts

Alice then sends this encrypted message to Bob who reconstructs the matrix, using the same key, by filling in the columns in the key’s lexicographical order. After the columns have been filled in, the matrix will look like the example above and Bob can read the message in clear text line by line.

Code example

The following example is written in C# but should be pretty easy to rewrite into any other object oriented language.

The code consists of three classes, Encipher, Decipher, and an abstract class Cipher that they inherit common and helper methods from.

C#

Let’s look at what the code in the Encipher class does.

C#

The constructor takes two arguments, the clear text message and the key we will use to encrypt. We calculate the number of rows the grid will have based on the key length. The modulus part is to make sure that we add an extra row if the characters can’t be divided equally.

C#

The FillGrid method loops over the rows and columns and places the characters into the matrix. We also want to make sure we only use upper or lowercase letters, in this case I chose to go with uppercase.

C#

The DoCipher method sorts the key in lexicographical order and then goes through the sorted key and concatenates the characters in the corresponding column to our cipheredText variable.

C#

Now let’s look at the Decipher class.

C#

The constructor in the Decipher class also takes two parameters, the encrypted string and the key. We start with checking if the message can be divided equally by the key, if not the key has the incorrect length.

C#

When we fill in the grid in the Decipher class we first sort the key in lexicographical order and then fill in the encrypted message into the matrix column by column in the sorted key’s order.

C#

To decipher the message we then simply read the clear text message line by line.

C#

The Cipher class is pretty straight forward but we’ll take a look at a few things.

C#

The IsKeyValid method checks that each character in the key is unique, is alphanumerical and at least two characters long.

C#

The GetChar method simply return a character at a specific position in the message, or if the position is outside the scope of the message (e.g. when we fill in a clear text message that can’t be divided equally with the key) we return a random letter instead.

Using the code

To use the code in this example you can simply call the code something like this.

C#

Using the example with Alice and Bob we get this output.

 Type the text to encode: meetmeatthecafeonmainstreettomorrowatnoon
 Type the key to use: quiet
 +---------+
 |Q|U|I|E|T|
 +---------+
 |M|E|E|T|M|
 |E|A|T|T|H|
 |E|C|A|F|E|
 |O|N|M|A|I|
 |N|S|T|R|E|
 |E|T|T|O|M|
 |O|R|R|O|W|
 |A|T|N|O|O|
 |N|H|M|P|C|
 +---------+
 TTFAROOOPETAMTTRNMMEEONEOANMHEIEMWOCEACNSTRTH

 +---------+
 |Q|U|I|E|T|
 +---------+
 |M|E|E|T|M|
 |E|A|T|T|H|
 |E|C|A|F|E|
 |O|N|M|A|I|
 |N|S|T|R|E|
 |E|T|T|O|M|
 |O|R|R|O|W|
 |A|T|N|O|O|
 |N|H|M|P|C|
 +---------+
 MEETMEATTHECAFEONMAINSTREETTOMORROWATNOONHMPC

Conclusion

CTC is one of my favorite ciphers because it’s so simple to use even when using pen and paper. It should not however be considered secure by any stretch of the imagination. If you decide to use this cipher you should combine it with another kind of cipher, even combining with a simple substitution cipher makes this more difficult to decrypt.

There’s one flaw, or feature depending on how you look at it, with this method. Both people doesn’t have to use the same key, the important thing is that the columns are sorted the same. Let’s say Alice uses a simple three letter key like JOB. If we look at the order we will use the key it will be B=1, J=2, and O=3. If Bob uses another key with the same 231 pattern, e.g. SPA, he can decipher the message even though he doesn’t use the same key.

You can download the demo code from github.


Featured image by Pete Linforth from Pixabay

Published by Jimmie

Jimmie is a full stack developer with over 13 years working experience in building web based solutions and over 25 years of programming experience over all. Jimmie has worked with a slew programming languages over the years, but his main focus is usually on web frameworks such as Symfony,  Angular, and Spring Boot.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: