Client.py
import socket
HOST = '127.0.0.1'
PORT = 65432
ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789.'
message = 'This message is encrypted using Caesar cipher'
def encryptstr(message,key):
#creating a list for new letters
newletters = []
#loop to assign new letter value
for letter in message:
uppercase = letter.isupper()
letter = letter.lower()
#checking if the letter is upper or lower
if letter in ALPHABET:
index = ALPHABET.find(letter)
newindex = (index key) % len(ALPHABET)
letter = ALPHABET[newindex]
if uppercase:
letter = letter.upper()
newletters.append(letter)
#joining the list
return ''.join(newletters)
def findkey():
return 10
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
test = encryptstr(message, findkey())
s.sendall(test.encode()) # How to send key of 10 and Message?
data = s.recv(1024)
print('Received', data.decode())
Server.py
import socket
HOST = '127.0.0.1'
PORT = 65432
ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789.'
def decryptstr(message,key):
#creating a list for new letters
newletters = []
#loop to assign new letter value
for letter in message:
uppercase = letter.isupper()
letter = letter.lower()
#checking if the letter is upper or lower
if letter in ALPHABET:
index = ALPHABET.find(letter)
newindex = (index - key) % len(ALPHABET)
letter = ALPHABET[newindex]
if uppercase:
letter = letter.upper()
newletters.append(letter)
#joining the list
return ''.join(newletters)
def findkey():
return 10
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
message = str(data.decode())
test = decryptstr(message, findkey())
print(test)
if not data:
break
conn.sendall(data)
What I am trying to do with this current code is from the server. I wish to send the variable of "10" to the client. From the client to the server, I would like to send back two variables, one being "10" and the other being the encrypted message. After both are sent, I would like the server to decrypt the message and print it on the screen.
You may be wondering why I would send the ten back and forth, and this is because I will be incorporating this Diffie-Hellman Example with the def findkeys()
, Thank you for your time!
CodePudding user response:
client ...
sock.send(b'10' encrypted_message)
server
msg = sock.recv(1024)
code = msg[:2]
encrypted_payload = msg[2:]
Is this what you mean?
CodePudding user response:
You can send that var in json instead of single var it could help you to send more then 1 data at same time