0% found this document useful (0 votes)
6 views4 pages

Cyber Security Lab Assignment 4

The document outlines the AES and RSA encryption algorithms, detailing the steps for key generation, encryption, and decryption processes. It includes Python code examples for implementing AES encryption with CBC mode and PKCS#7 padding, as well as RSA encryption using public and private keys. The document emphasizes the importance of padding for AES and provides a clear structure for both algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Cyber Security Lab Assignment 4

The document outlines the AES and RSA encryption algorithms, detailing the steps for key generation, encryption, and decryption processes. It includes Python code examples for implementing AES encryption with CBC mode and PKCS#7 padding, as well as RSA encryption using public and private keys. The document emphasizes the importance of padding for AES and provides a clear structure for both algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 4

A.​ AES algorithm

AES Encryption and Decryption Using CBC and PKCS#7 Padding

1.​ Generate Key and IV:


○​ Generate a random 32-byte key for AES-256.
○​ Generate a random 16-byte IV for CBC mode.
2.​ Prepare the Message:
○​ If the message size is not a multiple of the AES block size (16 bytes), pad it using
PKCS#7 padding.
3.​ Encrypt the Message:
○​ Create an AES Cipher object in CBC mode using the key and IV.
○​ Encrypt the padded plaintext using the encryptor.
4.​ Decrypt the Ciphertext:
○​ Create a decryptor using the same cipher configuration.
○​ Decrypt the ciphertext to get the padded plaintext.
5.​ Unpad the Message:
○​ Remove the PKCS#7 padding to recover the original plaintext.
6.​ Output Results:
○​ Display the ciphertext and decrypted plaintext.

Program :

from cryptography.hazmat.primitives.ciphers import Cipher,


algorithms, modes

from cryptography.hazmat.primitives import padding

import os

# Generate AES key and IV

key = os.urandom(32) # AES-256 requires a 32-byte key

iv = os.urandom(16) # IV for CBC mode

# Message to encrypt

message = b'Hello, AES Encryption!'


# Pad the message

padder = padding.PKCS7(128).padder()

padded_message = padder.update(message) + padder.finalize()

# Encrypt the message

cipher = Cipher(algorithms.AES(key), modes.CBC(iv))

encryptor = cipher.encryptor()

ciphertext = encryptor.update(padded_message) + encryptor.finalize()

# Decrypt the message

decryptor = cipher.decryptor()

unpadded_message = decryptor.update(ciphertext) +
decryptor.finalize()

# Unpad the message

unpadder = padding.PKCS7(128).unpadder()

plaintext = unpadder.update(unpadded_message) + unpadder.finalize()

print("Ciphertext:", ciphertext)

print("Decrypted Text:", plaintext.decode())

print("Key:", key)

B. RSA algorithm
Key Generation (Step 1)
1.​ Select Two Large Prime Numbers (p and q):​
Choose two large primes. Their product n = p * q is used as the modulus.
2.​ Compute Euler’s Totient (φ(n)):​
φ(n)=(p−1)⋅(q−1)
3.​ Choose a Public Exponent (e):​
Select e such that 1<e<φ(n) and gcd(e,φ(n))=1
4.​ In the code, e = 65537 is a common choice.
5.​ Compute the Private Exponent (d):​
Calculate d such that d⋅e≡1mod φ(n) using the modular multiplicative inverse.

The public key is (n, e) and the private key is (n, d).

Encryption (Step 2)

1.​ Plaintext Preparation (m):​


Represent the plaintext message as an integer m such that 0≤m<n.
2.​ Ciphertext Computation (c):​
Encrypt the message using the public key:​
c=me mod n ​
The ciphertext c is transmitted or stored securely.

Decryption (Step 3)

1.​ Recover the Message (m):​


Use the private key to compute the plaintext from the ciphertext:​
m=cdmod n
2.​ Transform Integer Back to Plaintext:​
Convert the decrypted integer m back to the original message.

Program:

from cryptography.hazmat.primitives.asymmetric import rsa, padding

from cryptography.hazmat.primitives import serialization, hashes

# Generate RSA keys


private_key = rsa.generate_private_key(public_exponent=65537,
key_size=2048)

public_key = private_key.public_key()

# Encrypt a message using the public key with PKCS1v15 padding

message = b'Hello, RSA Encryption!'

ciphertext = public_key.encrypt(

message,

padding.PKCS1v15() # Using PKCS1v15 padding

# Decrypt the message using the private key

plaintext = private_key.decrypt(

ciphertext,

padding.PKCS1v15() # Using PKCS1v15 padding for decryption as


well

print("Ciphertext:", ciphertext)

print("Decrypted Text:", plaintext.decode())

You might also like