-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.py
executable file
·56 lines (43 loc) · 1.47 KB
/
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
import socket
import sys
from ElGamal import *
# encrypt returns the encrypted character
# Parameters: b, message, pubKey
# returns the encrypted message in form of an array
def encrypt(message, pubKey):
list_of_cipher_letters = []
for i in range(0,len(message) - 1):
cipher = elgamal_encrypt(message[i],pubKey)
list_of_cipher_letters.append(cipher)
return list_of_cipher_letters
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
host = socket.gethostbyname(host)
port = 5555
s.connect((host,port))
bit_length = input("Choose a bit length for the Elgamal prime: ")
s.send(str.encode(bit_length))
pubKey = s.recv(2048).decode()
pubKeyList = pubKey.split(', ')
#prime
pubKeyList[0] = int(pubKeyList[0])
#generator
pubKeyList[1] = int(pubKeyList[1])
#halfMask
pubKeyList[2] = int(pubKeyList[2])
aes_key = input("Choose a secret key for aes: ")
print("Waiting for input, connected to: " + host + " on port " + str(port))
print("type quit* to disconnect")
while True:
msg = sys.stdin.readline()
if (msg == "quit*\n"):
break
#great lets encrypt a message
cipher_list = encrypt(msg, pubKeyList)
cipher_string = ""
for i in range(0,len(cipher_list) - 1):
cipher_string += str(cipher_list[i])
#encode turns our string into byte code to be sent to the socket
#we send our elgamal encoded cipher to chat
s.send(str.encode(cipher_string))
s.close()