A CLI-based collection of 4 asymmetric cryptography implementations focused on public-key encryption and number-theoretic primitives. This folder groups together four educational modules: RSA, ElGamal, Paillier, and Rabin.
Modules/
└── Asymmetric_Key_Cryptography/
└── Public_Key_Encryption/
├── rsa.py
├── ElGamal.py
├── Paillier.py
└── Rabin.py
| Module | Full Name | Main Idea | Implementation Style | Typical Use |
|---|---|---|---|---|
| RSA | Rivest-Shamir-Adleman | Modular exponentiation over large composites | cryptography |
Key transport, encryption, signatures |
| ElGamal | ElGamal Encryption | Discrete logarithm / Diffie-Hellman style masking | Pure Python | Probabilistic public-key encryption |
| Paillier | Paillier Cryptosystem | Composite residuosity | Pure Python | Additively homomorphic encryption |
| Rabin | Rabin Cryptosystem | Quadratic residues and integer factorization | Pure Python | Fast encryption, academic study |
RSA is included as the most widely used public-key primitive in this folder, even though the other modules emphasize classic encryption schemes.
pip install cryptography| Library | Used By |
|---|---|
cryptography |
RSA |
| None beyond the Python standard library | ElGamal, Paillier, Rabin |
| Module | Default Key Size | Notes |
|---|---|---|
| RSA | 2048 bits | 2048 to 4096 bits recommended |
| ElGamal | 2048 bits | Uses the RFC 3526 2048-bit MODP group |
| Paillier | 2048 bits | Generated from two large primes |
| Rabin | 2048 bits | Uses Blum primes p and q |
Every module follows the same menu pattern:
--- <MODULE NAME> ---
Type : Asymmetric / Public Key Encryption
Math : <module-specific cryptographic foundation>
Key : <default or recommended size>
1. Generate Keypair
2. Encrypt Message
3. Decrypt Message
4. How <MODULE> Works
5. Back
RSA uses How RSA Works and includes OAEP padding details. The other modules use similarly named explainers that describe their mathematics and security properties.
RSA uses large prime generation, modular arithmetic, and OAEP padding with MGF1-SHA256. It supports generating a keypair, encrypting with the public key, and decrypting with the private key.
Saved files:
samples/rsa_private_key.pem
samples/rsa_public_key.pem
samples/rsa_output.txt
ElGamal uses a fixed 2048-bit MODP group, a random private exponent, and an ephemeral encryption exponent so the same plaintext encrypts differently each time.
Saved files:
samples/elgamal_private_key.txt
samples/elgamal_public_key.txt
samples/elgamal_output.txt
Paillier demonstrates additive homomorphism. It generates two primes, computes n, lambda, and mu, and supports the standard Paillier encryption and decryption workflow.
Saved files:
samples/paillier_private_key.txt
samples/paillier_public_key.txt
samples/paillier_output.txt
Rabin uses Blum primes and square-root recovery with CRT. Because Rabin decryption yields four possible roots, the implementation appends a marker to identify the correct plaintext.
Saved files:
samples/rabin_private_key.txt
samples/rabin_public_key.txt
samples/rabin_output.txt
Ciphertext (Base64): <base64 data>
Ciphertext (C1): <hex data>
Ciphertext (C2): <hex data>
Ciphertext (hex): <hex data>
Ciphertext (hex): <hex data>
| Module | Confidentiality | Integrity | Randomized | Special Property |
|---|---|---|---|---|
| RSA | Yes | No | Yes, with OAEP | Widely deployed standard |
| ElGamal | Yes | No | Yes | Probabilistic encryption |
| Paillier | Yes | No | Yes | Additive homomorphism |
| Rabin | Yes | No | No, deterministic square mapping | Encryption is very fast |
| Module | Main Caution |
|---|---|
| RSA | Requires secure padding; textbook RSA should never be used directly |
| ElGamal | Ciphertext malleability means it should be paired with authentication |
| Paillier | Intended for controlled arithmetic on ciphertexts, not general-purpose authenticated encryption |
| Rabin | Decryption ambiguity requires extra formatting or padding |
Plaintext m
-> c = m^e mod n
-> m = c^d mod n
Public key (p, g, y)
Random k
C1 = g^k mod p
C2 = m * y^k mod p
c = (g^m * r^n) mod n^2
m = L(c^lambda mod n^2) * mu mod n
c = m^2 mod N
4 square roots are recovered during decryption
from Modules.Asymmetric_Key_Cryptography.Public_Key_Encryption.rsa import rsa_menu
from Modules.Asymmetric_Key_Cryptography.Public_Key_Encryption.ElGamal import elgamal_menu
from Modules.Asymmetric_Key_Cryptography.Public_Key_Encryption.Paillier import paillier_menu
from Modules.Asymmetric_Key_Cryptography.Public_Key_Encryption.Rabin import rabin_menu
# Launch a specific module menu
rsa_menu()- Keep each module interactive and easy to inspect from the command line.
- Save keys and ciphertext output into
samples/for reuse. - Provide a built-in explainer for the math behind each algorithm.
- Use consistent menu flows so the user can switch between schemes easily.