Practical 6: Encryption & Decryption
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.
Generate RSA public-private key pairs
Encrypt and decrypt messages using PKCS1_OAEP padding
Learn differences between symmetric and asymmetric encryption
Observe security mechanisms and limitations of RSA
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.
Uses a public key for encryption and a private key for decryption.
Shared openly; used to encrypt data.
Kept secret; used to decrypt data.
Based on the difficulty of factoring large numbers (e.g., product of two 1024-bit primes).
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())
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.
Plain RSA encryption (without padding) is not semantically secure. This means:
OAEP (Optimal Asymmetric Encryption Padding):
| 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 |
RSA offers strong security for small data chunks, based on the difficulty of factoring large integers.
| 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 |
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.