-
Notifications
You must be signed in to change notification settings - Fork 2
/
poker client.py
316 lines (291 loc) · 8.76 KB
/
poker client.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Contributors:
# Timothy Fong
# Haely Shah
# Oliver Zhu
# CMPE 209 Poker Project
import socket
from Crypto.PublicKey import RSA
from Crypto import Random
import hashlib
import os, random, struct, sys
import Crypto.Random
from Crypto.Cipher import AES
import time
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
#Declare server's attributes
server = socket.socket()
#host = "10.0.2.15"
host = "127.0.0.1"
port = 7777
#Connect to server
server.connect((host, port))
#Tell server that connection is OK for poker game
server.sendall("Poker: GO")
#Receive public key string from server
server_string = server.recv(1024)
#Remove extra characters
server_string = server_string.replace("public_key=", '')
server_string = server_string.replace("\r\n", '')
#Convert string to key
server_public_key = RSA.importKey(server_string)
#print server_public_key.exportKey()
#Generate Client public/private key pair
random_generator = Random.new().read
client_private_key = RSA.generate(1024, random_generator)
client_public_key = client_private_key.publickey()
#Server's response if too many clients
server_response = server.recv(1024)
server_response = server_response.replace("\r\n", '')
print "server: " + server_response
#If Server occupied
if server_response == "Too many clients":
print "Fail: server is currently full"
print "Now quiting"
server.close()
sys.exit()
#Otherwise, send client's public key for asymmetric encryption
elif server_response == "Request Public Key":
server.sendall("public_key=" + client_public_key.exportKey() + "\n")
#print client_public_key.exportKey()
#Login to obtain public key
username = raw_input("Username: ")
type(username)
password = raw_input("Password: ")
type(password)
message = "encrypted_login=" + username + "!@#$%^&*()" + password
#hashing, maybe later
#hash_user = hashlib.md5(username)
#hash_pass = hashlib.md5(password)
#message = hash_user.hexdigest() + "#" + hash_pass.hexdigest() + "#" + client_public_key
#Encrypt message
encrypted = server_public_key.encrypt(message, 32)
#Send Login Info to server
server.sendall(str(encrypted))
#Receive server reply confirming reception of username and password
#server_response = server.recv(1024)
#server_response = server_response.replace("\r\n", '')
#Receive request for session key
server_response = server.recv(1024)
encrypted = eval(server_response)
#Decrypt message
decrypted = client_private_key.decrypt(encrypted)
print decrypted
#Confirm request
if "Request Session Key" in decrypted:
#Generate AES key and information
AES_key = Crypto.Random.OSRNG.posix.new().read(AES.block_size)
#print AES_key
#Generate initialization vector
IV = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
#print IV
mode = AES.MODE_CBC
#Encrypt AES key and IV
encrypted = server_public_key.encrypt(AES_key + "!@#$%^&*()" + IV, 32)
#Send to server
server.sendall(str(encrypted))
else:
print "nono"
sys.exit()
#Decryptor/Encryptor check message
aes = AES.new(AES_key, mode, IV=IV)
#Get Player ID
player = ''
server_response = server.recv(1024)
player = aes.decrypt(server_response)
player = player.replace("~", '')
if player == "0":
print "Room full"
sys.exit()
else:
print "Player " + player
#Poker game
while True:
#Ready up
#print "type READY"
msg = raw_input("type READY or QUIT: ")
while msg != "READY" and msg != "QUIT":
#print "type READY or QUIT"
msg = raw_input("type READY or QUIT: ")
type(msg)
#msg = "QUIT"
if "READY" in msg:
print msg
if len(msg) % 16 != 0:
msg += '~' * (16 - len(msg) % 16)
ciphertext = aes.encrypt(msg)
server.sendall(ciphertext)
elif "QUIT" in msg:
print msg
if len(msg) % 16 != 0:
msg += '~' * (16 - len(msg) % 16)
ciphertext = aes.encrypt(msg)
server.sendall(ciphertext)
break
#Waiting for players
print "SIGNAL"
signal = ''
while("DEALING" not in signal):
server_response = server.recv(1024)
signal = aes.decrypt(server_response)
print signal
time.sleep(1)
#Receive a hand of cards
server_response = server.recv(1024)
HandofCards = aes.decrypt(server_response)
HandofCards = HandofCards.replace("~", '')
print HandofCards
#Betting
#Receive own money situation
while True:
#Bet Turn/Betting phase
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
msg = msg.replace("\r\n", '')
if "Betting" in msg:
#money = 0
choose = ""
current_bet = 0
bet = 0
#Receive Money info
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
msg = msg.replace("\r\n", '')
print "Make a bet. Your money: " + msg
money = int(msg)
#Receive Pot info
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
print "Pot: " + msg
#Receive current bet value
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
msg = msg.replace("\r\n", '')
print "Current_bet: " + msg
current_bet = int(msg)
print
#Choose
choose = raw_input("type BET or FOLD: ")
type(choose)
while choose != "BET" and choose != "FOLD":
#print "type READY or QUIT"
choose = raw_input("type BET or FOLD: ")
type(choose)
if choose == "FOLD":
#Hash the fold
digest = SHA256.new()
digest.update(choose)
#Load private key and sign message
signer = PKCS1_v1_5.new(client_private_key)
sig = signer.sign(digest)
#Sign the fold with RSA private key
signed_fold = choose + "!@#$%^&*()" + sig
#Encrypt with AES and send to server
if len(signed_fold) % 16 != 0:
signed_fold += '~' * (16 - len(signed_fold) % 16)
ciphertext = aes.encrypt(signed_fold)
server.sendall(ciphertext)
elif choose == "BET":
#Input bet amount
bet = raw_input("bet amount: ")
type(bet)
#Bet has to be great than or equal to the current bet
#Bet has to be less or equal to your current money
while(int(bet) < current_bet or int(bet) > money):
bet = raw_input("bet amount: ")
type(bet)
#Round numbers if jerks type float values
bet = int(bet)
bet = str(bet)
#Hash the bet
digest = SHA256.new()
digest.update(bet)
#Load private key and sign message
signer = PKCS1_v1_5.new(client_private_key)
sig = signer.sign(digest)
#Sign the bet with RSA private key
signed_bet = bet + "!@#$%^&*()" + sig
#Encrypt with AES and send to server
if len(signed_bet) % 16 != 0:
signed_bet += '~' * (16 - len(signed_bet) % 16)
ciphertext = aes.encrypt(signed_bet)
server.sendall(ciphertext)
else:
#print "You folded"
print msg
#Receive verification response
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
if "Resend" in msg:
print "Resending"
if choose == "FOLD":
#Hash the fold
digest = SHA256.new()
digest.update(choose)
#Load private key and sign message
signer = PKCS1_v1_5.new(client_private_key)
sig = signer.sign(digest)
#Sign the fold with RSA private key
signed_fold = choose + "!@#$%^&*()" + sig
#Encrypt with AES and send to server
if len(signed_fold) % 16 != 0:
signed_fold += '~' * (16 - len(signed_fold) % 16)
ciphertext = aes.encrypt(signed_fold)
server.sendall(ciphertext)
else:
#Hash the bet
digest = SHA256.new()
digest.update(bet)
#Load private key and sign message
signer = PKCS1_v1_5.new(client_private_key)
sig = signer.sign(digest)
#Sign the bet with RSA private key
signed_bet = bet + "!@#$%^&*()" + sig
#Encrypt with AES and send to server
if len(signed_bet) % 16 != 0:
signed_bet += '~' * (16 - len(signed_bet) % 16)
ciphertext = aes.encrypt(signed_bet)
server.sendall(ciphertext)
else:
#Should print No problem
print msg
#Check if all player's bets are the same
server_response = server.recv(1024)
msg = aes.decrypt(server_response)
msg = msg.replace("~", '')
if "Move" in msg:
print msg
break
else:
print msg
#Result: Win or Lose
server_response = server.recv(1024)
result = aes.decrypt(server_response)
result = result.replace("~", '')
print result
if "LOSE" in result:
print "Winner Hand"
server_response = server.recv(1024)
winning_hand = aes.decrypt(server_response)
winning_hand = winning_hand.replace("~", '')
print winning_hand
#print server_response
#while True:
#break
#Tell server to finish connection
#if message == "quit": break
#server.sendall("Quit")
print "done"
#print(server.recv(1024))
#Quit server response
server.close()
# Contributors:
# Timothy Fong
# Haely Shah
# Oliver Zhu
# CMPE 209 Poker Project