Untitled

 avatar
unknown
plain_text
16 days ago
17 kB
4
Indexable
Mahatma Education Society’s
Pillai College of Arts, Commerce & Science
(Autonomous)
Affiliated to University of Mumbai
NAAC Accredited 'A' grade (3 cycles) Best College Award by University of Mumbai
ISO 9001:2015 Certified




           CERTIFICATE    



This is to certify that Mr. _      Amit Pandey____ of M.Sc. D.A. Part-I Semester II has completed the practical work in the Subject of   Network Security & Cryptography  during the academic year 2024-25 under the guidance of Prof. __Simran Shinde__being the partial requirement for the fulfillment of the curriculum of Degree of Master of Science in Data Analytics, University of Mumbai. 




Place: 

Date: 





Name & Signature of faculty                                   Name & Signature of external



INDEX

Practical No.
Aim
Date
Sign
1.
Write programs to implement the following Substitution Cipher Techniques: - 
1. Caesar Cipher - Encryption and Decryption
29|11|2024


2.
Write programs to implement the following Substitution Cipher Techniques:
1. Playfair Cipher
2. Vernam Cipher
29|11|2024


3.
Write programs to implement the following Transposition Cipher Techniques:
1. Rail Fence Cipher
2. Simple Columnar Technique
23|12|2024


4.
Write a program to encrypt and decrypt strings using DES Algorithm.
31|01|2025


5.
Write a program to encrypt and decrypt strings using AES Algorithm.
31|01|2025


6.
Write a program to implement the Diffie-Hellman Key Agreement algorithm to generate symmetric keys.
06|02|2025


7.
Write a program to implement the MD5 algorithm to compute the message digest.
03|03|2025


8.
Write a program to calculate HMAC-SHA1 
Signature.
03|03|2025


9.
Write a python program to illustrate Elgamal encryption.
03|03|2025




















PRACTICAL 1

Aim: Write a program to implement the following Substitution Cipher Techniques.

1. Caesar Cipher(Encryption and Decryption)

Code :
def caesar_cipher(text, shift):
    result = ''
    for char in text:
        if char.isalpha():
            start = ord('a') if char.islower() else ord('A')
            shifted_char = chr((ord(char) - start + shift) % 26 + start)
        elif char.isdigit():
            shifted_char = str((int(char) + shift) % 10)
        else:
            shifted_char = char
        result += shifted_char
    return result
# Example usage:
text = input("Enter the text to be encrypted: ")
key = 4
encrypted_text = caesar_cipher(text, key)
print("Encrypted:", encrypted_text)


decrypted_text = caesar_cipher(encrypted_text, -key)
print("Decrypted:", decrypted_text)

Output :













PRACTICAL 2

Aim: Write a program to implement the following Substitution Cipher Techniques.

1. Playfair Cipher

Code :
import string


def prepare_text(text, fill_char='X'):
    text = ''.join(c for c in text.upper().replace('J', 'I') if c in string.ascii_uppercase)
    prepared_text = ""
    for i in range(len(text)):
        prepared_text += text[i]
        if i + 1 < len(text) and text[i] == text[i + 1]:
            prepared_text += fill_char
    if len(prepared_text) % 2 != 0:
        prepared_text += fill_char
    return prepared_text


def create_key_matrix(key):
    key = ''.join(c for c in key.upper().replace('J', 'I') if c in string.ascii_uppercase)
    key = ''.join(sorted(set(key), key=key.index))  # Remove duplicates while preserving order
    alphabet = string.ascii_uppercase.replace('J', '')
    return [list(key + ''.join(c for c in alphabet if c not in key))[i:i + 5] for i in range(0, 25, 5)]


def find_position(matrix, char):
    for r, row in enumerate(matrix):
        if char in row:
            return r, row.index(char)


def playfair(text, matrix, decrypt=False):
    text = prepare_text(text)
    shift = -1 if decrypt else 1
    result = ""
    for i in range(0, len(text), 2):
        r1, c1 = find_position(matrix, text[i])
        r2, c2 = find_position(matrix, text[i + 1])
        if r1 == r2:
            result += matrix[r1][(c1 + shift) % 5] + matrix[r2][(c2 + shift) % 5]
        elif c1 == c2:
            result += matrix[(r1 + shift) % 5][c1] + matrix[(r2 + shift) % 5][c2]
        else:
            result += matrix[r1][c2] + matrix[r2][c1]
    return result


# Main program
print("=== Playfair Cipher ===")


