Network - Security LAB-3
Network - Security LAB-3
Network - Security LAB-3
def power(a,b,P):
if (b == 1):
return a;
else:
return ((pow(a, b)) % P);
P = int(input("Enter the prime number :"))
print("The value of P :", P)
G = int(input("Enter the primitve root for pervious prime number :"))
print("The value of G :", G)
a = int(input("Enter the chosen private key A :"))
print("The private key a :", a)
x = power(G, a, P)
b = int(input("Enter the chosen private key B :"))
print("The private key b :", b)
y = power(G, b, P)
ka = power(y, a, P)
kb = power(x, b, P)
print("Secret key for a is :", ka)
print("Secret Key for b is :", kb)
2. Encrypted communication over socket using AES
Server Code:
import base64
import hashlib
import socket
class AESCipher(object):
self.bs = AES.block_size
self.key = hashlib.sha256(key.encode()).digest()
raw = self._pad(raw)
iv = Random.new().read(AES.block_size)
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
return
self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
def _pad(self, s):
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
string="Pabba Sumanth"
password="djknBDS89dHFS(*HFSD())"
enc_string=str(AESCipher(password).encrypt(string))
enc_string_2=AESCipher(password).encrypt(string)
dec_string=str(AESCipher(password).decrypt(enc_string_2))
sock.bind(('10.0.2.15', 4444))
sock.listen(5)
conn.send(enc_string_2)
conn.close()
Client Code:
import socket
import base64
import hashlib
sock.connect(('10.0.2.15', 4444))
class AESCipher(object):
self.bs = AES.block_size
self.key = hashlib.sha256(key.encode()).digest()
raw = self._pad(raw)
iv = Random.new().read(AES.block_size)
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-
8')
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
password="djknBDS89dHFS(*HFSD())"
data=sock.recv(4096)
decoded=AESCipher(password).decrypt(data.decode('utf-8'))
print(str(decoded))
sock.close()
Output:
Session Key establishment using RSA
Server code:
import socket
import math
import itertools as it
if __name__ == '__main__':
key=222
svr = socket.socket()
host = socket.gethostname()
port = 5544
svr.bind((host, port))
svr.listen(1)
while 1:
r_msg = svr.recv(1024).decode()
k = r_msg.split('~')
#print(k)
session_key=int(k[1])
rec_res=k[0]
svr.send(encrypted_text.encode())
import random
import math
import socket
class RSA:
def __init__(self):
self.key = namedtuple("Key",["modulus","exponent"])
def generateKeys(self):
# two distinct prime numbers p and q
p = self.__findPrime()
q = self.__findPrime()
n = p * q
phi = (p - 1) * (q - 1)
e = self.__findExponent(phi)
d = self.__multiplicativeInverse(e,phi)
publicKey = self.key(n,e)
privateKey = self.key(n,d)
def encrypt(self,message,key):
def decrypt(self,message,key):
def __isPrime(self,n):
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
# other numbers
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def __findPrime(self):
n = random.randrange(1024)
while(self.__isPrime(n) == False):
n = random.randrange(1024)
return n
def __gcd(self,a,b):
temp = a
# switch a for b so that
# a is always greater b
if b > a:
a = b
b = temp
while(temp):
temp = a % b
a = b
b = temp
return a
def __findExponent(self,phi):
e = random.randrange(2,phi)
while(self.__gcd(phi,e) != 1):
e = random.randrange(2,phi)
return e
def __multiplicativeInverse(self,value,modulus):
coef = 1
coefDelay = 0
valueDelay = modulus
while(value):
quotient = valueDelay//value
if coefDelay < 0:
coefDelay += modulus
return coefDelay
def __squareMultiply(self,base,power):
if power < 0:
if power == 0:
return 1
if power == 1:
return base
if power % 2:
return self.__squareMultiply(base*base,power/2)
if __name__ == '__main__':
s = socket.socket()
host = 'DESKTOP-DVGT262'
port = 5544
try:
s.connect((host, port))
print("connected to server")
except:
rsa = RSA()
while 1:
print("\n*****************************\n")
session_key=rsa.encrypt(raw_session_key,public_key)
s.send(encrypted_text.encode())
r_msg = s.recv(1024).decode()
k=r_msg.split('~')
rec_sk=int(k[1])
dec=rsa.decrypt(rec_sk, private_key)
if(dec==raw_session_key):
Output: