Skip to main content

Deck of Cards Code

Hide a message in the order of a 52-card deck, and read it back.

There are 52! (52 factorial) ways to put 52 playing cards in order, an 80-digit number of possibilities. By matching each ordering to a number, and each number to a string of letters, you can hide a whole message inside the order of a single deck. Type a message to lay out the deck, or enter a deck order to read the message back. See how it works below.

Allowed characters: letters A-Z, space, and period ., up to 46 characters (a 47th fits if the message starts with a letter before T).

Numeric value of this message

How much can a deck of cards store?

There are 52! (52 factorial, or 52 x 51 x 50 ... x 1) possible orderings of 52 cards:

80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000

You can count off all those possibilities and assign each one a number. A deck completely in order might be #1, a deck with the first two cards swapped #2, and so on. That gives a way to store any number from 1 up to that 68-digit value, so any 67-digit number always fits. That is enough room for six 10-digit phone numbers written one after another.

How many digits fit depends on the number base. log(52!) / log(10) = 67.9, so 68 base-10 digits. log(52!) / log(2) = 225.6, so the order of a deck holds 225 bits, enough for 28 eight-bit bytes or 32 seven-bit ASCII characters. To hide plain text we use a 28-character alphabet (26 letters plus a space and a period) as the digits of a base-28 number, which stores log(52!) / log(28) = 46.9, so a 46-character message. The mapping here is A=1, B=2 and so on, with space = 27 and the period = 0. Just like base-10, each column to the left is worth 28 times more: "A" = 1, "B" = 2, "A." = 28, "AA" = 29, "AB" = 30.

Counting the orders

Take a small deck of four cards, A, B, C, and D. They have 4! = 24 orders. Start with four empty slots. There are 4 places to put A, then 3 left for B, then 2 for C, and D takes the last slot. Placing cards left to right, "skip" 0, 1, 2, or 3 spaces before laying down A, then 0, 1, or 2 before B, then 0 or 1 before C.

To turn a message number into skip values, divide. The first division is by 52, and the remainder is how many spaces the first card skips. The next division is by 51, then 50, and so on down to 1, which always leaves a remainder of 0 (there is only one slot left). For four cards, encoding #18: 18 / 4 = 4 remainder 2, so A skips 2. Then 4 / 3 = 1 remainder 1, so B skips 1. Then 1 / 2 = 0 remainder 1, so C skips 1. D never skips. The result is the pattern "DBAC".

Reading the order back

To decode, look at each card and count how many bigger cards sit to its left (B is bigger than A, C bigger than B, and so on). In "DBAC", A has two bigger cards to its left, B has one, and C has one. Those counts are exactly the original skips. Multiply each by the number of possibilities before it and add: 2 + (1 x 4) + (1 x 12) = 18, recovering pattern #18. The same idea scales straight up to a full 52-card deck, you just need big-number math. The agreed deck order is Clubs, Diamonds, Hearts, Spades, each suit from the deuce up to the ace, so the first card is the deuce of clubs and the last is the ace of spades.

Concept and original encoding by Peter Jerde.