How it works
RSA encryption uses number theory and modular arithmetic:- Generate two large prime numbers (p and q)
- Compute n = p × q and φ(n) = (p-1) × (q-1)
- Choose public exponent e (coprime with φ(n))
- Calculate private exponent d (modular inverse of e)
- Encrypt: C = M^e mod n
- Decrypt: M = C^d mod n
Algorithm explanation
Key generation process
Mathematical formulas
Encryption:- M = plaintext message (as a number)
- C = ciphertext (encrypted number)
- e = public exponent
- d = private exponent
- n = modulus (p × q)
Implementation details
The implementation is inRSA.py with several key functions:
Helper functions
Greatest Common Divisor (GCD)
Greatest Common Divisor (GCD)
Modular Inverse
Modular Inverse
Prime Generation
Prime Generation
Usage example
Basic encryption and decryption
Example output
Each character is encrypted independently, producing an array of numbers. The ciphertext is much longer than the plaintext.
Encrypting individual numbers
Parameters
The bit size for key generation. Default is 32 for educational purposes. Each prime will be
bits/2 bits long.The plaintext message to encrypt. Each character is converted to its ASCII value and encrypted separately.
Public exponent (part of the public key). Used for encryption.
Private exponent (part of the private key). Used for decryption. Must be kept secret.
Modulus (product of p and q). Part of both public and private keys.
Key characteristics
Encryption type
Asymmetric - Different keys for encryption/decryption
Output format
Array of integers (one per character)
Character support
ASCII characters (32-127)
Key size
32-bit (educational), production uses 2048+ bits
Security notes
Major vulnerabilities
Extremely weak key size
Extremely weak key size
32-bit keys can be factored in seconds
- Modern computers can factor 32-bit numbers instantly
- Production RSA uses minimum 2048-bit keys (some use 4096)
- A 32-bit key provides essentially zero security
No padding scheme
No padding scheme
Vulnerable to multiple attacks without padding
- No OAEP (Optimal Asymmetric Encryption Padding)
- No PKCS#1 v1.5 padding
- Susceptible to chosen-ciphertext attacks
- Deterministic encryption reveals patterns
Weak primality testing
Weak primality testing
Simple trial division is not cryptographically secure
- Production systems use Miller-Rabin or similar tests
- Current implementation is slow and not guaranteed
- May accept composite numbers in edge cases
- No strong randomness guarantees
No key management
No key management
Missing essential security features
- No secure key storage
- No key expiration or rotation
- No certificate infrastructure
- Private key is in plain memory
Character-by-character encryption
Character-by-character encryption
Each character encrypted independently
- Reveals message length exactly
- No message integrity checking
- No authentication
- Vulnerable to manipulation attacks
For production use
Use established cryptography libraries:
Educational value
Despite security flaws, this implementation is excellent for learning:- Core concepts
- Number theory
- Cryptographic principles
- Practical insights
- Public-key cryptography fundamentals
- Asymmetric vs symmetric encryption
- Key pairs (public/private)
- How RSA actually works mathematically
Advanced example
Common pitfalls
- Using small primes: Always use cryptographically large primes (production: 1024+ bits each)
- Reusing keys: Don’t use the same key pair for different purposes
- Encrypting long messages: RSA is typically used to encrypt symmetric keys, not entire messages
- Ignoring padding: Always use proper padding schemes (OAEP)
- Rolling your own crypto: Use established libraries in production
Performance comparison
RSA is significantly slower than symmetric encryption (like AES). In practice, RSA encrypts a symmetric key, which then encrypts the actual message.
See also
- Caesar cipher - Simple substitution cipher
- Transposition cipher - Rearrangement-based encryption
- Algorithms overview - Compare all three algorithms