# Input key
key = input("Enter the key (text used for generating cipher matrix): ").strip()


# Generate key matrix
try:
    cipher_matrix = create_key_matrix(key)
except ValueError:
    print("Error: Invalid key provided.")
    exit()


print("\nKey Matrix:")
for row in cipher_matrix:
    print(row)


# Input plaintext
plaintext = input("\nEnter the plaintext to encrypt: ").strip()


# Encrypt
encrypted = playfair(plaintext, cipher_matrix)
print("\nEncrypted Text:", encrypted)


# Decrypt
decrypted = playfair(encrypted, cipher_matrix, decrypt=True)
print("\nDecrypted Text:", decrypted)












Output :




































2. Vernam Cipher

Code :
import string


def vernam_cipher(text, key):
    """
    Encrypt or decrypt text using Vernam cipher with a key.
    """
    if len(text) != len(key):
        raise ValueError("Key must be the same length as the plaintext!")
   
    # Perform XOR operation
    result = ''.join(chr(ord(t) ^ ord(k)) for t, k in zip(text, key))
    return result


def text_to_hex(text):
    """
    Convert text to a readable hexadecimal representation.
    """
    return ' '.join(f"{ord(c):02X}" for c in text)


# Main program
print("=== Vernam Cipher ===")


# Input plaintext
plaintext = input("Enter the plaintext: ").strip()
plaintext = ''.join(c for c in plaintext if c in string.printable)  # Sanitize input


# Input key
key = input("Enter the key (must be the same length as the plaintext): ").strip()
key = ''.join(c for c in key if c in string.printable)  # Sanitize input


if len(plaintext) != len(key):
    print("Error: Key length must match plaintext length!")
else:
    # Encrypt
    encrypted_text = vernam_cipher(plaintext, key)
    encrypted_hex = text_to_hex(encrypted_text)
    print("\nEncrypted Text (non-readable ASCII):", encrypted_text)
    print("Encrypted Text (hexadecimal):", encrypted_hex)


    # Decrypt (using the same function as XOR is reversible)
    decrypted_text = vernam_cipher(encrypted_text, key)
    print("\nDecrypted Text:", decrypted_text)

Output :




































PRACTICAL 3

Aim: Write a program to implement the following Transposition Cipher Techniques.

1. Rail Fence Cipher

Code :
def rail_fence_encrypt(text, key):
    fence = [['' for _ in range(len(text))] for _ in range(key)]
    direction = 1
    row, col = 0, 0
    for char in text:
        fence[row][col] = char
        col += 1
        row += direction
        if row == key - 1 or row == 0:
            direction *= -1
    encrypted_text = ''
    for i in range(key):
        for j in range(len(text)):
            if fence[i][j] != '':
                encrypted_text += fence[i][j]
    return encrypted_text


def rail_fence_decrypt(text, key):
    fence = [['' for _ in range(len(text))] for _ in range(key)]
    direction = 1
    row, col = 0, 0
    for i in range(len(text)):
        fence[row][col] = '*'
        col += 1
        row += direction
        if row == key - 1 or row == 0:
            direction *= -1
    index = 0
    for i in range(key):
        for j in range(len(text)):
            if fence[i][j] == '*':
                fence[i][j] = text[index]
                index += 1
    decrypted_text = ''
    direction = 1
    row, col = 0, 0
    for i in range(len(text)):
        decrypted_text += fence[row][col]
        col += 1
        row += direction
        if row == key - 1 or row == 0:
            direction *= -1
    return decrypted_text


# Example usage:
text = input("Enter the text to be encrypted: ")
key = 3 # Example key, you can change it
encrypted_text = rail_fence_encrypt(text, key)
print("Encrypted:", encrypted_text)


decrypted_text = rail_fence_decrypt(encrypted_text, key)
print("Decrypted:", decrypted_text)

Output :
























2. Row Column Transposition

Code :
import math
def row_column_transposition_encrypt(text, key):
    text_length = len(text)
    rows = int(math.ceil(text_length / key))  # Number of rows needed
    matrix = [['' for _ in range(key)] for _ in range(rows)]


    # Fill the matrix row by row
    index = 0
    for i in range(rows):
        for j in range(key):
            if index < text_length:
                matrix[i][j] = text[index]
                index += 1


    # Read the matrix column by column to create the ciphertext
    result = ''
    for j in range(key):
        for i in range(rows):
            if matrix[i][j] != '':  # Only add non-empty characters
                result += matrix[i][j]


    return result




