-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alice.py
64 lines (44 loc) · 1.38 KB
/
Alice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from pokercards import cards
from crypto import encrypt, decrypt, generate_keys
from Utils import *
from poker import playPoker
import socket
import pickle
TCP_IP = '127.0.0.1'
TCP_PORT = 5002
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
# connect to BoB
conn, addr = s.accept()
# get prime from Bob
prime = receive(conn)
# generate encryption key such that gcd(encryptionkey,prime) == 1
# generate decryption key such that (encyptionkey)(decyptionkey) = 1 mod(prime)
en_key, de_key = generate_keys(prime)
# encrypt the cards
encrypted_card_numbers = []
for cardnumber in cards.keys():
encrypted_card_numbers.append(encrypt(cardnumber, en_key, prime))
# send encrytped cards to Bob
send(conn, encrypted_card_numbers)
# receive my crads
mycards = receive(conn)
# receive bob cards
bobcards = receive(conn)
# decrypt my cards
for index, card in enumerate(mycards):
mycards[index] = decrypt(card, de_key, prime)
# decrypt bob cards
for index, card in enumerate(bobcards):
bobcards[index] = decrypt(card, de_key, prime)
# send bob cards to bob
send(conn, bobcards)
#print("My Cards are")
#printCards(mycards, cards)
#playPoker(mycards, othercards, me, other, socket, de_key, prime)
playPoker(mycards, bobcards, "Alice", "Bob", conn, de_key, prime)
conn.close()
s.close()