Chaocipher Card Simulation

The history of Chaocipher is so well covered elsewhere, I won't bother with the details here. Instead I'll concentrate on an implementation of a playing card based simulation [1] of the cipher. That is, taking the manual technique created by Aaron Toponce and executing a Perl based model.

The Code

File performs the high level details of the encrypting and decrypting process. A Deck of "cards" is created and split into two piles (red and black/left and right). As we'll see, the cards are keyed to the letters of the alphabet. Encrypting any single letter of the plaintext is done by finding the position of the letter in the plaintext pile and then providing the value of the card in the same position in the cypher text pile. The two piles are both them permuted according to straightforward rules. Decryption is virtually the same, except first the cypher text pile is searched and the corresponding index used for the plaintext pile. - abbreviated view - abbreviated view

In the snippet of above you can see the permuting process. In both cases we are essentially cutting the piles at the index of the plaintext/cyphertext, swapping the halves, and then re-assigning the cards at index 0 and 13 (the so called zenith and nadir.) [1] describes the process in slightly more detail.

The Deck itself is a blessed array reference of Card objects. Cards are based on Class::Enum. The reason for this has more to do with the design of some older code I had written for a game simulation than for the purposes of Choacipher, but it ends up working well here too, I think. is a really a wrapper around the information contained in a card: Suit, Rank, and the letter assigned to it for use in the cipher.

The usefulness of having enumerated ranks is not necessary for Chaocipher but for simplicity's sake this code was left in place. uses the ordinal property of the enum to red/black divide the suits.

Putting it all together we can see below what happens when we encrypt the message ATTACKATDAWN and the decrypt the encrypted message. It works!

Sample Run

$ perl -Iperl5 perl5/


All the details of this playing card technique can be found in [1]. Aaron Toponce should be congratulated for this extremely clever playing card cipher system! Simulating the use of this card technique, rather than the cipher directly, provides greater insight into the playing card method as well as the original cipher process itself.



Comments for this post were locked by the author