prac 1
Sheth L.U. & M. V College
Practical no. 1
CODE:Python code for implementing Caesar Cipher
import tkinter as tk
from tkinter import messagebox
# Caesar Cipher encryption and decryption functions
def caesar_encrypt(text, shift):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) + shift - 97) % 26 + 97)
else:
result += char
return result
def caesar_decrypt(cipher_text, shift):
return caesar_encrypt(cipher_text, -shift)
# Function for the Encrypt button
def encrypt_text():
try:
shift = int(shift_entry.get())
text = input_text.get("1.0", tk.END).strip()
encrypted = caesar_encrypt(text, shift)
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, encrypted)
except ValueError:
messagebox.showerror("Invalid Input", "Shift must be an integer.")
# Function for the Decrypt button
def decrypt_text():
try:
shift = int(shift_entry.get())
text = input_text.get("1.0", tk.END).strip()
decrypted = caesar_decrypt(text, shift)
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, decrypted)
1
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
except ValueError:
messagebox.showerror("Invalid Input", "Shift must be an integer.")
# Create the main window
root = tk.Tk()
root.title("Caesar Cipher")
# Create and position the widgets
tk.Label(root, text="Input Text:").grid(row=0, column=0, padx=10, pady=10)
input_text = tk.Text(root, height=5, width=40)
input_text.grid(row=0, column=1, padx=10, pady=10)
tk.Label(root, text="Shift:").grid(row=1, column=0, padx=10, pady=10)
shift_entry = tk.Entry(root)
shift_entry.grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="Encrypt", command=encrypt_text).grid(row=2, column=0, padx=10, pady=10)
tk.Button(root, text="Decrypt", command=decrypt_text).grid(row=2, column=1, padx=10, pady=10)
tk.Label(root, text="Output Text:").grid(row=3, column=0, padx=10, pady=10)
output_text = tk.Text(root, height=5, width=40)
output_text.grid(row=3, column=1, padx=10, pady=10)
# Run the application
root.mainloop()
2
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
CODE: Python code for implementing Railfence Cipher
import tkinter as tk
from tkinter import messagebox
# Rail Fence Cipher encryption
def rail_fence_encrypt(text, key):
rail = [['\n' for i in range(len(text))] for j in range(key)]
# To find the direction of zigzag
dir_down = False
row, col = 0, 0
for i in range(len(text)):
if row == 0 or row == key - 1:
dir_down = not dir_down
rail[row][col] = text[i]
col += 1
if dir_down:
row += 1
else:
row -= 1
# Read the rail matrix to get the encrypted text
encrypted = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
encrypted.append(rail[i][j])
return ''.join(encrypted)
# Rail Fence Cipher decryption
def rail_fence_decrypt(cipher, key):
rail = [['\n' for i in range(len(cipher))] for j in range(key)]
dir_down = None
row, col = 0, 0
# Mark the positions that will have characters
for i in range(len(cipher)):
3
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
if row == 0 or row == key - 1:
dir_down = not dir_down
rail[row][col] = '*'
col += 1
if dir_down:
row += 1
else:
row -= 1
# Fill the rail matrix with the characters of the cipher
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
# Now read the matrix to get the decrypted text
result = []
row, col = 0, 0
for i in range(len(cipher)):
if row == 0 or row == key - 1:
dir_down = not dir_down
if rail[row][col] != '\n':
result.append(rail[row][col])
col += 1
if dir_down:
row += 1
else:
row -= 1
return ''.join(result)
# Function for the Encrypt button
def encrypt_text():
try:
key = int(key_entry.get())
text = input_text.get("1.0", tk.END).strip()
encrypted = rail_fence_encrypt(text, key)
output_text.delete("1.0", tk.END)
4
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
output_text.insert(tk.END, encrypted)
except ValueError:
messagebox.showerror("Invalid Input", "Key must be an integer.")
# Function for the Decrypt button
def decrypt_text():
try:
key = int(key_entry.get())
cipher = input_text.get("1.0", tk.END).strip()
decrypted = rail_fence_decrypt(cipher, key)
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, decrypted)
except ValueError:
messagebox.showerror("Invalid Input", "Key must be an integer.")
# Create the main window
root = tk.Tk()
root.title("Rail Fence Cipher")
# Create and position the widgets
tk.Label(root, text="Input Text:").grid(row=0, column=0, padx=10, pady=10)
input_text = tk.Text(root, height=5, width=40)
input_text.grid(row=0, column=1, padx=10, pady=10)
tk.Label(root, text="Key:").grid(row=1, column=0, padx=10, pady=10)
key_entry = tk.Entry(root)
key_entry.grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="Encrypt", command=encrypt_text).grid(row=2, column=0, padx=10, pady=10)
tk.Button(root, text="Decrypt", command=decrypt_text).grid(row=2, column=1, padx=10, pady=10)
tk.Label(root, text="Output Text:").grid(row=3, column=0, padx=10, pady=10)
output_text = tk.Text(root, height=5, width=40)
output_text.grid(row=3, column=1, padx=10, pady=10)
# Run the application
root.mainloop()
OUTPUT:
5
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
6
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Practical No. 2
CODE:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import binascii
# Generate RSA key pair (1024 bits)
keyPair = RSA.generate(1024)
# Get public key
pubKey = keyPair.publickey()
# Print public key values (n and e)
print(f"Public key: (n={hex(pubKey.n)}, e={hex(pubKey.e)})")
# Export and print public key in PEM format
pubKeyPEM = pubKey.exportKey()
print(pubKeyPEM.decode('ascii'))
# Print private key values (n and d)
print(f"Private key: (n={hex(pubKey.n)}, d={hex(keyPair.d)})")
# Export and print private key in PEM format
privKeyPEM = keyPair.exportKey()
print(privKeyPEM.decode('ascii'))
# Encryption
msg = 'Ismile Academy'.encode('utf-8') # Convert string to bytes for encryption
encryptor = PKCS1_OAEP.new(pubKey)
encrypted = encryptor.encrypt(msg)
# Print encrypted message in hexadecimal format
print("Encrypted:", binascii.hexlify(encrypted).decode('ascii'))
7
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
OUTPUT:
8
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Practical No. 3
CODE:Python code for implementing MD5 Algorithm
import hashlib
# Create MD5 hash objects for both 'Ismile' and 'Esmile'
result = hashlib.md5(b'Ismile')
result1 = hashlib.md5(b'Esmile')
# Printing the hash in hexadecimal format
print("The MD5 hash of 'Ismile' is: ", result.hexdigest())
print("The MD5 hash of 'Esmile' is: ", result1.hexdigest())
OUTPUT:
CODE:Python code for implementing SHA Algorithm
import hashlib # Corrected import statement
# Get user input
input_value = input("Enter the value to encode: ").strip()
9
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
# Use a different variable name
# Calculate the SHA-1 hash
result = hashlib.sha1(input_value.encode())
# Print the hexadecimal equivalent of SHA-1
print("The hexadecimal equivalent of SHA-1 is: ", result.hexdigest())
OUTPUT:
10
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Practical No. 4
CODE:
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes # Use this for random number generation
def generate_signature(private_key, message):
# Load the private key
key = RSA.import_key(private_key)
# Generate SHA-256 hash of the message
hashed_message = SHA256.new(message.encode('utf-8'))
# Create a signature using the private key
signer = PKCS1_v1_5.new(key)
signature = signer.sign(hashed_message)
return signature
def verify_signature(public_key, message, signature):
# Load the public key
key = RSA.import_key(public_key)
# Generate SHA-256 hash of the message
hashed_message = SHA256.new(message.encode('utf-8'))
# Verify the signature using the public key
verifier = PKCS1_v1_5.new(key)
return verifier.verify(hashed_message, signature)
# Generate RSA key pair
key_pair = RSA.generate(2048)
# Extract public and private keys
public_key = key_pair.publickey().export_key()
private_key = key_pair.export_key()
# Example usage
message = "Hello, World!"
# Generate a digital signature
signature = generate_signature(private_key, message)
print("Generated Signature:", signature.hex()) # Display signature in hex format
# Verify the digital signature
is_valid = verify_signature(public_key, message, signature)
print("Signature Verification Result:", is_valid)
11
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
OUTPUT:
12
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Practical No. 5
CODE:
from random import randint
if __name__ == '__main__':
# Publicly known values (prime P and base G)
P = 23 # Prime number
G = 9 # Primitive root modulo P (base)
print('The Value of P is: %d' % (P))
print('The Value of G is: %d' % (G))
# Alice chooses her private key (a)
a=4
print('Secret Number for Alice is: %d' % (a))
# Alice calculates her public key (x)
x = int(pow(G, a, P)) # (G^a) mod P
print('Alice\'s public key (G^a mod P) is: %d' % (x))
# Bob chooses his private key (b)
b=6
print('Secret Number for Bob is: %d' % (b))
# Bob calculates his public key (y)
y = int(pow(G, b, P)) # (G^b) mod P
print('Bob\'s public key (G^b mod P) is: %d' % (y))
# Alice computes the shared secret key using Bob's public key (y) and her private key (a)
ka = int(pow(y, a, P)) # (y^a) mod P
print('Secret key for Alice is: %d' % (ka))
# Bob computes the shared secret key using Alice's public key (x) and his private key (b)
kb = int(pow(x, b, P)) # (x^b) mod P
print('Secret key for Bob is: %d' % (kb))
# Both keys (ka and kb) should be the same if Diffie-Hellman works correctly
OUTPUT:
13
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
14
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Practical No. 6
CODE:
Enter the following command in the CLI mode of Router1
Router(config)#ip route 0.0.0.0 0.0.0.0 20.0.0.2
Router(config)#hostname R1
R1(config)#exit
R1#show version
R1#
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#
R1(config)#license boot module c1900 technology-package securityk9
R1(config)#exit
R1#
R1#copy run startup-config
R1#reload
R1>enable
R1#show version
Enter the following command in the CLI mode of Router2
Router(config)#ip route 0.0.0.0 0.0.0.0 30.0.0.2
Router(config)#hostname R2
R2(config)#exit
R2#show version
R2#
R2#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#
R2(config)#license boot module c1900 technology-package securityk9
R2(config)#exit
R2#
R2#copy run startup-config
R2#reload
R2>enable
R2#show version
(The security package is enabled)
15
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
Enter the following command in the CLI mode of Router0
Router>enable
Router#configure terminal
Router(config)#hostname R0
R0(config)#
Defining the Hostname for all Routers and Configuring the Routers R1 and R2 for IPSec
VPN tunnel R1#configure terminal
R1(config)#access-list 100 permit ip 192.168.1.0 0.0.0.255 192.168.2.0
0.0.0.255 R1(config)#crypto isakmp policy 10
R1(config-isakmp)#encryption aes 256
R1(config-isakmp)#authentication pre-share
R1(config-isakmp)#group 5
R1(config-isakmp)#exit
R1(config)#crypto isakmp key ismile address 30.0.0.1
R1(config)#crypto ipsec transform-set R1->R2 esp-aes 256 esp-sha-hmac
R1(config)#
R2#
R2#configure terminal
R2(config)#access-list 100 permit ip 192.168.2.0 0.0.0.255 192.168.1.0
0.0.0.255 R2(config)#crypto isakmp policy 10
R2(config-isakmp)#encryption aes 256
R2(config-isakmp)#authentication pre-share
R2(config-isakmp)#group 5
R2(config-isakmp)#exit
R2(config)#crypto isakmp key ismile address 20.0.0.1
R2(config)#crypto ipsec transform-set R2->R1 esp-aes 256 esp-sha-hmac
R2(config)#
R1>enable
R1#configure terminal
R1(config)#crypto map IPSEC-MAP 10 ipsec-isakmp
R1(config-crypto-map)#set peer 30.0.0.1
R1(config-crypto-map)#set pfs group5
R1(config-crypto-map)#set security-association lifetime seconds 86400
R1(config-crypto-map)#set transform-set R1->R2
R1(config-crypto-map)#match address 100
R1(config-crypto-map)#exit
R1(config)#interface g0/0
R1(config-if)#crypto map IPSEC-MAP
16
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
R2>enable
R2#configure terminal
R2(config)#crypto map IPSEC-MAP 10 ipsec-isakmp
R2(config-crypto-map)#set peer 20.0.0.1
R2(config-crypto-map)#set pfs group5
R2(config-crypto-map)#set security-association lifetime seconds 86400
R2(config-crypto-map)#set transform-set R2->R1
R2(config-crypto-map)#match address 100
R2(config-crypto-map)#exit
R2(config)#interface g0/0
R2(config-if)#crypto map IPSEC-MAP
OUTPUT:
17
S111 Sarah Raviraj Shriyan
Sheth L.U. & M. V College
18
S111 Sarah Raviraj Shriyan