Ace Your Cryptography Assignments with Expert Assistance

Комментарии · 58 Просмотры

Get expert cryptography assignment help from ProgrammingHomeworkHelp.com. Solutions for RSA and AES algorithms. Master cryptography with our guidance!

Welcome to ProgrammingHomeworkHelp.com! Are you struggling with your cryptography assignments? Our expert team is here to provide top-notch cryptography assignment help. Whether you need help understanding complex algorithms or coding secure encryption methods, we've got you covered. To showcase our expertise, here are two sample cryptography questions with detailed solutions, crafted by our professionals.

Question 1: Implementing the RSA Algorithm

Problem Statement:

You are required to implement the RSA algorithm for both encryption and decryption. RSA is a public-key cryptosystem that relies on the computational difficulty of factoring large integers. Your task is to:

  1. Generate two large prime numbers, pp and qq.
  2. Compute the modulus nn as n=p×qn = p \times q.
  3. Calculate the Euler's totient function, ϕ(n)=(p−1)×(q−1)\phi(n) = (p-1) \times (q-1).
  4. Choose an integer ee such that 1<e<ϕ(n)1 < e < \phi(n) and gcd⁡(e,ϕ(n))=1\gcd(e, \phi(n)) = 1.
  5. Compute the private key dd such that d≡e−1mod  ϕ(n)d \equiv e^{-1} \mod \phi(n).
  6. Write functions to encrypt and decrypt a message using the public and private keys.

Solution:

To tackle this problem, we will implement the RSA algorithm step-by-step in Python. Here is the complete code:

import random
from sympy import isprime, mod_inverse

def generate_prime_candidate(length):
    """ Generate an odd integer randomly """
    p = random.getrandbits(length)
    # apply a mask to set MSB and LSB to 1
    p |= (1 << length - 1) | 1
    return p

def generate_prime_number(length=1024):
    """ Generate a prime number of the given bit length """
    p = 4
    # keep generating while the number is not prime
    while not isprime(p):
        p = generate_prime_candidate(length)
    return p

def rsa_key_generation(bit_length=1024):
    # Generate two distinct primes p and q
    p = generate_prime_number(bit_length)
    q = generate_prime_number(bit_length)
    while q == p:
        q = generate_prime_number(bit_length)

    # Compute n = pq
    n = p * q
    # Compute Euler's totient function
    phi = (p - 1) * (q - 1)

    # Choose e such that 1 < e < phi and gcd(e, phi) = 1
    e = 65537  # Common choice for e
    # Compute d, the mod inverse of e
    d = mod_inverse(e, phi)

    # Public key: (e, n), Private key: (d, n)
    return ((e, n), (d, n))

def encrypt(plaintext, public_key):
    e, n = public_key
    # Convert plaintext to integer
    plaintext_int = int.from_bytes(plaintext.encode('utf-8'), 'big')
    # Encrypt the integer
    ciphertext_int = pow(plaintext_int, e, n)
    return ciphertext_int

def decrypt(ciphertext, private_key):
    d, n = private_key
    # Decrypt the integer
    plaintext_int = pow(ciphertext, d, n)
    # Convert integer back to plaintext
    plaintext_bytes = plaintext_int.to_bytes((plaintext_int.bit_length() + 7) // 8, 'big')
    return plaintext_bytes.decode('utf-8')

# Generate RSA keys
public_key, private_key = rsa_key_generation()

# Test the implementation
message = "Cryptography is fascinating!"
print(f"Original message: {message}")

encrypted_message = encrypt(message, public_key)
print(f"Encrypted message: {encrypted_message}")

decrypted_message = decrypt(encrypted_message, private_key)
print(f"Decrypted message: {decrypted_message}")

Explanation:

  1. Prime Generation: The generate_prime_number function generates a prime number of the specified bit length. It uses the sympy.isprime function to ensure that the number is prime.
  2. Key Generation: The rsa_key_generation function generates the RSA key pair. It chooses a common public exponent e=65537e = 65537 and computes the private key dd using the modular inverse.
  3. Encryption and Decryption: The encrypt and decrypt functions handle the encryption and decryption processes, respectively. They convert the plaintext message to an integer, perform the modular exponentiation, and then convert the integer back to a string.

This code provides a secure and efficient implementation of the RSA algorithm, demonstrating how to handle large numbers and cryptographic operations in Python.

Question 2: Implementing the Advanced Encryption Standard (AES)

Problem Statement:

You are tasked with implementing the AES algorithm in ECB mode. AES is a symmetric-key algorithm widely used for secure data encryption. Your implementation should:

  1. Generate a random 256-bit key.
  2. Implement the AES encryption and decryption functions using the ECB mode.
  3. Write functions to encrypt and decrypt a message with the AES algorithm.

Solution:

To solve this problem, we will use the pycryptodome library, which provides cryptographic primitives and recipes.

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import base64

def pad_message(message):
    """ Pad message to be a multiple of 16 bytes """
    padding_length = 16 - (len(message) % 16)
    return message + chr(padding_length) * padding_length

def unpad_message(padded_message):
    """ Unpad the message """
    padding_length = ord(padded_message[-1])
    return padded_message[:-padding_length]

def generate_aes_key():
    """ Generate a 256-bit AES key """
    return get_random_bytes(32)  # 256 bits

def aes_encrypt(plaintext, key):
    """ Encrypt the plaintext using AES with the given key """
    cipher = AES.new(key, AES.MODE_ECB)
    padded_message = pad_message(plaintext)
    encrypted_bytes = cipher.encrypt(padded_message.encode('utf-8'))
    encrypted_message = base64.b64encode(encrypted_bytes).decode('utf-8')
    return encrypted_message

def aes_decrypt(encrypted_message, key):
    """ Decrypt the ciphertext using AES with the given key """
    cipher = AES.new(key, AES.MODE_ECB)
    encrypted_bytes = base64.b64decode(encrypted_message)
    decrypted_padded_message = cipher.decrypt(encrypted_bytes).decode('utf-8')
    return unpad_message(decrypted_padded_message)

# Generate AES key
aes_key = generate_aes_key()

# Test the implementation
message = "Advanced Encryption Standard"
print(f"Original message: {message}")

encrypted_message = aes_encrypt(message, aes_key)
print(f"Encrypted message: {encrypted_message}")

decrypted_message = aes_decrypt(encrypted_message, aes_key)
print(f"Decrypted message: {decrypted_message}")

Explanation:

  1. Padding: AES requires the plaintext to be a multiple of the block size (16 bytes). The pad_message function pads the plaintext to meet this requirement, and the unpad_message function removes the padding after decryption.
  2. Key Generation: The generate_aes_key function generates a random 256-bit key for AES encryption.
  3. Encryption and Decryption: The aes_encrypt and aes_decrypt functions perform the encryption and decryption processes using AES in ECB mode. The encrypted message is base64-encoded for easy handling, and the decryption function decodes and unpads the message.

This implementation provides a robust example of how to use AES for secure data encryption in Python, ensuring that your data remains protected.

Conclusion

Cryptography can be a challenging subject, but with the right guidance and support, you can master it. At ProgrammingHomeworkHelp.com, our experts are ready to provide you with the cryptography assignment help you need. Whether it's implementing complex algorithms like RSA or AES, or understanding theoretical concepts, we're here to assist you every step of the way. Feel free to reach out for personalized assistance and take your cryptography skills to the next level.

Комментарии