Isrecord (21211a05b5)
Isrecord (21211a05b5)
LAB RECORD
(III B. Tech. II SEM)
(Computer Science and Engineering)
B.V. RAJU INSTITUTE OF TECHNOLOGY
(UGC Autonomous, NBA and NAAC Accredited)
VISHNUPUR, NARSAPUR, MEDAK DIST.
www.bvrit.ac.in
CERTIFICATE
Date:
External Examiner
1
INDEX
2
S.No Experiment Name Page no Date of Date of signat
conduct submit ure
3
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-1
1. Implement the following Substitution Techniques
a) Ceaser Cipher
b) Playfair Cipher
1a) Ceaser Cipher:
AIM: Python program to implement Ceaser Cipher substitution Technique
def caesar_encrypt(text, shift):
"""Encrypts the given text using Caesar Cipher with the specified shift."""
encrypted_text = "
for char in text:
if char.isalpha():
shift_amount = 65 if char.isupper() else 97
encrypted_char = chr((ord(char) - shift_amount + shift) % 26 + shift_amount)
encrypted_text += encrypted_char
else:
encrypted_text += char
return encrypted_text
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
1b) Playfair cipher
AIM: Python program to implement Playfair Cipher substitution Technique
def generate_playfair_matrix(key):
key = ''.join(sorted(set(key), key=lambda x: key.index(x))) # Remove duplicates while
preserving order
key = key.replace('J', 'I') # Replace 'J' with 'I' to fit into the 5x5 matrix
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
matrix = [char for char in key if char in alphabet]
matrix.extend([char for char in alphabet if char not in matrix])
return [matrix[i * 5:(i + 1) * 5] for i in range(5)]
def preprocess_text(text):
text = text.upper().replace('J', 'I').replace(' ', '')
new_text = ''
i=0
while i < len(text):
new_text += text[i]
if i + 1 < len(text) and text[i] == text[i + 1]:
new_text += 'X'
elif i + 1 == len(text):
new_text += 'X'
else:
new_text += text[i + 1]
i += 1
i += 1
return new_text
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
for row in range(5):
for col in range(5):
if matrix[row][col] == char:
return row, col
return None
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-2
2. Implement the following Substitution Techniques
a) Hill Cipher
b) Vigenere Cipher
c) One-Time pad
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
return np.array(matrix)
def main():
# Get inputs from user
plain_text = input("Enter the text to be encrypted (A-Z only): ").upper().replace(' ', '')
key = input("Enter the key matrix as a single string (e.g., 'GYBNQKURP' for a 3x3 matrix):
").upper()
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
print("Invalid key length. The length of the key string should be a perfect square.")
return
if __name__ == "__main__":
main()
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
2b) Vigenere Cipher Program
AIM: Python program to implement Vigenere Cipher substitution Technique
def generate_key(text, key):
"""Generates the key in a cyclic manner until its length matches the length of the text."""
key = list(key)
if len(text) == len(key):
return "".join(key)
else:
for i in range(len(text) - len(key)):
key.append(key[i % len(key)])
return "".join(key)
# Driver code
if __name__ == "__main__":
plain_text = input("Enter the text to be encrypted: ")
key = input("Enter the key: ")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
2c) One-Time Pad Program
AIM: Python program to implement One-Time Pad substitution Technique
import random
import string
def generate_otp_key(length):
"""Generates a random key of specified length."""
return ''.join(random.choice(string.ascii_uppercase) for _ in range(length))
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-3
3. Implement the following Transposition Techniques
a) Rail fence
b) Row & Columnar transposition
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
def decryptRailFence(cipher, key):
rail = [['\n' for i in range(len(cipher))]
for j in range(key)]
dir_down = None
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
rail[row][col] = '*'
col += 1
if dir_down:
row += 1
else:
row -= 1
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
result = []
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
if __name__ == "__main__":
print(encryptRailFence("Sumanth", 2))
print(encryptRailFence("Luffy ", 3))
print(encryptRailFence("KingoftheNorth",3))
print(decryptRailFence("Smnhuat", 3))
print(decryptRailFence("Lyuf f", 2))
print(decryptRailFence("KoetigfhNrhnto", 3))
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
3b) Row & Columnar Program
AIM: Python program to implement Row & Columnar Transposition technique.
import math
def encryptMessage(msg, key):
cipher = ""
k_indx = 0
msg_len = float(len(msg))
msg_lst = list(msg)
key_lst = sorted(list(key))
col = len(key)
row = int(math.ceil(msg_len / col))
fill_null = int((row * col) - msg_len)
msg_lst.extend('_' * fill_null)
matrix = [msg_lst[i: i + col] for i in range(0, len(msg_lst), col)]
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
cipher += ''.join([row[curr_idx] for row in matrix])
k_indx += 1
return cipher
def decryptMessage(cipher, key):
msg = ""
k_indx = 0
msg_indx = 0
msg_len = float(len(cipher))
msg_lst = list(cipher)
col = len(key)
row = int(math.ceil(msg_len / col))
key_lst = sorted(list(key))
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
dec_cipher = []
for _ in range(row):
dec_cipher += [[None] * col]
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
for j in range(row):
dec_cipher[j][curr_idx] = msg_lst[msg_indx]
msg_indx += 1
k_indx += 1
try:
msg = ''.join(sum(dec_cipher, []))
except TypeError:
raise TypeError("This program cannot handle repeating words.")
null_count = msg.count('_')
if null_count > 0:
return msg[: -null_count]
return msg
# Driver Code
if __name__ == "__main__":
msg = input("Enter the message to encrypt: ")
key = input("Enter the encryption key: ")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
4b) AES Program
AIM: Python program to implement AES technique.
from Crypto.Cipher import AES
import hashlib
import base32hex
password = "Parvathi"
salt = b'\x28\xAB\xBC\xCD\xDE\xEF\x00\x33'
encode_string = base32hex.b32encode(ciphertext)
print("The encoded string is:", encode_string)
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-5
5. Implement the RC4 Stream cipher
AIM: Pyhton program to implement RC4 Stream cipher.
def KSA(key):
key_length = len(key)
S = list(range(256))
j=0
for i in range(256):
j = (j + S[i] + key[i % key_length]) % 256
S[i], S[j] = S[j], S[i] # Swap
return S
def PRGA(S):
i=0
j=0
while True:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i] # Swap
K = S[(S[i] + S[j]) % 256]
yield K
# Example usage:
key = input("Enter the encryption key: ")
plaintext = input("Enter the plaintext: ")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-6
6. Implement the following Asymmetric Key algorithms
a) RSA
b) Diffie-Hellman Key Exchange
6a) RSA Program
AIM: Pyhton program to implement RSA technique.
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
6b) Diffie-Hellman Key Exchange Program
AIM: Pyhton program to implement Diffie-Hellman Key Exchange technique.
def generate_shared_secret(p, g, private_key):
shared_secret = (g ** private_key) % p
return shared_secret
p = int(input("Enter the prime number (p): "))
g = int(input("Enter the base (g): "))
private_key_A = int(input("Enter Alice's private key: "))
private_key_B = int(input("Enter Bob's private key: "))
shared_secret_A = generate_shared_secret(p, g, private_key_A)
shared_secret_B = generate_shared_secret(p, g, private_key_B)
print("Alice's shared secret:", shared_secret_A)
print("Bob's shared secret:", shared_secret_B)
# Verifying the shared secret
if shared_secret_A == shared_secret_B:
print("Shared secrets match! Secure connection established.")
else:
print("Shared secrets do not match! Connection compromised.")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-7
7. Implement the following MAC / Hash function Cryptography algorithms
a) SHA-512
7a) SHA-512 Program
AIM: Pyhton program to implement SHA-512
import hashlib
def sha512_hash(text):
# Create a new sha512 hash object
sha512 = hashlib.sha512()
# Encode the input text to bytes, then update the hash object with it
sha512.update(text.encode('utf-8'))
# Get the hexadecimal representation of the digest
return sha512.hexdigest()
# Driver code
if __name__ == "__main__":
# Take input from user
input_text = input("Enter the text to hash: ")
# Get the SHA-512 hash of the input text
hash_result = sha512_hash(input_text)
# Print the resulting hash
print(f"SHA-512 hash of '{input_text}' is: {hash_result}")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-8
8. Implement the following MAC / Hash function Cryptography algorithms
a) HMAC
8a) HMAC Program
AIM: Pyhton program to implement HMAC
import hmac
import hashlib
import secrets
def generate_key(length=32):
return secrets.token_hex(length)
def create_hmac(message, key):
hmac_obj = hmac.new(key.encode(), message.encode(), hashlib.sha256)
return hmac_obj.hexdigest()
def verify_hmac(message, key, received_hmac):
hmac_obj = hmac.new(key.encode(), message.encode(), hashlib.sha256)
return hmac.compare_digest(hmac_obj.hexdigest(), received_hmac)
# Driver code
if __name__ == "__main__":
message = input("Enter the message: ")
key = generate_key()
print(f"Generated Key: {key}")
hmac_result = create_hmac(message, key)
print(f"HMAC: {hmac_result}")
received_hmac = input("Enter the received HMAC for verification: ")
if verify_hmac(message, key, received_hmac):
print("HMAC verification successful.")
else:
print("HMAC verification failed.")
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
INFORMATION SECURITY LAB DATE:
PAGE NO:
_____________________________________________________________________________________________
Week-9
9. Implement the signature scheme named Digital Signature Standard
AIM: Pyhton program to implement Digital Signature Standard
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
from cryptography.hazmat.primitives import serialization
# Function to generate DSA key pair
def generate_dsa_keys():
private_key = dsa.generate_private_key(key_size=2048)
public_key = private_key.public_key()
return private_key, public_key
_______________________________________________________________________________________________
Department of CSE BV Raju Institute of Technology III Year II Sem
Date:
Information Security Lab Page No:
Week 10
10. Analyze the IP packet using Wireshark and observe the AH & ESP
protocols
Aim: To analyze the IP packet using Wireshark and observe the AH & ESP
protocols
Wireshark is a free, open-source software application for capturing and analyzing network
packets. Wireshark can help users glean valuable insights about the network’s activity and
identify issues or threats by capturing and analyzing these packets.
You can select one or more of the network interfaces using shift+left-click. Once select the
network interface, you can start the capture, and there are several ways to do that.
Click the first button on the toolbar, titled “Start capturing packets.”
During the capture, Wireshark will show you the packets captured in real-time.
Once you have captured all the packets needed, use the same buttons or menu options to
stop the capture as you did to begin.
No.: This is the number order of the packet captured. The bracket indicates that this
packet is part of a conversation.
Time: This column shows how long after you started the capture this particular packet
was captured. You can change this value in the Settings menu to display a different
option.
Source: This is the address of the system that sent the packet.
Destination: This is the address of the packet destination.
Protocol: This is the type of packet. For example: TCP, DNS, DHCPv6, or ARP.
Length: This column shows you the packet’s length, measured in bytes.
Info: This column shows you more information about the packet contents, which will
vary depending on the type of packet.
Packet Details, the middle pane, shows you as much readable information about the packet
as possible, depending on the packet type. You can right-click and create filters based on the
highlighted text in this field.
The bottom pane, Packet Bytes, displays the packet exactly as it was captured in hexadecimal.
When looking at a packet that is part of a conversation, you can right-click the packet and
select Follow to see only the packets that are part of that conversation.
In the display filter bar on the screen, enter TCP and apply the filter.
From analyzing the menu in the menu bar select display filters or from capture
select capture filters and then TCP only and ok.
In the display filter bar on the screen, enter ESP and apply the filter.