Pratical No 1
Name : Avadhut Rananavare.
Roll No : 18.
Batch : AI&DS.
1. Implementation of S-DES.
Code :
from Crypto.Cipher import DES
import binascii
def pad(text):
while len(text) % 8 != 0:
text += " "
return text
key = b"8bytekey" # DES requires an 8-byte key
cipher = DES.new(key, DES.MODE_ECB)
plaintext = "Hello123"
padded_text = pad(plaintext)
# Encrypt
encrypted_text = cipher.encrypt(padded_text.encode())
print("Encrypted:", binascii.hexlify(encrypted_text))
# Decrypt
decrypted_text = cipher.decrypt(encrypted_text).decode().strip()
print("Decrypted:", decrypted_text)
Output :
Pratical No : 2
Name : Avadhut Rananavare.
Roll No : 18
Batch : TE AI&DS
Code :
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# Function to encrypt data
def aes_encrypt(data, key):
# Ensure the key is 16, 24, or 32 bytes (AES key sizes)
key = key.ljust(32)[:32]
# Generate a random initialization vector (IV)
iv = get_random_bytes(AES.block_size)
# Create AES cipher object in CBC mode
cipher = AES.new(key.encode(), AES.MODE_CBC, iv)
# Pad the data to make it a multiple of AES block size (16 bytes)
padded_data = pad(data.encode(), AES.block_size)
# Encrypt the data
encrypted_data = cipher.encrypt(padded_data)
# Return IV + encrypted data (IV is needed for decryption)
return iv + encrypted_data
# Function to decrypt data
def aes_decrypt(encrypted_data, key):
# Ensure the key is 16, 24, or 32 bytes (AES key sizes)
key = key.ljust(32)[:32]
# Extract the IV (first 16 bytes of the encrypted data)
iv = encrypted_data[:AES.block_size]
# Create AES cipher object in CBC mode
cipher = AES.new(key.encode(), AES.MODE_CBC, iv)
# Decrypt the data
decrypted_data = cipher.decrypt(encrypted_data[AES.block_size:])
# Unpad the decrypted data
return unpad(decrypted_data, AES.block_size).decode()
# Example Usage
key = "mysecretpassword"
data = "This is a secret message"
# Encrypt the data
encrypted_data = aes_encrypt(data, key)
print(f"Encrypted data: {encrypted_data.hex()}")
# Decrypt the data
decrypted_data = aes_decrypt(encrypted_data, key)
print(f"Decrypted data: {decrypted_data}")
Output :
Pratical No : 3
Name : Avadhut Rananavare.
Roll No : 18.
Batch : TE AI&DS.
Code :
import random
# Function to generate a large prime number for the Diffie-Hellman key exchange
def generate_prime(bits=2048):
# For simplicity, using a library to generate large primes (or you can use an existing prime)
# In a real-world scenario, you'd use a cryptographic library to generate this securely.
# For now, we'll use a small prime for demonstration purposes.
# The larger the prime, the more secure the exchange.
return 23 # Small prime example for demonstration (replace with a real large prime in
production)
# Function to generate the base (generator)
def generate_base(p):
# In real-world scenarios, you would use a base g that is primitive mod p
# but for simplicity, let's use 5 as a base (common choice for base in some cases).
return 5
# Function to compute the public key (A or B)
def compute_public_key(private_key, g, p):
return pow(g, private_key, p)
# Function to compute the shared secret key (s)
def compute_shared_secret(public_key, private_key, p):
return pow(public_key, private_key, p)
# Step 1: Choose a large prime and a base
p = generate_prime() # In production, use a large prime
g = generate_base(p)
# Step 2: Generate private keys for Alice and Bob (randomly selected)
alice_private_key = random.randint(1, p-1)
bob_private_key = random.randint(1, p-1)
# Step 3: Compute public keys for Alice and Bob
alice_public_key = compute_public_key(alice_private_key, g, p)
bob_public_key = compute_public_key(bob_private_key, g, p)
print(f"Alice's private key: {alice_private_key}")
print(f"Bob's private key: {bob_private_key}")
print(f"Alice's public key: {alice_public_key}")
print(f"Bob's public key: {bob_public_key}")
# Step 4: Exchange public keys and compute the shared secret
alice_shared_secret = compute_shared_secret(bob_public_key, alice_private_key, p)
bob_shared_secret = compute_shared_secret(alice_public_key, bob_private_key, p)
# Step 5: Display the shared secret (both will be the same)
print(f"Alice's computed shared secret: {alice_shared_secret}")
print(f"Bob's computed shared secret: {bob_shared_secret}")
# Verify both computed the same shared secret
assert alice_shared_secret == bob_shared_secret, "The shared secrets do not match!"
print("Shared secret successfully computed and verified!")
Output :
Pratical No : 4
Name : Avadhut Rananavare.
Roll No : 18.
Batch : TE AI&DS.
Code :
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Random import get_random_bytes
# Function to generate RSA keys (public and private)
def generate_rsa_keys(bits=2048):
# Generate RSA keys (private key and public key)
private_key = RSA.generate(bits)
public_key = private_key.publickey()
return private_key, public_key
# Function to encrypt a message using the public key
def rsa_encrypt(public_key, message):
# Create a cipher object using the public key
cipher = PKCS1_OAEP.new(public_key)
# Encrypt the message
ciphertext = cipher.encrypt(message.encode())
return ciphertext
# Function to decrypt a message using the private key
def rsa_decrypt(private_key, ciphertext):
# Create a cipher object using the private key
cipher = PKCS1_OAEP.new(private_key)
# Decrypt the ciphertext
plaintext = cipher.decrypt(ciphertext).decode()
return plaintext
# Example Usage
# Step 1: Generate RSA keys
private_key, public_key = generate_rsa_keys()
# Step 2: Encrypt a message using the public key
message = "This is a secret message"
ciphertext = rsa_encrypt(public_key, message)
print(f"Ciphertext (encrypted): {ciphertext.hex()}")
# Step 3: Decrypt the ciphertext using the private key
decrypted_message = rsa_decrypt(private_key, ciphertext)
print(f"Decrypted message: {decrypted_message}")
Output :