0% found this document useful (0 votes)
28 views5 pages

Client and Server Without Encryption

Uploaded by

uzairch514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Client and Server Without Encryption

Uploaded by

uzairch514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Client And Server Without Encryption:

Server Code :
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ip_address = '127.0.0.1'

port_num = 12345

s.bind((ip_address, port_num))

s.listen(3)

print("Server is Listening...")

client_socket, client_address = s.accept()

while True:

msg = client_socket.recv(1024).decode()

if msg.lower() == 'quit':

print("Client has disconnected.")

break

print(f"Message from client: {msg}")

s.close()

Client Code “:………….


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ip_address = '127.0.0.1'

port_num = 12345
s.connect((ip_address, port_num))

while True:

msg = input("Enter message (type 'quit' to exit): ")

s.send(msg.encode())

if msg.lower() == 'quit':

break

s.close()
Client And Server With AES Encryption:
SERVER CODE :

import socket

from Crypto.Cipher import AES

from Crypto.Util.Padding import unpad

AES_KEY = b'231330 byte key!'

IV = b'231330 byte IV!!'

def decrypt_message(encrypted_message):

cipher = AES.new(AES_KEY, AES.MODE_CBC, IV)

decrypted_message = unpad(cipher.decrypt(encrypted_message), AES.block>

return decrypted_message.decode()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ip_address = '127.0.0.1'

port_num = 1234

s.bind((ip_address, port_num))

s.listen(5)

print("Server is listening:")

client_socket, client_address = s.accept()

print(" Client Connected: ", client_address)

while True:

encrypted_msg = client_socket.recv(1024)

msg = decrypt_message(encrypted_msg)
if msg.lower() == 'quit':

print("Client has disconnected.")

break

print(f"Message from client: {msg}")

s.close()

Client CODE:
import socket

from Crypto.Cipher import AES

from Crypto.Util.Padding import pad

AES_KEY = b'231330 byte key!'

IV = b'231330 byte IV!!'

def encrypt_message(message):

cipher = AES.new(AES_KEY, AES.MODE_CBC, IV)

encrypted_message = cipher.encrypt(pad(message.encode(), AES.block_siz>

return encrypted_message

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ip_address = '127.0.0.1'

port_num = 1234

s.connect((ip_address, port_num))

while True:
msg = input("Enter message (type 'quit' to exit): ")

encrypted_msg = encrypt_message(msg)

s.send(encrypted_msg)

if msg.lower() == 'quit':

break

s.close()

You might also like