If we include the two jokers, there are 54! ways to arrange a deck of playing cards. If we use an alphabet of 27 distinct characters (26 letters, plus a space), then we can, in theory, encode in the arrangement of the deck a message of up to 49 characters, since 2749<54!<2750. But how are we to convert a message to a deck? The task is not straightforward, because of the difference in the nature of the two media: in text, every character is a choice from 27 options; in a deck of cards, the decision of which card should go next is first a choice from 54 options, then from 53, and so on.

I will describe the algorithm I devised to do this, and then explain why it works.

• The algorithms
• Encoding
• Decoding
• Examples
• Encoding
• Decoding
• Why does it work?
• Practical use

### The algorithms

#### Encoding

First, you and the person to whom you intend to send the message must agree on a key. The key is simply a random arrangement of the deck, which can be created by shuffling it. The key can be communicated either by giving your partner a list of all 54 cards in the correct order, or the actual deck itself. Either way, both of you need to know the key in order to communicate (so don't lose it!).

Your message must be no longer than 47 characters (the discrepancy from the earlier figure of 49 will be explained later). To encode the message, you will need:

• A deck of cards arranged in the agreed-upon key order
• Pen(cil) and paper
• A pocket calculator (in fact, you can do all the computations by hand, but this is tedious and error-prone)
If your message is shorter than 47 characters, then fill it out with spaces or random padding letters so that it is exactly 47 characters long. Convert the message to a list of 47 numbers (space=0, A=1, B=2, etc.). We will refer to this list as List M. Elsewhere on your paper, start another list of numbers (which we will call List A) containing just the number 0. Take your key deck, face down, and deal the first 7 cards into a new pile (which we will call Pile C). Now, focus on the last number in List M and do the following:
1. Multiply the last number in List A by 27.
2. Take the number in List M that you are currently focussed on, and add that to the product from Step 1.
3. Take the sum from Step 2 and divide it by 1 more than the number of cards in Pile C.
4. Find the integral part (that is, the part of the number before the decimal point) of the result of Step 3, and append it to the end of List A.
5. Subtract the number from Step 4 from the result of Step 3 (you should get a number between 0 and 1).
6. Multiply the result from Step 5 by 1 more than the number of cards in Pile C. (The answer should come out to an integer, but if it doesn't because of calculator rounding, just round to the nearest integer.)
7. Remove as many cards from the top of Pile C as the result of Step 6 (you might remove as few as 0 or as many as all of the cards).
8. Take the top card from the key deck and put it on top of what's left of Pile C.
9. Replace the cards back on top of Pile C.
10. If there are no more cards left in the key deck, stop. Otherwise, shift your "focus" to the entry in List M immediately before the one you are currently focussed on, and go to Step 1.
Pile C now encodes your message, and can be sent to the recipient.

#### Decoding

To decode a message, you need the deck in which the message is encoded (the "message pile"), in addition to the key in some form. The key does not need to be an actual deck of cards; an ordered list of the cards will suffice. Pen(cil), paper, and calculator are also necessary. As before, start a list of numbers (which we will call List B) containing the number 0. Focus on the last "card" in the key deck and do the following:

1. Multiply the last entry in List B by the number of cards in the message pile.
2. Find the card in the message pile that matches the current card in the key deck, and count the number of cards above that card (if the card is on top, the result is 0). Remove the card and put it in the discard pile; it will not be used again.
3. Add the result from Step 2 to the product from Step 1.
4. Divide the result from Step 3 by 27.
5. Find the integral part of the result of Step 4, and append it to the end of List B.
6. Subtract the number from Step 5 from the result of Step 4 (you should get a number between 0 and 1).
7. Multiply the result from Step 6 by 27 (again, round to the nearest integer).
8. Write down the character corresponding to the number obtained in Step 7 (remember, space=0, A=1, B=2, etc.).
9. If you have reached the end of the message, stop. Otherwise, shift your focus to the "card" in the key deck immediately above the one you are currently focussed on, and go to Step 1.

### Examples

The cards will be referred to by rank and suit; e.g.: 8s=Eight of Spades, 9d=Nine of Diamonds, etc. A=Ace, T=Ten, J=Jack, Q=Queen, K=King. The jokers are referred to as Rj and Bj (Red Joker and Black Joker), althought it does not matter what color the actual jokers are, as long as they are distinguishable. In a listing of cards, the first card in the list is at the top of the deck, while the last is at the bottom.

#### Encoding

Suppose we wanted to send the message MEET AT TWO AM BEHIND THE CASINO WITH THE MONEY (incidentally, this message has exactly 47 characters). Convert it to a list of numbers:

13 05 05 20 00 01 20 00 20 23 15 00 01 13 00 02 05 08 09 14 04 00 20 08 05 00 03 01 19 09 14 15 00 23 09 20 08 00 20 08 05 00 13 15 14 05 25
Further suppose that we had previously agreed upon the following as the key deck:
2s Kh Qd Ah Ks Bj 5c 4s 3s 7h 4h Kd 9c 8c Td Js 5h 3h 8h Ts 9s 5s As 7s Ad 4c 3c Jc 6h 8d Tc 2h Ac Qh Th 7c 9d 8s 5d Kc 2c 2d Qc Jh 6s Rj 6c Jd 6d 4d Qs 7d 3d 9h
Deal out the first 7 cards of the key deck to form "Pile C". The algorithm will then proceed as follows:

```List M: ...13 15 14 05 25
Pile C: 5c Bj Ks Ah Qd Kh 2s
Key deck: 4s 3s 7h 4h Kd 9c...
List A: 0```
• Multiply the last number in List A by 27: 0*27=0.
• Add the current value in List M: 0+25=25.
• Divide by 1 more than the number of cards in Pile C: 25/8=3.125.
• Append the integral part to List A:
List A: 0 3
• Subtract the integral part: 3.125-3=0.125.
• Multiply by 1 more than the number of cards in Pile C: 0.125*8=1.
• Remove that many cards from the top of Pile C:
Pile C: 5c _ Bj Ks Ah Qd Kh 2s
• Insert the top card of the key deck and replace the cards:
Key deck: 3s 7h 4h Kd 9c...
Pile C: 5c 4s Bj Ks Ah Qd Kh 2s
• Go back one in List M:
List M: ...13 15 14 05 25
• Multiply the last number in List A by 27: 3*27=81.
• Add the current value in List M: 81+5=86.
• Divide by 1 more than the number of cards in Pile C: 86/9=9.555
• etc...
When this is done, you will end up with Pile C being:
5c 2c 2h 3c 5s 4s Bj 7d Ks 6c 6h Rj Ah 9h 3s 4c Jh Qd 8d Kd 3h 7h Jd 8c Kc 4h 7c Ac Qs Jc Td 9c 8s 3d 9s As 2d Qh 9d Kh Ad Th 8h 5d 5h Js Ts 4d 2s 6s Tc 6d Qc 7s

#### Decoding

We receive a deck of cards from our co-conspirator. It is the same one that was produced above:

5c 2c 2h 3c 5s 4s Bj 7d Ks 6c 6h Rj Ah 9h 3s 4c Jh Qd 8d Kd 3h 7h Jd 8c Kc 4h 7c Ac Qs Jc Td 9c 8s 3d 9s As 2d Qh 9d Kh Ad Th 8h 5d 5h Js Ts 4d 2s 6s Tc 6d Qc 7s

We also know that the message was encoded using the following key:

2s Kh Qd Ah Ks Bj 5c 4s 3s 7h 4h Kd 9c 8c Td Js 5h 3h 8h Ts 9s 5s As 7s Ad 4c 3c Jc 6h 8d Tc 2h Ac Qh Th 7c 9d 8s 5d Kc 2c 2d Qc Jh 6s Rj 6c Jd 6d 4d Qs 7d 3d 9h

The algorithm will now proceed as follows:

```Message pile: 5c 2c 2h...Ah 9h 3s...8s 3d 9s...
Key deck: ...Jd 6d 4d Qs 7d 3d 9h
List B: 0```
• Multiply the last entry in List B by the number of cards in the message pile: 0*54=0.
• Find the card in the message pile that matches the current card in the key deck:
Message pile: 5c 2c 2h...Ah 9h 3s...8s 3d 9s...7s
Count the number of cards above it: 13. Now discard it:
Message pile: 5c 2c 2h...Ah 3s...8s 3d 9s...7s
• Add the results from the previous two steps: 13+0=13.
• Divide by 27: 13/27=0.481481
• Find the integral part, and append it to List B:
List B: 0 0
• Subtract the integral part from the original: 0.481481-0=0.481481.
• Multiply the result by 27: 0.481481*27=13.
• Write down the character corresponding to this number: 13=M.
• Shift the focus in the key deck up one card:
Key deck: ...Jd 6d 4d Qs 7d 3d 9h
• Multiply the last entry in List B by the number of cards in the message pile: 0*53=0.
• Find the card in the message pile that matches the current card in the key deck:
Message pile: 5c 2c 2h...Ah 3s...8s 3d 9s...7s
Count the number of cards above it: 32. Now discard it:
Message pile: 5c 2c 2h...Ah 3s...8s 9s...7s
• Add the results from the previous two steps: 32+0=32.
• Divide by 27: 32/27=1.185185.
• Find the integral part, and append it to List B:
List B: 0 0 1
• Subtract the integral part from the original: 1.185185-1=0.185185.
• Multiply the result by 27: 0.185185*27=5.
• Write down the character corresponding to this number: 5=E.
• Shift the focus in the key deck up one card:
Key deck: ...Jd 6d 4d Qs 7d 3d 9h
• Multiply the last entry in List B by the number of cards in the message pile: 1*52=52.
• etc...
When the message is finished, the letters will reconstitute the original message: MEET AT TWO AM BEHIND THE CASINO WITH THE MONEY.

#### Why does it work?

Although it may seem otherwise, the algorithms are not arbitrary sequences of steps. By taking a more abstracted view, we can see how each step is necessary to encode or decode. Note: usages of ordinals written in digits (e.g., 1st, 3rd) should be interpreted as referring to a list that is begun with "0th", so for example {space} is the 0th letter of the alphabet, "A" is the 1st, etc. When written out in words (e.g., first, third), the count begins with "first".

Start at the beginning, at the first iteration, where there are 7 cards in Pile C. This gives us 8 options of where to put the next card. However, we need to encode one of 27 characters. To do this, the algorithm essentially divides the "alphabet" into blocks of 8. (Since 8 does not go into 27 evenly, the last block has 3, not 8, characters.) There are 4 blocks, and since the choice of which block the right character is in cannot be encoded in the the placement of the next card, this decision must be "deferred" to the next iteration. In the algorithm, this is done by writing the deferred choice at the end of List A. For example, if the first letter to encode is "Y", then, being the 25th letter, it is the 1st letter in the 3rd block, so the next card is inserted at position 1 in Pile C, and the deferred choice is 3.

At the second iteration, we again have another character to encode, as well as encoding the choice that we "inherited" from the previous iteration. Since there are 27 options for a character, and 4 inherited options, there are now 108 options to be encoded (27*4=108). We do this by multiplying the inherited choice by 27 and then adding the character to be encoded. In effect, we have created a new, 108-letter alphabet by repeating the 27-letter alphabet 4 times, where the alphabet from which the character should be selected is determined by the inherited choice. Continuing the previous example, if the letter is "E", and the inherited choice is 3, then we choose "E" from the 3rd repetition of the alphabet, which is the 86th letter in the 108-letter alphabet. Now there are 9 options of where to put the next card, so we break the 108-letter alphabet into 12 blocks of 9. Again, the position in which to place the new card is determined by the selected character's position within its block, while the deferred choice is determined by which block contains the character. In this case, the 86th character in the whole alphabet is the 5th letter in the 9th block, so we insert the next card at position 5 and defer "9" to the next iteration.

At the twentieth iteration, there are 27 options of where to put the next card. There are 23,863 inherited options, but since the placement of the cards is exactly sufficient to encode the next character, we do not need to defer any more options to the twenty-first iteration. The twenty-first iteration therefore also inherits 23,863 options. At this point, there are 28 options of where to put the next card, more than the number of characters in the alphabet, so we can finally start making a dent in the deferred options. 27 character options * 23,863 inherited options = 644,301 total options to encode. We now divide this set of options into blocks of 28, resulting in 23,011 blocks. This means that only 23,011 options are deferred to the twenty-second iteration, which is finally fewer than before.

From this point on, the number of deferred options continues to shrink. At the last iteration, when there are 53 cards in Pile C, we have 54 choices of where to put the next card. We have also inherited 2 options, so we repeat the alphabet 2 times and choose the appropriate letter from whichever repetition of the alphabet is indicated by the inherited choice. Now we are done, and the deferred options have been reduced to 1.

Now you see why we started with 7 cards in Pile C: 7 is the fewest cards we can start with without ending up with more than one option to defer at the last iteration. This would not be good, since we have run out of cards, and there is nothing to defer the choice to.

The decoding algorithm is simply the reverse of the encoding algorithm.

Why can this algorithm only handle 47-character messages, while 49 characters are theoretically possible? The answer is that the order of the first 7 cards in the key deck does not matter. When encoding, the first 7 cards in the key deck serve only as "anchors" around which to construct the message deck; when decoding, you reach the end of the message without having looked at these cards. Since 272<7!<273, we could conceivably encode 2 additional characters into the arrangement of these 7 cards, bringing the total up to 49. However, it seems an unnecessary complication of the algorithm for the sake of 2 letters.

#### Practical use

It is fairly straightforward to create a program or a spreadsheet setup to perform the algorithms more-or-less instantaneously. Doing it longhand with a calculator shouldn't take more than 30 minutes if you know what you're doing.

As long as the keys are kept secret and are not reused, this method provides perfect security. That is, without knowing the key, it is impossible even in principle to extract a message from the deck. This is because the system is a one time pad, so any possible message is equally likely given only the cipher deck.

Unfortunately, reusing keys may not be perfectly secure, because the method does not cause the avalanche effect. In other words, a small change in the message text does not cause a total and unpredictable change in the cipher deck. This means that it may be possible to analyze several cipher decks encoded with the same key and extract information from them based on the redundancy of natural language.

On the plus side, if you are caught with a deck encoding a potentially compromising message, then the message can be destroyed quickly and permanently by throwing the cards up into the air.

Log in or register to write something here or to contact authors.