Pocket-RC4

Encryption for the covert magician.

Pocket-RC4 is an adaptation of the RC4B algorithm for use with standard playing cards, developed by Matthew E. McKague in his master’s thesis.

A card cipher is a great introduction to cryptography as it gives you something tangible to work with, which helps to bind abstract cryptography concepts to real world objects; I highly recommend this as a teaching aide for cryptography 101 courses.

Let’s assume you work for the Malevolent Association of Genuinely Ingenious Conjures (M.A.G.I.C.), a clandestine order of magicians who need to covertly communicate the details of their latest magic tricks. Or you might be someone who wants to secretly communicate with a friend, or you might just be someone who is interested in ciphers, but is far more fun to assume you work for M.A.G.I.C..

For the tl;dr version try jumping right ahead to the code and interactive demo.

Set-up

Before you begin, M.A.G.I.C. and you need to agree on the following things:

  1. Initialisation Vector
  2. Key deck
  3. Card letter assignments

1) Initialisation Vector

An initialisation vector (IV), is a string of random characters used to prepare the deck for encryption/decryption. The IV adds additional randomness to the encryption process by ensuring each ciphertext produced by the algorithm is associated with a pseudorandom initial deck state. This makes it harder for our nemesis to work out the key deck ordering from captured plaintext/ciphertext pairs. This is why the IV should be unique for every communication and should be randomly generated. An IV of 27 letters should be sufficient.

An easy way to randomly generate an IV for a card cipher is to: shuffle the deck, draw a card, write down the card’s letter value, return the card to the deck. Repeat this until you have an IV of 27 characters.

2) Key deck

The key deck is a specific ordering of cards in a deck. This ordering is kept secret and each of the two parties wishing to communicate with each other will need to maintain the same ordering as the other. When the IV and ciphertext are received, they are applied to the key deck to reveal the hidden message.

Any random deck order can be used as the key deck. A random order is better than a simple pattern, as the simple pattern might be guessed easily in an intelligent brute force attack.

The ciphertext and the IV are the public components of the encryption, they can be freely captured without compromising the message (Sort of, RC4 is quite broken and each captured IV/ciphertext pair gets you closer to revealing the key deck). The key deck’s ordering is the secret component of the communication, and should be changed if the ordering is ever compromised.

3) Card Values

The assignment of letters to cards can be entirely decided upon by you, but assigning the cards to letters in alphabetical order, with jokers being space, is the easiest, see below.

Hearts and Spades
Ace
2
3
4
5
6
7
8
9
10
Jack
Queen
King
1
2
3
4
5
6
7
8
9
10
11
12
13
a
b
c
d
e
f
g
h
i
j
k
l
m
Diamonds and Clubs
Ace
2
3
4
5
6
7
8
9
10
Jack
Queen
King
14
15
16
17
18
19
20
21
22
23
24
25
26
n
o
p
q
r
s
t
u
v
w
x
y
z
Joker
Joker
27
Space

Algorithm

The Pocket-RC4 algorithm is a (nearly) symmetrical stream cipher. The only difference being: When encrypting, you add the keystream value to the plaintext, and when decrypting, you subtract the keystream value from the ciphertext.

Prepare the Key Deck

The key deck needs to be rearranged so that it maintains the same order within cards of the same colour, but cards in the deck must alternate between colours, starting with red on top.

  1. Take the key deck, face up.
  2. Deal each card from the key deck into either a red pile, or a black pile. Both piles must be face up and be sure to preserve the order exactly.
  3. Make a new pile, face up, by interleaving the two piles. This is done starting with the first black card, then the first red card, the second black card, second red, and so on. The new pile should have a red card on top and alternate between red and black cards.

Randomise the Key Deck with the Initialisation Vector (IV)

The pseudorandom initial deck ordering needs to be generated.

  1. Take the key deck, face up.
  2. For each card in the IV sequence do the following:
    1. Move the top red card to the bottom of the deck.
    2. Find the black card in the key deck that has the same value as the current IV card.
    3. Move the red card above this black card to the top of the deck.
    4. Move the black card to the bottom of the deck.
    5. Moce the top two cards (one red, one black) to the bottom of the deck.

Encrypt the Plaintext Message

  1. Take the key deck, face up.
  2. For each letter in the plaintext do the following:
    1. Set j to the value of the bottom red card.
    2. Add the value of the top red card to j, modulo 27.
    3. Find the black card with the value of j.
    4. Set k to the value of the red card above the j black card plus the value of the top red card, modulo 27.
    5. Set k to the value of the plaintext letter plus k, modulo 27.
    6. Write down the corresponding ciphertext character, which has the value of k.
    7. Exchange the red card above the j black card with the top red card.
    8. Move the top black/red card pair to the bottom.

Decrypt the Ciphertext Message

  1. Take the key deck, face up.
  2. For each letter in the ciphertext do the following:
    1. Set j to the value of the bottom red card.
    2. Add the value of the top red card to j, modulo 27.
    3. Find the black card with the value of j.
    4. Set k to the value of the red card above the j black card plus the value of the top red card, modulo 27.
    5. Set k to the value of the ciphertext letter minus k, modulo 27.
    6. Write down the corresponding plaintext character, which has the value of k.
    7. Exchange the red card above the j black card with the top red card.
    8. Move the top black/red card pair to the bottom.

Code Example

The following is a bare-bones example, and tries to do things as simply as possible; which ignores a lot of good coding practices, as well as places where the implementation can be made faster and/or more efficient.



Interactive Demo

Afterthoughts and Variations

  • The jokers can be excluded to produce an alphabet without spaces, a subtle variation on the algorithm would be required for this. This seems unnecessary, because the presence of jokers increases the cryptographic strength of the algorithm. You may want to remove the jokers if you are in a region or near a game that does not typically use jokers.
  • The cards don’t need to be assigned to the standard Latin alphabet, you could swap them with characters from other writing systems. The space could be replaced with a ß, for German. A c could be replaced with an é for Afrikaans. In alphabets that contain less than 26 characters, the remaining cards can be used to duplicate letters, this will maintain cryptographic strength. If your alphabet contains more than 26 letters, you may build an encoding system that can store the alphabet in 26-space.
Written on the 14th day of January in the year of our Lord two thousand and seventeen