def row_column_transposition_decrypt(ciphertext, key):
    text_length = len(ciphertext)
    rows = int(math.ceil(text_length / key))  # Number of rows needed
    matrix = [['' for _ in range(key)] for _ in range(rows)]


    # Fill the matrix column by column
    index = 0
    for j in range(key):
        for i in range(rows):
            if index < text_length:
                matrix[i][j] = ciphertext[index]
                index += 1


    # Read the matrix row by row to reconstruct the plaintext
    result = ''
    for i in range(rows):
        for j in range(key):
            result += matrix[i][j]


    return result.strip()  # Remove trailing spaces if any




# Example usage:
if __name__ == "__main__":
    text = input("Enter the text: ")
    key = int(input("Enter the key (number of columns): "))


    encrypted_text = row_column_transposition_encrypt(text, key)
    print("Encrypted Text:", encrypted_text)


    decrypted_text = row_column_transposition_decrypt(encrypted_text, key)
    print("Decrypted Text:", decrypted_text)





Output :





















PRACTICAL 4

Aim: Write a program to encrypt and decrypt strings using DES Algorithm.

Code :
pip  install pycryptodome


#DES
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import base64


# Generate a random 8-byte key for DES
def generate_key():
    return get_random_bytes(8)  # DES key size is 8 bytes (64 bits)


# Encrypt function using DES
def encrypt_data(key, data):
    cipher = DES.new(key, DES.MODE_ECB)
    # Pad data to make sure it's a multiple of DES block size (8 bytes)
    padded_data = pad(data.encode(), DES.block_size)
    encrypted_data = cipher.encrypt(padded_data)
    # Return the encrypted data as a base64 encoded string
    return base64.b64encode(encrypted_data).decode('utf-8')


# Decrypt function using DES
def decrypt_data(key, encrypted_data):
    cipher = DES.new(key, DES.MODE_ECB)
    # Decode the base64 encoded encrypted data
    encrypted_data_bytes = base64.b64decode(encrypted_data)
    # Decrypt the data and unpad it
    decrypted_data = unpad(cipher.decrypt(encrypted_data_bytes), DES.block_size)
    return decrypted_data.decode('utf-8')


if __name__ == "__main__":
    # Generate a DES key
    key = generate_key()
    print(f"Generated DES Key: {base64.b64encode(key).decode('utf-8')}")


    # Data to be encrypted
    data = "HELLO WORLD"


    # Encrypt the data
    encrypted_data = encrypt_data(key, data)
    print(f"Encrypted Data: {encrypted_data}")


    # Decrypt the data
    decrypted_data = decrypt_data(key, encrypted_data)
    print(f"Decrypted Data: {decrypted_data}")





Output:







































PRACTICAL 5

Aim: Write a program to encrypt and decrypt strings using AES Algorithm.

Code :
pip  install pycryptodome
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import base64


# Generate a random 16-byte key for AES (AES-128)
def generate_key():
    return get_random_bytes(16)  # 128-bit AES key


# Encrypt function using AES
def encrypt_data(key, data):
    cipher = AES.new(key, AES.MODE_ECB)
    # Pad data to make sure it's a multiple of AES block size (16 bytes)
    padded_data = pad(data.encode(), AES.block_size)
    encrypted_data = cipher.encrypt(padded_data)
    # Return the encrypted data as a base64 encoded string for easy storage/transfer
    return base64.b64encode(encrypted_data).decode('utf-8')


# Decrypt function using AES
def decrypt_data(key, encrypted_data):
    cipher = AES.new(key, AES.MODE_ECB)
    # Decode the base64 encoded encrypted data
    encrypted_data_bytes = base64.b64decode(encrypted_data)
    # Decrypt the data and unpad it
    decrypted_data = unpad(cipher.decrypt(encrypted_data_bytes), AES.block_size)
    return decrypted_data.decode('utf-8')


if __name__ == "__main__":
    # Generate an AES key
    key = generate_key()
    print(f"Generated AES Key: {base64.b64encode(key).decode('utf-8')}")


    # Data to be encrypted
    data = "HELLO WORLD"


    # Encrypt the data
    encrypted_data = encrypt_data(key, data)
    print(f"Encrypted Data: {encrypted_data}")


    # Decrypt the data
    decrypted_data = decrypt_data(key, encrypted_data)
    print(f"Decrypted Data: {decrypted_data}")

Output:









































PRACTICAL 6

