Skip to content

nochoy/RSA-Encryption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” RSA Cryptography

An RSA encryption library implemented in C with optimized key generation, encryption, and decryption capabilities.

🎬 Live Demo

demo

🎯 Features

  • βœ… 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

πŸ” Technical Architecture

RSA Algorithm Implementation

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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    β”‚    β”‚                 β”‚    β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Mathematical Foundation

  • 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)

πŸ“‹ Installation

Prerequisites

  • C Compiler: GCC 7.0+ or Clang 6.0+
  • GMP Library: GNU Multiple Precision Arithmetic Library
  • Make: GNU Make or compatible

Install GMP Library

# Ubuntu/Debian
sudo apt-get install libgmp3-dev build-essential

# macOS
brew install gmp

Build Instructions

# Clone the repository
git clone https://github.com/nochoy/RSA-Encryption.git

# Open repository
cd RSA-Encryption

# Build
make clean && make

πŸ“– Usage Guide

Key Generation

./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

Encryption

./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

Decryption

./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

Examples

# 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

πŸ§ͺ Testing & Demo

Interactive Demo (Linux/macOS)

./demo.sh

This Will:

  1. Build the project
  2. Create sample files
  3. Demonstrate key generation & encryption/decription
  4. Compare key sizes
  5. Show security features

Manual Testing

# 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!"

πŸ“ Project Structure

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

πŸ›‘οΈ Security Considerations

Best Practices

  • 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

Limitations

  • 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

πŸ”¬ Algorithm Details

RSA Key Generation Process

The RSA key generation follows these mathematical steps:

1. Prime Number Generation

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

2. Modulus Calculation

n = p Γ— q

Where n becomes the RSA modulus (public and private)

3. Euler's Totient Function

Ο†(n) = (p-1) Γ— (q-1)

This represents the count of integers up to n that are coprime to n

4. Public Exponent Selection

Choose e such that:
- 1 < e < Ο†(n)
- gcd(e, Ο†(n)) = 1
- Common choices: e = 3, 17, or 65537 (2^16 + 1)

5. Private Exponent Calculation

d ≑ e^(-1) mod Ο†(n)

Where d is the modular multiplicative inverse of e modulo Ο†(n)

RSA Encryption Process

Encryption Algorithm

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.

RSA Decryption Process

Decryption Algorithm

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.

Modular Exponentiation (Square-and-Multiply)

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

πŸ“Š Complexity Analysis

Let k be the number of bits in the RSA key.

Time Complexity

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.

Space Complexity

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.

πŸ”§ Troubleshooting

Common Issues

Build Errors

Problem: fatal error: gmp.h: No such file or directory

# Ubuntu/Debian
sudo apt-get install libgmp3-dev

# macOS
brew install gmp

Problem: undefined reference to '__gmpz_init'

# Ensure GMP library is linked
make clean && make
# Or manually:
gcc rsa.c keygen.c -lgmp -o keygen

Permission Errors

Problem: Private key file permissions too open

# Secure private key permissions
chmod 600 private.pem

NOTE: 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.

About

A RSA cryptosystem to encrypt and decrypt messages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published