Skip to content

iibram/RSA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

RSA Demonstration Environment

C++ License Purpose Category OS

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.


🚀 Key Features & Architectural Highlights

  • 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 e is mathematically invalid, the system automatically attempts to use FERMAT_F4 (65537). If that fails, it executes a fallback routine starting at 5 with steps of +=2 to find the first valid e. 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_t specifically for text output. This ensures a broad coverage of clean Extended ASCII / Unicode 16 output on both systems.

🛠️ Technical Insights & Engineering Challenges

The 4-Cell Array Extended Euclidean Algorithm

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.

Data Type Strategy

To ensure correct behavior during large modular multiplications and prevent dangerous integer overflows, the codebase strictly utilizes standard fixed-width types:

  • uint64_t is deployed across all core cryptographic operations and modular arithmetic.
  • int64_t is selectively utilized inside the helper structures of the Extended Euclidean Algorithm to safely handle negative coefficients during back-substitution.

📁 Repository Structure

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

💻 Building and Running the Project

# 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_demo

🔄 Workflow & Demonstration Loop

The application splits the execution into a strict setup phase followed by an interactive runtime loop:

  1. 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_F4 or the automated fallback routines are executed here to ensure a mathematically flawless RSA environment.
  2. The Interactive Conversation Loop: Once the cryptographic session is successfully established, the continuous loop starts. The user can type any arbitrary text input.
  3. 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), and HEX (Hexadecimal) values in real-time.
  4. Graceful Exit: The user can safely break out of the continuous loop and terminate the application at any time by pressing the [ESC] key.

🗺️ Future Roadmap

  • 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 between WCHAR, DEC, and HEX modes instantly, without interrupting the user's input stream.
  • Next Milestone: File-Stream Encryption 📂
    • Extend the RSA engine to handle direct file encryption and decryption for large .txt or binary files, maintaining the same robust performance.

About

An educational C++20 RSA cryptography demonstration built from scratch for Windows & Linux. Features custom number theory algorithms (Square-and-Multiply, EEA) and a validated runtime loop.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages