LAB6
LAB6
LAB ASSIGNMENT 5
PREETHA SIVAKUMAR 110127240 & NAZHATH NAFIZZA NAZEERUDDIN 110126014
Question 1: Use openssl to generate RSA public/private key. This will generate a
rsa instance (p, q, d, e, n) with p, q of 1024 bits and to prevent leaking the private
key, the output private.pem is encrypted by aes128 cipher with password you will
be prompted to provide. Now use the above command to generate a rsa private key
and save it in file private.pem. Then, extract the public key (e, n) in a file
public.pem: $ openssl rsa –in private.pem –pubout >public.pem
Display private key using $openssl rsa –in private.pem –text -noout
Display public key using $openssl rsa –in public.pem –pubin –text -noout
Question 2:
a) Encrypt messages using PKCS1_OAEP, which is an implementation of RSA. Use the key RsaKey
derived above to do the encryption. The functions are described as follow.
• Cipher=PKCS1_OAEP.new(RsaKey): o For the encryption, RsaKey is a public-key. Return an
encryption object Cipher.
• Cipher.encrypt(message): This returns ciphertext of message (byte string) under encryption
object Cipher.
Code Encrypt_RSA.py:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import binascii
# Message to be encrypted
message = b"PREETHA SIVAKUMAR 110127240"
Question 4:
In this problem, you will use Diffie-Hellman with authentication to protect the client-server
communication. Implement the following functionalities.
a. Create two files: TCP client and TCP server, capable to chat with each other using socket.
b. Client and Server execute Diffie-Hellman to generate a shared key and use sha256 to hash this
shared key to 32-byte secret sk. Diffie-Hellman uses parameters:
p=258224987808690858965591917200301187432970579282922351283065935654064762201
6841194629645353280137831435903171972747559779 g=2 Note: x, y in Diffie-Hellman can be
obtained with Crypto.Random.random.getrandbits(400); see
https://pycryptodome.readthedocs.io/en/latest/src/random/random.html if necessary.
c. Sender (Client or Server) uses sk as a secret key of AES to encrypt your chat message in (a).
This results in ciphertext C and computes tag=sha256(C). In (a), sender sends (C, tag), instead of
plain chat message.
d. At the receiver, when receiving (C, tag), verify whether tag=sha256(C) holds. If it fails, raise
exception; otherwise, use sk as the AES secret to decrypt C. This will recover your chat message.
Paste your client and server programs in your submission file. Print out sk, C, tag and decrypted
chat message in (d) for one chat message
import socket
from Crypto.Random import get_random_bytes
from Crypto.Random.random import getrandbits
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Hash import SHA256
from Crypto.Util import number
# Diffie-Hellman parameters
p=
2582249878086908589655919172003011874329705792829223512830659
356540647622016841194629645353280137831435903171972747559779
g=2
while True:
# Receive the message
response = conn.recv(4096)
if not response:
break
received_C = response[:-32]
received_tag = response[-32:]
conn.close()
server_socket.close()
# Diffie-Hellman parameters
p=
2582249878086908589655919172003011874329705792829223512830659
356540647622016841194629645353280137831435903171972747559779
g=2
while True:
# Get user input
message = input("Enter message: ")
received_C = response[:-32]
received_tag = response[-32:]
client_socket.close()
Server Terminal:
Client Terminal: