RSA Algorithm Implementation

Practical 6: Encryption & Decryption

Asymmetric Cryptography

πŸ” Objective

To implement RSA (Rivest-Shamir-Adleman) public key encryption algorithm, and demonstrate how asymmetric cryptography works for securing communication between two parties without prior key exchange.

πŸ”Purpose of Practical

πŸ”‘ Key Generation

Generate RSA public-private key pairs

πŸ”’ Encryption

Encrypt and decrypt messages using PKCS1_OAEP padding

πŸ“š Understanding

Learn differences between symmetric and asymmetric encryption

πŸ›‘οΈ Security

Observe security mechanisms and limitations of RSA

πŸ”Theory: RSA Algorithm Overview

RSA (Rivest–Shamir–Adleman) is one of the first public-key cryptosystems and is widely used for secure data transmission. It is based on the mathematical difficulty of factoring large prime numbers.

Key Concepts

πŸ”“ Asymmetric Encryption

Uses a public key for encryption and a private key for decryption.

🌐 Public Key

Shared openly; used to encrypt data.

πŸ” Private Key

Kept secret; used to decrypt data.

πŸ›‘οΈ Security

Based on the difficulty of factoring large numbers (e.g., product of two 1024-bit primes).

πŸ“ Python Implementation

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

# Generate RSA key pair (public and private)
key = RSA.generate(2048)
public_key = key.publickey()
cipher_rsa = PKCS1_OAEP.new(public_key)

# Encrypt message
message = b'GECBHARUCH'
ciphertext = cipher_rsa.encrypt(message)
print("Encrypted:", ciphertext.hex())

# Decrypt message
decryptor = PKCS1_OAEP.new(key)
decrypted = decryptor.decrypt(ciphertext)
print("Decrypted:", decrypted.decode())
πŸ’‘ Installation Required: pip install pycryptodome

πŸ”Output

Encrypted: b239b54cb5b0a558df62df395cf5dc16dc6447f475012ba3526f28836118d342e890ef9c72c80d1f1800f666b3fc17597dda26e
721391a0105a0d7139eab704722717e034bc75d95bc8fe696bc5574a69ab02fdaa03f502bc41fe5e7465c95e113589bd27ae2de
06b77d5b46b34023d19a83de45dea7b85c4fca0667fc10230782b60b34ded5b7a5740004ebec0683a23f77523369937c5064dae
4b17b81ac5d4dcb12b2eb9f26e152afb4b3b2c6e56d020fda16d5ab5ec37b5dbb52c62b67f3355c2beb14b0d1051fe328cb897d
e3b3a284fe2ae0d7bcca992f8d9cb545e030bf1720c463635b7b188794c7ef37e6a23d104c95a87ba17a107eb3df4089149f

Decrypted: GECBHARUCH

πŸ”Understanding PKCS1_OAEP

πŸ” What is PKCS1_OAEP?

PKCS1_OAEP stands for: Public-Key Cryptography Standards #1 β€” Optimal Asymmetric Encryption Padding. It is a padding scheme used with RSA encryption to make the process more secure and resistant to attacks.

Why is Padding Needed in RSA?

Plain RSA encryption (without padding) is not semantically secure. This means:

What Does OAEP Do?

OAEP (Optimal Asymmetric Encryption Padding):

πŸ”Important Notes

Topic Description
Key Size 2048 bits (Secure enough for practical applications)
Message Size Limit Limited by key size and padding (~190 bytes for 2048-bit key with OAEP)
Use of OAEP Optimal Asymmetric Encryption Padding improves security
Asymmetric Use Case Not for bulk data, typically encrypts small items (AES keys, tokens)
Security Best Practice Never reuse keys with the same message; use hybrid encryption for big files

πŸ”Security Aspects

RSA offers strong security for small data chunks, based on the difficulty of factoring large integers.

⚠️ RSA is vulnerable if:

  • Poor padding (e.g., not using OAEP)
  • Small keys (<2048 bits)
  • Insecure random number generation

🎯 RSA is commonly used for:

  • Digital signatures
  • Secure key exchange
  • Authentication

πŸ”Real-Life Applications of RSA

🌐 HTTPS (SSL/TLS)
✍️ Digital Signatures
πŸ“§ Email Encryption
πŸ“ Secure File Sharing
πŸ”‘ Key Exchange
πŸ” PGP/S-MIME

πŸ”RSA vs AES Comparison

Feature RSA (Asymmetric) AES (Symmetric Block Cipher)
Key Type Public and Private Shared Secret
Key Length 1024–4096 bits (2048 common) 128, 192, 256 bits
Encrypts Small data (used for keys, not bulk) Large data blocks
Speed Slower Faster
Use Cases Key exchange, signatures, authentication Bulk encryption (VPNs, files, etc.)
Security Basis Prime factorization Substitution-permutation network

πŸ”Key Observations

πŸ”Summary

This practical introduces RSA, the backbone of public-key cryptography. It shows how encryption and decryption work with different keys, enabling secure communication without sharing a key in advance. While RSA is secure and elegant, its limitations in speed and data size make it ideal for secure key exchanges or digital signatures rather than bulk data encryption. When combined with AES (hybrid encryption), RSA provides the foundation for secure internet communication, including HTTPS, PGP, and blockchain systems.

πŸŽ“ Learning Outcomes

By the end of this practical, students will be able to:

πŸ” Understand
How RSA encryption and decryption works
πŸ”‘ Generate
RSA key pairs (public/private)
πŸ”’ Implement
RSA for secure data encryption and decryption
πŸ’» Use
RSA using the pycryptodome library in Python