[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Card Playing Protocol?



What I suggest you do is you build something that can be telnetted into.

Say, something that would sit on a specific telnet port that people can telnet
into.  When they do, another copy of the poker (or whatever game) process is
forked into existence, and all of these processes can talk to each other
to pass on the deck encrypted in some form or other.

From what I remember off the top of my head:

  You have to use a cypher which allows each card to be doubly encrypted and
decrypted without decrypting both encryptions:

  1.  Card encrypted by player 1:   E1(Card,eK1)
  2.  Card encrypted by player 2:   E2(Card,eK2)
  3.  Card encrypted by player 1, then encrypted by player 2:
                                    E2(E1(Card,eK1),eK2)

Now, whatever you do, player one must be able to decrypt his encryption from
step 3 above.  That is he should be able to take: E2(E1(Card,eK1),eK2) and
decrypt it with his key giving E2(Card,eK2) as follows:

      D1(E2(E1(Card,eK1),eK2),dK1) = E2(Card,eK2)

Where E1(card,key1) means encrypted by Player 1 with his key, and eK1 means
Player 1's encryption key; D1() means decrypt by player 1 with his decryption
key dK1, etc.

You can take any cypher you like and make it into a random number generator
by putting it in a feedback mode which doesn't encrypt, but rather just
generates numbers (I forgot the name of this mode, but it's one of the DES
modes that's commonly used for communications which is immune to noise.) This
mode is built so that both sides use this sort of generator and simply XOR
the plaintext with the generated data to produce the cyphertext, and the
receiver XOR's the generated code of his generator with the received
cyphertext.

Anyway, what I'm getting to here is that XOR (exclusive OR, the ^ operator in
C) will allow you to meet the above requirement:
D1(E2(E1(Card,eK1),eK2),dK1) = E2(Card,eK2) so as to be able to implement the
card playing protocol.

An analogy to this is a box that has two pad locks on it put in such a way so
that the owner of one lock can remove that lock without having the other owner
remove his first.

Basically the two players pass an encrypted deck to each other.  Off the top
of my head (please check this!) both players encrypt the deck of cards.  
Alice and Bob are our players.  So Alice picks her hand, but since they are
still encrypted with Bob's key, she can't see what she's picked.  She passes
her picked hand to Bob.  He decrypts the hard with his key and returns it to
Alice.  Since this had was encrypted by Alice, Bob can't reveal it by decryption

Then Alice decrypts her hand and holds on to it.  She then passes the whole
deck (except for her hand) to Bob.  He picks his hand, sends it back to Alice,
she decrypts his hand and returns it to Bob.  He decrypts his hand and keeps
it, then passes the deck back to Alice.

When Alice needs to pick a card, she has to pass it to Bob to decrypt, etc.
And that in a nutshell is how the protocol works.  Since both sides see that
all the cards are there, they can verify that no one has cheated.  Since
neither side can see the other's cards, the game is safe.

I don't recall what you do with discarded cards... maybe mark them as such?

Also here's something else out to help you:

// shuffle the deck routine:
cardtype   cards[4*13+2]; // four suites of 13 cards + 2 jokers.

//initialize the deck:
for (i=0; i<=4*13+2; i++) cards[i].cardnumber=i;

//shuffle the deck:
for (i=0; i<=10000; i++)
 {
  c1=rand() % (4*13+2);
  c2=rand() % (4*13+2);
  swapcards(&cards[c1],&cards[c2]);
 }


You still have to define what the cards structure is, but I suggest you put
in plenty of information in them such as a discarded flag, maybe a player's
ID in which hand this card lives (if you pass the whole deck instead of the
unused cards), flags to indicate which players encrypted this card, etc.

The two for loops above work to build a deck for you in the best possible way.
The 1st, initializes the deck in order..   The second shuffles the cards
by swapping two at a time.  These functions are far more efficient for
shuffling/building a deck of cards than by picking a random number for a card
ID and checking to see if we've already seen it.


Also, I would add functions in to automate the game, be it Poker, or 21, or
whatever....  Ie: allowing the players to decide what's wild, automatically
checking each player's hand and telling them their hand, allowing for a card
split in Blakc Jack, etc.

If you like I can see if I can find some sources to card games for you...