Aim: Write a program to implement the Diffie-Hellman Key Agreement algorithm to generate symmetric keys.

Code :
import random
import hashlib


# Function to compute modular exponentiation (g^a mod p)
def mod_exp(base, exp, mod):
    return pow(base, exp, mod)


# Generate Diffie-Hellman shared secret
def diffie_hellman(p, g):
    # Step 1: Alice's private key (randomly chosen)
    a = random.randint(1, p - 1)
    # Alice computes public key A = g^a mod p
    A = mod_exp(g, a, p)


    # Step 2: Bob's private key (randomly chosen)
    b = random.randint(1, p - 1)
    # Bob computes public key B = g^b mod p
    B = mod_exp(g, b, p)


    print(f"Alice's private key: {a}")
    print(f"Alice's public key: {A}")
    print(f"Bob's private key: {b}")
    print(f"Bob's public key: {B}")


    # Step 3: Exchange public keys
    # Alice computes shared secret K = B^a mod p
    shared_secret_alice = mod_exp(B, a, p)


    # Bob computes shared secret K = A^b mod p
    shared_secret_bob = mod_exp(A, b, p)


    # Both should compute the same shared secret
    print(f"Alice's computed shared secret: {shared_secret_alice}")
    print(f"Bob's computed shared secret: {shared_secret_bob}")


    # Step 4: Generate a symmetric key from the shared secret (using a hash function)
    symmetric_key = hashlib.sha256(str(shared_secret_alice).encode()).hexdigest()
    print(f"Generated symmetric key: {symmetric_key}")


    return symmetric_key


# Example usage:
if __name__ == "__main__":
    # Public parameters (p is a large prime, g is a primitive root mod p)
    p = 23  # A small prime for simplicity; in practice, use a large prime
    g = 5   # A primitive root modulo p


    symmetric_key = diffie_hellman(p, g)



Output :



































PRACTICAL 7

Aim:Write a program to implement the MD5 algorithm to compute the message digest.

Code: 

import hashlib


def md5(msg: str) -> str:
    return hashlib.md5(msg.encode()).hexdigest()


# Test the MD5 function
message = "Hello World!"
print("MD5 Digest:", md5(message))

Output :


































PRACTICAL 8

Aim:Write a program to calculate HMAC-SHA1 Signature.

Code: 

import hmac
import hashlib


def calculate_hmac_sha1(key: str, message: str) -> str:
    # Create a new HMAC object with the given key and message, using SHA-1 as the hashing algorithm
    hmac_sha1 = hmac.new(key.encode(), message.encode(), hashlib.sha1)
   
    # Return the HMAC-SHA1 signature as a hexadecimal string
    return hmac_sha1.hexdigest()


# Example usage
key = "secret_key"
message = "Hello, HMAC-SHA1!"
hmac_signature = calculate_hmac_sha1(key, message)
print("HMAC-SHA1 Signature:", hmac_signature)






Output :





















PRACTICAL 9

Aim: Write a python program to illustrate Elgamal encryption.

Code:

import random
from sympy import mod_inverse


def generate_keys(prime, base):
    private_key = random.randint(2, prime - 2)
    public_key = pow(base, private_key, prime)
    return private_key, public_key


def encrypt(prime, base, public_key, message):
    k = random.randint(2, prime - 2)  # Random ephemeral key
    c1 = pow(base, k, prime)
    c2 = (message * pow(public_key, k, prime)) % prime
    return c1, c2


def decrypt(prime, private_key, c1, c2):
    s = pow(c1, private_key, prime)
    s_inv = mod_inverse(s, prime)
    message = (c2 * s_inv) % prime
    return message


def elgamal_demo():
    prime = 23  # Small prime number
    base = 5  # Primitive root modulo prime


    # Key generation
    private_key, public_key = generate_keys(prime, base)
    print(f"Prime (Public): {prime}")
    print(f"Base (Public): {base}")
    print(f"Private Key (Secret): {private_key}")
    print(f"Public Key: {public_key}")
   
    message = 8  # Message to be encrypted (must be smaller than prime)
    print(f"Original Message: {message}")
   
    # Encryption
    c1, c2 = encrypt(prime, base, public_key, message)
    print(f"Encrypted Message: (c1: {c1}, c2: {c2})")
   
    # Decryption
    decrypted_message = decrypt(prime, private_key, c1, c2)
    print(f"Decrypted Message: {decrypted_message}")


if __name__ == "__main__":
    elgamal_demo()




Output:



Editor is loading...
Leave a Comment