-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_send.py
57 lines (43 loc) · 1.48 KB
/
connection_send.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
import socket #for sockets
import sys #for exit
import encryptor
import keys
import pickle
try:
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
print 'Socket Created'
host = 'localhost'
port = 5003
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#could not resolve
print 'Hostname could not be resolved. Exiting'
sys.exit()
print 'Ip address of ' + host + ' is ' + remote_ip
#Connect to remote server
s.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
#Send some data to remote server
p = keys.generateLargePrime(10)
#q = int(raw_input("Enter another prime number (Not one you entered above): "))
q = keys.generateLargePrime(10)
print "Generating your public/private keypairs now . . ."
public, private = encryptor.generate_keypair(p, q)
print "Your public key is ", public ," and your private key is ", private
message = "Hej med dig"
encrypted_msg = encryptor.encrypt(private, message)
print(encrypted_msg)
try :
#Set the whole string
s.sendall(pickle.dumps(public))
s.sendall(''.join(map(lambda x: str(x), pickle.dumps(encrypted_msg))))
except socket.error:
#Send failed
print 'Send failed'
sys.exit()
print 'Message send successfully'