Is Ex - 1 - To - 10
Is Ex - 1 - To - 10
Practical : - 1
1
IS(3170720)
211260107049
# Perform encryption
encrypted_message = encrypt(message, shift_value)
print(f"Encrypted Message: {encrypted_message}")
# Perform decryption
decrypted_message = decrypt(encrypted_message, shift_value)
print(f"Decrypted Message: {decrypted_message}")
Output:-
2
IS(3170720)
211260107049
Practical : - 2
ciphertext += cipher_alphabet[index]
else:
ciphertext += char # Non-alphabet characters are added as is
return ciphertext
def decrypt(ciphertext, cipher_alphabet):
alphabet = string.ascii_lowercase
plaintext = ""
for char in ciphertext:
if char in cipher_alphabet:
index = cipher_alphabet.index(char)
plaintext += alphabet[index]
else:
plaintext += char # Non-alphabet characters are added as is
return plaintext
# Example usage
key = input("Enter the key for the cipher (a word or phrase): ").replace(" ", "")
plaintext = input("Enter the message to encrypt: ")
cipher_alphabet = generate_cipher_alphabet(key)
encrypted_message = encrypt(plaintext, cipher_alphabet)
print(f"Encrypted Message: {encrypted_message}")
decrypted_message = decrypt(encrypted_message, cipher_alphabet)
print(f"Decrypted Message: {decrypted_message}")
Output:-
4
IS(3170720)
211260107049
Practical : - 3
6
IS(3170720)
211260107049
Output:-
7
IS(3170720)
211260107049
Practical : - 4
9
IS(3170720)
211260107049
pair = formatted_message[i:i+2]
ciphertext += encrypt_pair(pair, matrix)
return ciphertext
# Main program to take input from user
key = input("Enter the keyword: ")
message = input("Enter the message to encrypt: ")
matrix = create_matrix(key)
ciphertext = encrypt_message(message, matrix)
print("\nPlayfair Cipher Key Matrix:")
for row in matrix:
print(" ".join(row))
print("\nFormatted Message:", format_message(message))
print("Encrypted Message:", ciphertext)
Output:-
10
IS(3170720)
211260107049
Practical : - 5
11
IS(3170720)
211260107049
12
IS(3170720)
211260107049
print(f"Error: {e}")
if name == " main ":
main()
Output:-
13
IS(3170720)
211260107049
Practical : - 6
14
IS(3170720)
211260107049
if rails <= 1:
raise ValueError("Number of rails must be greater than 1.")
num_cols = len(ciphertext)
rail = [['' for _ in range(num_cols)] for _ in range(rails)]
# Reconstruct the rail fence grid with placeholders
row, step = 0, 1
for i in range(num_cols):
rail[row][i] = '*'
if row == 0 or row == rails - 1:
step = -step
row += step
# Fill the rail fence grid with ciphertext characters
index = 0
for i in range(rails):
for j in range(num_cols):
if rail[i][j] == '*':
rail[i][j] = ciphertext[index]
index += 1
# Read off the grid in zigzag pattern to get the plaintext
result = []
row, step = 0, 1
for i in range(num_cols):
result.append(rail[row][i])
if row == 0 or row == rails - 1:
step = -step
row += step
return ''.join(result)
15
IS(3170720)
211260107049
def main():
message = input("Enter the plaintext message: ")
rails = int(input("Enter the number of rails: "))
if rails < 2:
print("Number of rails must be 2 or more.")
return
print("\nEncrypting...")
try:
ciphertext = rail_fence_encrypt(message, rails)
print("Encrypted Message:", ciphertext)
print("\nDecrypting...")
decrypted_message = rail_fence_decrypt(ciphertext, rails)
print("Decrypted Message:", decrypted_message)
except Exception as e:
print(f"ERROR! {e}")
if name == " main ":
main()
Output:-
16
IS(3170720)
211260107049
Practical : - 7
return True
# Function to compute modular inverse using the Extended Euclidean Algorithm
def mod_inverse(e, phi):
t, newt = 0, 1
r, newr = phi, e
while newr != 0:
quotient = r // newr
t, newt = newt, t - quotient * newt
r, newr = newr, r - quotient * newr
if r > 1:
raise ValueError("e is not invertible")
if t < 0:
t = t + phi
return t
# RSA Key Generation
def generate_keys():
# Step 1: Select two large prime numbers p and q
p = generate_large_prime()
q = generate_large_prime()
print(f"Selected primes: p = {p}, q = {q}")
# Step 2: Compute n = p * q
n=p*q
# Step 3: Compute Euler's Totient function phi(n)
phi_n = (p - 1) * (q - 1)
# Step 4: Choose e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1
e = random.randrange(2, phi_n)
while gcd(e, phi_n) != 1:
18
IS(3170720)
211260107049
e = random.randrange(2, phi_n)
print(f"Public exponent e = {e}")
# Step 5: Compute d, the modular multiplicative inverse of e modulo phi(n)
d = mod_inverse(e, phi_n)
print(f"Private exponent d = {d}")
# Public key: (e, n), Private key: (d, n)
return (e, n), (d, n)
# RSA Encryption: c = m^e mod n
def encrypt(plaintext, public_key):
e, n = public_key
ciphertext = [pow(ord(char), e, n) for char in plaintext]
return ciphertext
# RSA Decryption: m = c^d mod n
def decrypt(ciphertext, private_key):
d, n = private_key
decrypted_text = ''.join([chr(pow(char, d, n)) for char in ciphertext])
return decrypted_text
# Driver code
if name == ' main ':
# Key generation
public_key, private_key = generate_keys()
print(f"Public Key: {public_key}")
print(f"Private Key: {private_key}")
# Plaintext message
message = input("Enter the message to encrypt: ")
# Encrypt the message
encrypted_message = encrypt(message, public_key)
19
IS(3170720)
211260107049
Output:-
20
IS(3170720)
211260107049
Practical : - 8
# Driver code
if name == ' main ':
diffie_hellman_key_exchange()
22
IS(3170720)
211260107049
Output:-
23
IS(3170720)
211260107049
Practical : - 9
24
IS(3170720)
211260107049
Example:
Vigenere Cipher:
Key: D
25
IS(3170720)
211260107049
Hill cipher:
Monoalphabetic Cipher:
C=( P + K ) mod 26
(‘P’ is the character in plain text, ‘K’ is the key, and ‘C’ is the Cipher.)
Example:
26
IS(3170720)
211260107049
Key: LABCDEFGHIJKMNOPQRSTUVWXYZ
27
IS(3170720)
211260107049
Practical : - 10
AIM: Study and use the wires hark for the various network Protocols
EXCERCISE:
Explain Installation and working of Wireshak for Windows?
Ans:-
Wireshark is software that is widely used in the analysis of data packets in a
network. Wireshark is completely free and open source. This packet analyzer is
used for a variety of purposes like troubleshooting networks, understanding
communication between two systems, developing new protocols, etc. The
original name of Wireshark was Ethereal which was changed in 2006 due to some
company’s copyright issues. This software is written in C and C++, and its initial
release was in the year 1998. Its latest release is 3.6.0 which got released on 22
November 2021. Wireshark is a cross-platform software, it can be run on Linux,
windows, mac, and any other operating system.
Installing Wireshark on Windows:
Follow the below steps to install Wireshark on Windows:
Step 1: Visit the official Wireshark website using any web browser.
28
IS(3170720)
211260107049
Step 4: Now check for the executable file in downloads in your system and
run it.
29
IS(3170720)
211260107049
30
IS(3170720)
211260107049
Step 8: This screen is for choosing components, all components are already
marked so don’t change anything just click on the Next button.
Step 9: This screen is of choosing shortcuts like start menu or desktop icon
along with file extensions which can be intercepted by Wireshark, tick all
boxes and click on Next button.
Step 10: The next screen will be of installing location so choose the drive
which will have sufficient memory space for installation. It needed only a
memory space of 223.4 MB.
31
IS(3170720)
211260107049
Step 11: Next screen has an option to install Npcap which is used with
Wireshark to capture packets pcap means packet capture so the install
option is already checked don’t change anything and click the next button.
Step 12: Next screen is about USB network capturing so it is one’s choice to
use it or not, click on Install.
32
IS(3170720)
211260107049
Step 14: This installation will prompt for Npcap installation as already
checked so the license agreement of Npcap will appear to click on the I
Agree button.
Step 15: Next screen is about different installing options of npcap, don’t do
anything click on Install.
Step 16: After this installation process will start which will take only a
minute.
Step 17: After this installation process will complete click on the Next
button.
33
IS(3170720)
211260107049
Step 19: After this installation process of Wireshark will complete click on
the Next button.
34
IS(3170720)
211260107049
35
IS(3170720)