Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

@AliAlimohammadi AliAlimohammadi commented Dec 13, 2025

Description

Adds the RSA (Rivest-Shamir-Adleman) cipher algorithm to the ciphers module.

This algorithm implements asymmetric encryption using public-key cryptography, where encryption and decryption use different keys. The implementation demonstrates core RSA concepts including key generation, modular exponentiation, and the Extended Euclidean Algorithm.

Implementation Details

  • Key generation from two prime numbers (p, q)
  • Modular exponentiation using square-and-multiply algorithm for efficiency
  • Extended Euclidean Algorithm for computing modular inverse
  • Public key encryption: C = M^e mod n
  • Private key decryption: M = C^d mod n
  • Text encryption/decryption by converting characters to numeric values
  • Strong type safety with PublicKey and PrivateKey structs
  • Time Complexity: O(log n) for encryption/decryption (modular exponentiation)
  • Space Complexity: O(1)

Examples

// Generate keypair from two primes
let (public, private) = generate_keypair(61, 53);

// Encrypt and decrypt a number
let message = 65;
let encrypted = encrypt(message, &public);
let decrypted = decrypt(encrypted, &private);
assert_eq!(message, decrypted);

// Encrypt and decrypt text
let text = "HELLO";
let encrypted = encrypt_text(text, &public);
let decrypted = decrypt_text(&encrypted, &private);
assert_eq!(text, decrypted);

Security Warning

⚠️ This is an educational implementation demonstrating RSA concepts. DO NOT use for production cryptography! Use established libraries like ring, rust-crypto, or openssl for real-world applications.

Limitations:

  • Small key sizes (educational purposes)
  • No padding schemes (OAEP, PKCS#1)
  • Vulnerable to timing attacks
  • Not constant-time operations

Testing

  • Added comprehensive unit tests covering:
    • GCD computation (Euclid's algorithm)
    • Modular inverse calculation (Extended Euclidean)
    • Modular exponentiation (square-and-multiply)
    • Key generation with various prime pairs
    • Number encryption/decryption
    • Text encryption/decryption
    • Key property verification
    • Edge cases and various input sizes
  • All 12 tests pass with cargo test
  • Code formatted with cargo fmt
  • No clippy warnings

Checklist

  • I have tested the code
  • I have run cargo fmt
  • I have run cargo clippy
  • I have added documentation
  • I have added tests
  • I have included security warnings

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 98.63014% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.64%. Comparing base (e877687) to head (ce661e5).

Files with missing lines Patch % Lines
src/ciphers/rsa_cipher.rs 98.63% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #967      +/-   ##
==========================================
+ Coverage   95.62%   95.64%   +0.01%     
==========================================
  Files         339      340       +1     
  Lines       22078    22224     +146     
==========================================
+ Hits        21113    21257     +144     
- Misses        965      967       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@siriak siriak merged commit 6540508 into TheAlgorithms:master Dec 13, 2025
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-rsa-cipher branch December 13, 2025 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants