NS & C - Journal-2
NS & C - Journal-2
- SY MSc CS 05
Practical No. - 01
Aim - Implement a Basic Encryption and Decryption Process for Plaintext
Data. Use Cryptographic Libraries (e.g., OpenSSL, Python
Cryptography Library or a Similar Encryption Toolkit) to Encrypt and
Decrypt a Given Plaintext Message.
__________________________________________________________________
Theory:
Encryption is the process of converting plaintext data into a coded format, known as ciphertext,
to prevent unauthorized access. This transformation uses an algorithm and a key, ensuring that
only those with the correct key can decrypt and access the original data. Decryption is the
reverse process, converting ciphertext back into its original plaintext format using a key.
Encryption and decryption are fundamental to data security, ensuring confidentiality and
protecting information during transmission or storage. Symmetric encryption uses the same key
for both processes, while asymmetric encryption uses a pair of keys—a public key for encryption
and a private key for decryption.
# Encrypt a message
def encrypt_message(message, key):
fernet = Fernet(key)
encrypted_message = fernet.encrypt(message.encode())
return encrypted_message
# Decrypt a message
def decrypt_message(encrypted_message, key):
fernet = Fernet(key)
decrypted_message = fernet.decrypt(encrypted_message).decode()
return decrypted_message
# Example usage:
key = generate_key()
print("Encryption Key: ", key) # Print the key as bytes
message = input("Enter the plaintext: ")
encrypted_message = encrypt_message(message, key)
Practical No. - 01
Aim - Implement a Basic Encryption and Decryption Process for Plaintext
Data. Use Cryptographic Libraries (e.g., OpenSSL, Python
Cryptography Library or a Similar Encryption Toolkit) to Encrypt and
Decrypt a Given Plaintext Message.
__________________________________________________________________
Output: fernet.py
Practical No. - 02
Aim - Develop Encryption and Decryption Routines for DES Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
Theory:
The Data Encryption Standard (DES) is a symmetric-key block cipher that was widely used for
data encryption. Developed in the 1970s by IBM and adopted by the U.S. National Institute of
Standards and Technology (NIST) in 1977, DES encrypts data in 64-bit blocks using a 56-bit
key. DES operates through a series of permutations and substitutions, creating complex
encryption transformations. Although it was considered secure for many years, advancements in
computing power have made DES vulnerable to brute-force attacks. As a result, DES has been
largely replaced by more secure algorithms like AES (Advanced Encryption Standard) in modern
applications.
return encrypted_text_base64
Practical No. - 02
Aim - Develop Encryption and Decryption Routines for DES Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
return decrypted_text
if choice == '1':
plaintext = input("Enter the plaintext to encrypt: ")
key = input("Enter an 8-character key: ")
if len(key) != 8:
print("Key must be exactly 8 characters long.")
continue
ciphertext = des_encrypt(plaintext, key)
print(f"Encrypted Text: {ciphertext}")
Practical No. - 02
Aim - Develop Encryption and Decryption Routines for DES Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
else:
print("Invalid choice. Please try again.")
Output: des.py
Practical No. - 03
Aim - Develop the Elgamal Encryption System, Including Key Generation,
Encryption and Decryption Processes. Tools/Technologies:
Cryptographic Libraries, Python or Specialized Encryption Software.
__________________________________________________________________
Theory:
The ElGamal encryption system is a public-key cryptosystem based on the Diffie-Hellman key
exchange. Developed by Taher ElGamal in 1984, it uses asymmetric cryptography to encrypt and
decrypt messages. The system involves selecting a large prime number ‘p’ and a generator ‘g’ of
the multiplicative group modulo ‘p’. Each user has a private key ‘x’ and a public key ‘y’, where
y = gx mod p. Encryption involves choosing a random integer ‘k’ to generate a pair of ciphertext
values. Decryption requires the private key to recover the original message. ElGamal is known
for its security based on the hardness of the discrete logarithm problem.
import random
def generate_keys():
# Step 1: Enter a large prime number p
p = int(input("Enter a large prime number p: "))
# Step 1: Convert the message to an integer (for simplicity, assuming message is an integer)
m = message
Practical No. - 03
Aim - Develop the Elgamal Encryption System, Including Key Generation,
Encryption and Decryption Processes. Tools/Technologies:
Cryptographic Libraries, Python or Specialized Encryption Software.
__________________________________________________________________
# Example usage
public_key, private_key = generate_keys()
print("Public Key:", public_key)
print("Private Key:", private_key)
message = 9
print("Original Message:", message)
Practical No. - 03
Aim - Develop the Elgamal Encryption System, Including Key Generation,
Encryption and Decryption Processes. Tools/Technologies:
Cryptographic Libraries, Python or Specialized Encryption Software.
__________________________________________________________________
Output: elgamal.py
Practical No. - 04
Aim - Implement the Playfair Cipher Algorithm and Develop Encryption and
Decryption Routines, Including the 5x5 Matrix and Matrix Indexes.
Tools/Technologies: Programming Languages, such as Python or Java
for algorithm Implementation.
__________________________________________________________________
Theory:
The Playfair cipher is a digraph substitution cipher devised by Charles Wheatstone in 1854 and
popularized by Sir Frank Playfair. It encrypts pairs of letters rather than single letters. The
algorithm uses a 5x5 matrix of letters, constructed from a keyword, with the remaining letters of
the alphabet (excluding duplicates and usually combining 'I' and 'J' into one) filling the rest. To
encrypt, each pair of plaintext letters is located in the matrix and substituted based on their
positions: if they’re in the same row or column, they’re replaced by letters in the same row or
column but shifted. If not, they form a rectangle, and the opposite corners are swapped. This
method enhances security compared to simple substitution ciphers.
import numpy as np
return matrix
Practical No. - 04
Aim - Implement the Playfair Cipher Algorithm and Develop Encryption and
Decryption Routines, Including the 5x5 Matrix and Matrix Indexes.
Tools/Technologies: Programming Languages, such as Python or Java
for algorithm Implementation.
__________________________________________________________________
# Add 'X' between repeating characters and at the end if odd length
i=0
while i < len(plaintext):
formatted_text += plaintext[i]
if i + 1 < len(plaintext) and plaintext[i] == plaintext[i + 1]:
formatted_text += 'X'
elif i + 1 < len(plaintext):
formatted_text += plaintext[i + 1]
i += 1
i += 1
if len(formatted_text) % 2 != 0:
formatted_text += 'X'
return formatted_text
Practical No. - 04
Aim - Implement the Playfair Cipher Algorithm and Develop Encryption and
Decryption Routines, Including the 5x5 Matrix and Matrix Indexes.
Tools/Technologies: Programming Languages, such as Python or Java
for algorithm Implementation.
__________________________________________________________________
if row1 == row2:
# Same row, shift right
ciphertext += matrix[row1][(col1 + 1) % 5] + matrix[row2][(col2 + 1) % 5]
elif col1 == col2:
# Same column, shift down
ciphertext += matrix[(row1 + 1) % 5][col1] + matrix[(row2 + 1) % 5][col2]
else:
# Rectangle swap
ciphertext += matrix[row1][col2] + matrix[row2][col1]
return ciphertext
if row1 == row2:
# Same row, shift left
plaintext += matrix[row1][(col1 - 1) % 5] + matrix[row2][(col2 - 1) % 5]
elif col1 == col2:
# Same column, shift up
plaintext += matrix[(row1 - 1) % 5][col1] + matrix[(row2 - 1) % 5][col2]
else:
# Rectangle swap
plaintext += matrix[row1][col2] + matrix[row2][col1]
return plaintext
Practical No. - 04
Aim - Implement the Playfair Cipher Algorithm and Develop Encryption and
Decryption Routines, Including the 5x5 Matrix and Matrix Indexes.
Tools/Technologies: Programming Languages, such as Python or Java
for algorithm Implementation.
__________________________________________________________________
while True:
print("\nChoose an option:")
print("1. Encrypt")
print("2. Decrypt")
print("3. Exit")
if choice == '1':
plaintext = input("Enter the plaintext to encrypt: ")
ciphertext = playfair_encrypt(plaintext, matrix)
print(f"Ciphertext: {ciphertext}")
else:
print("Invalid choice. Please try again.")
Practical No. - 04
Aim - Implement the Playfair Cipher Algorithm and Develop Encryption and
Decryption Routines, Including the 5x5 Matrix and Matrix Indexes.
Tools/Technologies: Programming Languages, such as Python or Java
for algorithm Implementation.
__________________________________________________________________
Output: playfair.py
Practical No. - 05
Aim - Implement Cryptographic Algorithm Diffie-Hellman Key Exchange.
Tools/Technologies: Cryptographic Libraries, Programming Languages
and Hashing Utilities.
__________________________________________________________________
Theory:
The Diffie-Hellman key exchange is a cryptographic protocol allowing two parties to securely
share a secret key over an insecure communication channel. Introduced by Whitfield Diffie and
Martin Hellman in 1976, it leverages modular arithmetic and the difficulty of solving discrete
logarithms. Both parties agree on a large prime number ‘p’ and a base ‘g’. Each party generates a
private key and computes a public key based on ‘g’ and their private key. They exchange public
keys and each computes the shared secret using their private key and the other party’s public key.
The shared secret is identical for both parties and can be used for symmetric encryption.
import random
def generate_private_key(p):
"""Generate a private key."""
return random.randint(2, p-2)
def main():
# Step 1: Select a large prime number p and a generator g
p = int(input("Enter a large prime number p: "))
g = int(input("Enter a generator g: "))
Practical No. - 05
Aim - Implement Cryptographic Algorithm Diffie-Hellman Key Exchange.
Tools/Technologies: Cryptographic Libraries, Programming Languages
and Hashing Utilities.
__________________________________________________________________
print("\nParty B:")
private_key_b = generate_private_key(p)
public_key_b = generate_public_key(p, g, private_key_b)
print(f"Private Key B: {private_key_b}")
print(f"Public Key B: {public_key_b}")
Output: deffie.py
Practical No. - 06
Aim - Develop Encryption and Decryption Routine for RSA Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
Theory:
import random
from sympy import isprime, mod_inverse
import base64
def generate_prime(bits=8):
"""Generate a random prime number of specified bit length."""
while True:
num = random.getrandbits(bits)
if isprime(num):
return num
def generate_keys():
"""Generate RSA public and private keys."""
p = generate_prime()
q = generate_prime()
n=p*q
phi_n = (p - 1) * (q - 1)
Practical No. - 06
Aim - Develop Encryption and Decryption Routine for RSA Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
private_key = (n, d)
return c
return message
# Example usage
public_key, private_key = generate_keys()
print("Public Key:", public_key)
print("Private Key:", private_key)
Practical No. - 06
Aim - Develop Encryption and Decryption Routine for RSA Algorithm.
Tools/Technologies: Cryptographic Libraries, Programming Languages
like Python or Java.
__________________________________________________________________
print("Ciphertext:", ciphertext)
Output: rsa.py
Practical No. - 07
Aim - Implement Cryptographic Algorithm MD5. Tools/Technologies:
Cryptographic Libraries, Programming Languages and Hashing
Utilities.
__________________________________________________________________
Theory:
MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces a
128-bit hash value from input data. Designed by Ronald Rivest in 1991, MD5 is commonly used
for data integrity verification and checksums. It processes input data in 512-bit blocks, resulting
in a fixed-size 128-bit hash value. Despite its popularity, MD5 is now considered
cryptographically broken and unsuitable for security purposes due to vulnerabilities such as
susceptibility to collision attacks, where different inputs produce the same hash. As a result,
MD5 is largely replaced by more secure hashing algorithms like SHA-256 in modern
applications.
import hashlib
def md5_hash(message):
"""Compute the MD5 hash of a given message."""
# Create an MD5 hash object
md5 = hashlib.md5()
return hash_value
# Example usage
message = "The weather is gloomy"
print("Original Message:", message)
hash_value = md5_hash(message)
print("MD5 Hash:", hash_value)
Practical No. - 07
Aim - Implement Cryptographic Algorithm MD5. Tools/Technologies:
Cryptographic Libraries, Programming Languages and Hashing
Utilities.
__________________________________________________________________
Output: md5.py
Practical No. - 08
Aim - Implement Cryptographic Algorithm SHA-1. Tools/Technologies:
Cryptographic Libraries, Programming Languages and Hashing
Utilities.
__________________________________________________________________
Theory:
SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function designed by the NSA and
published by NIST in 1993. It produces a 160-bit hash value from input data, typically
represented as a 40-digit hexadecimal number. SHA-1 processes data in 512-bit blocks,
generating a fixed-size 160-bit hash. Initially used for data integrity and digital signatures,
SHA-1 is now considered weak due to vulnerabilities, including susceptibility to collision attacks
where different inputs generate the same hash. As a result, SHA-1 has been deprecated in favour
of more secure algorithms like SHA-256, which provide stronger cryptographic security.
import hashlib
def sha1_hash(message):
"""Compute the SHA-1 hash of a given message."""
# Create a SHA-1 hash object
sha1 = hashlib.sha1()
return hash_value
# Example usage
message = "Sunshine and Moonlight"
print("Original Message:", message)
hash_value = sha1_hash(message)
print("SHA-1 Hash:", hash_value)
Practical No. - 08
Aim - Implement Cryptographic Algorithm SHA-1. Tools/Technologies:
Cryptographic Libraries, Programming Languages and Hashing
Utilities.
__________________________________________________________________
Output: sha1.py