0Pricing
Cryptology Academy · Lesson

RSA Encryption & Decryption Math

Apply modular exponentiation to encrypt and decrypt a message.

RSA Encryption & Decryption Math is a free Cryptology Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

With RSA keys generated, we now apply them. Encryption and decryption are both modular exponentiations — simple to compute, infeasible to reverse without the private key.

Textbook RSA Encryption

Encrypt message M (as integer) with public key (e, n): C = M^e mod n Example: M=42, e=17, n=3233 C = 42^17 mod 3233 Python: C = pow(42, 17, 3233) = 2557

Textbook RSA Decryption

Decrypt ciphertext C with private key (d, n): M = C^d mod n C=2557, d=2753, n=3233 M = 2557^2753 mod 3233 Python: M = pow(2557, 2753, 3233) = 42 ✓

Why It Works: Euler's Theorem

C^d mod n = (M^e)^d mod n = M^(ed) mod n. Since ed ≡ 1 (mod φ(n)), M^(ed) = M^(1+k×φ(n)) = M × (M^φ(n))^k ≡ M × 1^k = M.

Square-and-Multiply Algorithm

Naive computation of M^e would require e multiplications. Square-and-multiply reduces this to O(log e) multiplications: pow(base, exp, mod) in Python uses this automatically.

Message Size Limitation

Textbook RSA requires M < n. For RSA-2048, M can be at most 256 bytes. This is fine for encrypting a short AES key, not for encrypting large files directly.

Why Textbook RSA Is Insecure

Problems with raw RSA: 1. Same M always → same C (deterministic) 2. Small M with small e: C = M^e literally (no modular reduction) 3. Multiplicative: Enc(M1)×Enc(M2) = Enc(M1×M2) Padding schemes solve all these issues.

RSA Encryption in Python (OAEP)

from cryptography.hazmat.primitives.asymmetric import padding from cryptography.hazmat.primitives import hashes ciphertext = public_key.encrypt( b'Secret message', padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) )

RSA Decryption in Python

plaintext = private_key.decrypt( ciphertext, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) print(plaintext) # b'Secret message'

Hybrid RSA + AES Pattern

# Real-world pattern: aes_key = os.urandom(32) # Random 256-bit AES key encrypted_key = rsa_encrypt(aes_key, recipient_pub) # RSA ciphertext = aes_gcm_encrypt(aes_key, plaintext) # AES # Send: encrypted_key + ciphertext

RSA Performance Numbers

RSA-2048 encrypt (public): ~0.1 ms. RSA-2048 decrypt (private): ~1 ms. AES-256-GCM: ~100 MB/s. Hybrid encryption combines RSA key exchange speed with AES data throughput.

Quick Check

Using textbook RSA with e=3, n=33: what is the encryption of M=5?

Recap

RSA encryption and decryption math is clear. Next we see how RSA protects HTTPS, SSH, and code signing in the real world.

Frequently asked questions

Is the “RSA Encryption & Decryption Math” lesson free?

Yes — the full text of “RSA Encryption & Decryption Math” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “RSA Encryption & Decryption Math”?

Apply modular exponentiation to encrypt and decrypt a message. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “RSA Encryption & Decryption Math” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Public-Key Cryptography Concepts
  2. RSA Key Generation Step by Step
  3. RSA Encryption & Decryption Math
  4. RSA Real-World Use Cases & Key Sizes
← Back to Cryptology Academy