An educational, highly robust C++20 implementation of the RSA cryptographic algorithm. This project was developed from scratch based on purely mathematical principles to demonstrate key generation, encryption, and decryption routines within an interactive terminal environment.
⚠️ Educational Disclaimer: This repository is intended strictly for educational and demonstration purposes. It is designed to visualize the underlying mathematics of RSA and strengthen low-level programming skills. It is explicitly not suited for production-grade security as cryptographic keys are exposed during runtime.
- Robust Custom Mathematical Library (
DivAlgs): Fully decoupled from the core RSA logic using a clean C++ namespace. It contains highly optimized number theory algorithms, including a hardware-friendly Right-to-Left Square-and-Multiply variant for efficient modular exponentiation. - The "Rigid" Constructor (Input Validation): Implements maximal defensive programming. The constructor rigorously validates user inputs before any cryptographic operations occur:
- Verifies if both inputs are valid prime numbers.
- Validates the user-defined encryption exponent
e. - Automated Fallback Chain: If the user's
eis mathematically invalid, the system automatically attempts to useFERMAT_F4(65537). If that fails, it executes a fallback routine starting at 5 with steps of+=2to find the first valide. If no valid exponent can be determined, it prompts the user to select larger primes.
- Cross-Platform Consistency (Windows & Linux): Engineered to deliver an identical terminal experience across different operating systems. To combat the issue of Linux terminals utilizing 32-bit character representations (which typically results in broken rectangle symbols for non-standard characters), the processed numerical values are safely downcast to
uint16_tspecifically for text output. This ensures a broad coverage of clean Extended ASCII / Unicode 16 output on both systems.
The primary engineering highlight of this project was translating the manual, pen-and-paper matrix calculation of the Extended Euclidean Algorithm (EEA) into an optimized codebase. Instead of allocating large, dynamic matrices or heavy structures, the modular inverse (the private exponent d) is calculated using a highly efficient static array of just 4 cells to track states, combining mathematical elegance with absolute minimal memory overhead.
To ensure correct behavior during large modular multiplications and prevent dangerous integer overflows, the codebase strictly utilizes standard fixed-width types:
uint64_tis deployed across all core cryptographic operations and modular arithmetic.int64_tis selectively utilized inside the helper structures of the Extended Euclidean Algorithm to safely handle negative coefficients during back-substitution.
RSA/
├── src/
│ ├── DivAlgs.h # Division algorithms & Square-and-Multiply (namespace)
│ ├── RSA.h # RSA class declaration (interface)
│ ├── RSA.cpp # RSA implementation (validation, core logic & runtime loop)
│ └── main.cpp # Application entry point (instantiation & execution only)
├── .gitignore # Specifies intentionally untracked files to ignore
├── LICENSE # MIT License File
└── README.md # Project documentation and architecture overview
# Clone the repository
git clone https://github.com/iibram/RSA.git
# Change directory
cd RSA
# Build using g++ with O2 Optimization flag
g++ -std=c++20 -O2 src/*.cpp -o rsa_demo
# Run the interactive demonstration
./rsa_demoThe application splits the execution into a strict setup phase followed by an interactive runtime loop:
- The Strict Setup Phase (One-Time): Before entering the loop, the system rigorously validates the user's prime numbers and calculates the public and private keys. Security standards like
FERMAT_F4or the automated fallback routines are executed here to ensure a mathematically flawless RSA environment. - The Interactive Conversation Loop: Once the cryptographic session is successfully established, the continuous loop starts. The user can type any arbitrary text input.
- Instant Verification & Live Toggle: Upon pressing [ENTER], the system displays the encrypted cipher values alongside the instantly decrypted string for live verification. At any point during input, the user can press [F1] to dynamically toggle the visual encryption mode between
WCHAR,DEC(Decimal), andHEX(Hexadecimal) values in real-time. - Graceful Exit: The user can safely break out of the continuous loop and terminate the application at any time by pressing the [ESC] key.
- Dynamic Output Toggle (Done!) 🚀
- Implementation: Integrated a real-time display mode switcher triggered by the [F1] key. Leveraged our unbuffered, cross-platform architecture (
_getwch()on Windows /termios&select()on Linux) to dynamically rotate betweenWCHAR,DEC, andHEXmodes instantly, without interrupting the user's input stream.
- Implementation: Integrated a real-time display mode switcher triggered by the [F1] key. Leveraged our unbuffered, cross-platform architecture (
- Next Milestone: File-Stream Encryption 📂
- Extend the RSA engine to handle direct file encryption and decryption for large
.txtor binary files, maintaining the same robust performance.
- Extend the RSA engine to handle direct file encryption and decryption for large