-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
46 lines (35 loc) · 1.23 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
import socket
import pickle
import sys
from generate_key import *
# RSA functions (similar to the previous code)
# Other RSA functions (is_prime, gcd, mod_inverse, generate_keypair, encrypt, decrypt) as shown before
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Get the server machine name and port
host = socket.gethostname()
port = 12345
# Connect to the server
client_socket.connect((host, port))
# Receive the public key from server
public_key = pickle.loads(client_socket.recv(4096))
while True:
# Your message to be sent
# message = "Hello, RSA Encryption!"
message = input('enter your message: ')
if message == 'exit01':
# Close the socket
print("\n")
print('bye-bye server')
encrypted_message = encrypt(public_key, message)
client_socket.send(pickle.dumps(encrypted_message))
break
# Encrypt the message using the received public key
encrypted_message = encrypt(public_key, message)
print('Original Message:', message)
print('Encrypted Message:', encrypted_message)
print("\n")
# Send the encrypted message to the server
client_socket.send(pickle.dumps(encrypted_message))
client_socket.close()
sys.exit(0)