An RSA encryption library implemented in C with optimized key generation, encryption, and decryption capabilities.
- β Key generation: Public and private key pairs
- β Cryptographically Secure: Uses Miller-Rabin primality testing with 50 iterations
- β Encryption: Uses RSA encryption
- β GNU Multiple Precision Arithmetic Library (GMP): handles large integers critical for RSA cryptographic operations
- β Cross-Platform: Works on Linux and macOS
- β CLI Interface: Unix-style command-line arguments
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Key Generation β β Encryption β β Decryption β
βββββββββββββββββββ€ βββββββββββββββββββ€ βββββββββββββββββββ€
β 1. Generate p,q β β 1. Read message β β 1. Read cipher β
β 2. Compute n=pq β β 2. Pad message β β 2. Decrypt β
β 3. Compute Ο(n) β β 3. Encrypt β β 3. Remove pad β
β 4. Choose e β β 4. Write output β β 4. Write output β
β 5. Compute d β β β β β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
- Key Generation: Based on the difficulty of factoring large integers
- Encryption:
c = m^e mod n - Decryption:
m = c^d mod n - Security: Relies on the RSA problem (computing e-th roots modulo n)
- C Compiler: GCC 7.0+ or Clang 6.0+
- GMP Library: GNU Multiple Precision Arithmetic Library
- Make: GNU Make or compatible
# Ubuntu/Debian
sudo apt-get install libgmp3-dev build-essential
# macOS
brew install gmp# Clone the repository
git clone https://github.com/nochoy/RSA-Encryption.git
# Open repository
cd RSA-Encryption
# Build
make clean && make
./keygen [OPTIONS]
Options:
-b <bits> Key size in bits (1024, 2048, 4096)
[default: 2048]
-n <file> Output public key file
[default: rsa pub]
-d <file> Output private key file
[default: rsa.priv]
-s <seed> Random seed for reproducible keys
-v Verbose output./encrypt [OPTIONS]
Options:
-i <file> Input file to encrypt
-o <file> Output encrypted file [default: encrypted.bin]
-n <file> Public key file [default: rsa.pub]
-v Verbose output./decrypt [OPTIONS]
Options:
-i <file> Input file to decrypt
-o <file> Output decrypted file [default: decrypted.txt]
-n <file> Private key file [default: rsa.priv]
-v Verbose output# Generate RSA key pair
./keygen -b 2048 -n public.pem -d private.pem
# Encrypt a message
echo "Hello, RSA!" > message.txt
./encrypt -i message.txt -o encrypted.bin -n public.pem
# Decrypt the message
./decrypt -i encrypted.bin -o decrypted.txt -d private.pem
# Clean
make clean./demo.shThis Will:
- Build the project
- Create sample files
- Demonstrate key generation & encryption/decription
- Compare key sizes
- Show security features
# Create demo file
echo "This is a secret message!" > secret.txt
# Generate keys
./keygen -b 2048 -n public.pem -d private.pem
# Encrypt
./encrypt -i secret.txt -o secret.enc -n public.pem
# Decrypt
./decrypt -i secret.enc -o secret_decrypted.txt -d private.pem
# Verify
diff secret.txt secret_decrypted.txt && echo "Success!"RSA-Encryption/
βββ keygen.c # Key generation program
βββ encrypt.c # Encryption program
βββ decrypt.c # Decryption program
βββ rsa.c/.h # Core RSA implementation
βββ numtheory.c/.h # Number theory utilities
βββ randstate.c/.h # Random state management
βββ examples/ # Example files
βββ Makefile # Build configuration
βββ demo.sh # Interactive Demo script
βββ README.md # This file
- Key Size: Use at least 2048-bit keys for production
- Key Storage: Store private keys securely with appropriate permissions
- Random Generation: Use high-quality random sources
- Padding: Always use proper padding schemes
- Message Size: Limited by key size (2048-bit = 256 bytes max)
- Performance: Slower than symmetric encryption
- Use Case: Best for key exchange, not bulk encryption
The RSA key generation follows these mathematical steps:
1. Generate two distinct large prime numbers p and q
2. Verify primality using Miller-Rabin test with 50 iterations
3. Ensure |p - q| is sufficiently large to prevent Fermat factorization
n = p Γ q
Where n becomes the RSA modulus (public and private)
Ο(n) = (p-1) Γ (q-1)
This represents the count of integers up to n that are coprime to n
Choose e such that:
- 1 < e < Ο(n)
- gcd(e, Ο(n)) = 1
- Common choices: e = 3, 17, or 65537 (2^16 + 1)
d β‘ e^(-1) mod Ο(n)
Where d is the modular multiplicative inverse of e modulo Ο(n)
ciphertext = plaintext^e mod n
The program encrypts files in blocks, rather than the entire file at once. The ciphertext is written to the output as hexstring.
plaintext = ciphertext^d mod n
The program decrypts files in blocks, using the private key d and modulus n. The decoded text is written to the output in plaintext.
The core operation uses the efficient square-and-multiply algorithm to efficiently compute large exponents:
function modular_exponentiation(base, exponent, modulus):
result = 1
base = base mod modulus
while exponent > 0:
if exponent mod 2 = 1:
result = (result Γ base) mod modulus
exponent = exponent >> 1
base = (base Γ base) mod modulus
return result
Let k be the number of bits in the RSA key.
| Operation | Time Complexity | Description |
|---|---|---|
| Key Generation | O(kβ΅) | Dominated by prime number generation. |
| Prime Testing | O(kβ΄) | Miller-Rabin test with a fixed number of iterations. |
| Encryption | O(kΒ²) | Modular exponentiation with a small public exponent. |
| Decryption | O(kΒ³) | Modular exponentiation with a large private exponent. |
| Component | Space Complexity | Description |
|---|---|---|
| Key Storage | O(k) | Storage for public and private keys. |
| Intermediate | O(k) | Temporary variables used during calculations. |
| Total Memory | O(k) | The memory usage is linear with the key size. |
Problem: fatal error: gmp.h: No such file or directory
# Ubuntu/Debian
sudo apt-get install libgmp3-dev
# macOS
brew install gmpProblem: undefined reference to '__gmpz_init'
# Ensure GMP library is linked
make clean && make
# Or manually:
gcc rsa.c keygen.c -lgmp -o keygenProblem: Private key file permissions too open
# Secure private key permissions
chmod 600 private.pemNOTE: This program was modified from a Computer Systems and C Programming course assignment. All header files were provided by Professor Darrell Long at UC Santa Cruz.
