-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient222.py
60 lines (43 loc) · 1.61 KB
/
client222.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
import socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from optparse import OptionParser as OP
#host = "127.0.1.1"
#port = 8000
parser = OP(usage="usage: %python filename -t <server> -p <port> ")
parser.add_option("-t",type="string",
help = "IP address of the server",
dest ='server_ip')
parser.add_option("-p",type="int",
help = "Port number of the server",
dest = 'port')
(options, args ) = parser.parse_args()
if (options.server_ip == 0) | (options.port == 0):
print(parser.usage)
host = options.server_ip
port = options.port
Cserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
Cserver.connect((host, port))
#Tell server that connection is OK
Cserver.sendall("Client: OK")
#Receive public key string from server
server_string = Cserver.recv(2048)
#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)
key_s = PKCS1_OAEP.new(server_public_key) #Breaking the vulnerability of RSA
#Encrypt message and send to server
message = raw_input("Stream > \n")
encrypted = server_public_key.encrypt(message, 2048)
Cserver.sendall("encrypted_message=" + str(encrypted))
#Server's response
server_response = Cserver.recv(1024)
server_response = server_response.replace("\r\n", '')
if server_response == "server: OK":
print "Server decrypted message successfully"
#Tell server to finish connection
Cserver.sendall("Quit")
print(Cserver.recv(1024)) #Quit server response
Cserver.close()