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

CNS Assignment 7

Uploaded by

Damini
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)
7 views5 pages

CNS Assignment 7

Uploaded by

Damini
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

CNS Assignment 7

Diffie Hellman

Client:-
#!/usr/bin/python3
import socket
from Crypto.Cipher import DES
from Crypto.Hash import MD5
# Diffie-Hellman parameters
g=5
p = 23
# Generate shared secret key
def generate_key(x, R):
key = (R**x) % p
key_str = str(key)
hash_obj = MD5.new(key_str.encode())
key_bytes = hash_obj.digest()[:8] # Use first 8 bytes as key
key_decimal = int.from_bytes(key_bytes, byteorder='big') # Convert bytes to
decimal
return key_decimal
# Perform DES encryption
def encrypt_message(key, message):
cipher = DES.new(key.to_bytes(8, byteorder='big'), DES.MODE_ECB)
block_size = cipher.block_size
padded_message = message.encode().rjust((len(message) // block_size + 1) *
block_size)
encrypted_message = cipher.encrypt(padded_message)
return encrypted_message
# Perform DES decryption
def decrypt_message(key, encrypted_message):
cipher = DES.new(key.to_bytes(8, byteorder='big'), DES.MODE_ECB)
decrypted_message = cipher.decrypt(encrypted_message)
return decrypted_message.decode().rstrip()
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
clientsocket.connect((host, port))
g=5
p = 23
print("g = 5, p = 23 (These values are public.)")
y = int(input("Enter y: "))
# Generate R2 and send it to the server
R2 = (g**y) % p
clientsocket.send(str(R2).encode())
# Receive R1 from the server
R1 = clientsocket.recv(2324)R1 = int(R1.decode())
print("Received: R1 =", R1)
# Generate shared secret key
K = generate_key(y, R1)
print("Shared secret key (decimal): K =", K)
# Encryption
message = input("Enter the message to encrypt: ")
encrypted_message = encrypt_message(K, message)
clientsocket.send(encrypted_message)
# Close the socket
clientsocket.close()

Server:-

import socket
from Crypto.Cipher import DES
from Crypto.Hash import MD5
# Diffie-Hellman parameters
g=5
p = 23
# Generate shared secret key
def generate_key(x, R):
key = (R**x) % p
key_str = str(key)
hash_obj = MD5.new(key_str.encode())
key_bytes = hash_obj.digest()[:8] # Use first 8 bytes as key
key_decimal = int.from_bytes(key_bytes, byteorder='big') # Convert bytes to
decimal
return key_decimal
# Perform DES encryption
def encrypt_message(key, message):
cipher = DES.new(key.to_bytes(8, byteorder='big'), DES.MODE_ECB)
block_size = cipher.block_size
padded_message = message.encode().rjust((len(message) // block_size + 1) *
block_size)
encrypted_message = cipher.encrypt(padded_message)
return encrypted_message
# Perform DES decryption
def decrypt_message(key, encrypted_message):
cipher = DES.new(key.to_bytes(8, byteorder='big'), DES.MODE_ECB)
decrypted_message = cipher.decrypt(encrypted_message)
return decrypted_message.decode().rstrip()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
serversocket.bind((host, port))
serversocket.listen(5)
print("Waiting for a client connection...")
clientsocket, addr = serversocket.accept()
print("Client connected:", addr)
g=5
p = 23
print("g = 5, p = 23 (These values are public.)")
x = int(input("Enter x: "))
# Receive R2 from the client
R2 = clientsocket.recv(2324)R2 = int(R2.decode())
print("Received: R2 =", R2)
# Generate R1 and send it to the client
R1 = (g**x) % p
clientsocket.send(str(R1).encode())
print("Sent: R1 =", R1)
# Generate shared secret key
K = generate_key(x, R2)
print("Shared secret key (decimal): K =", K)
# Receive encrypted message from the client
encrypted_message = clientsocket.recv(2324)
# Decrypt the message
decrypted_message = decrypt_message(K, encrypted_message)
print("Received encrypted message from client:", encrypted_message)
print("Decrypted message from client:", decrypted_message)
# Send a response back to the client
response = "Thank you for the message!"
encrypted_response = encrypt_message(K, response)
clientsocket.send(encrypted_response)
# Close the socket
clientsocket.close()

Output:-

You might also like