A complete Python implementation for secure RSA operations including encryption, decryption, digital signatures, and verification.
- Prerequisites
- Installation
- Initial Setup
- Key Exchange Protocol
- Usage Guide
- Security Architecture
- Best Practices
- Cryptographic Specifications
- Python 3.8+
- pip package manager
- Clone the repository:
git clone https://github.com/rezak9876/rsa-tool.git
cd rsa-toolInstall required dependencies:
pip install cryptographyGenerate your cryptographic key pair:
cd keys
python key_gen.pyThis will:
- Generate a 2048-bit RSA private key in
keys/my/private_key.pem - Display your public key in PEM format (copy this for sharing)
- Get the contact's public key file (.pem)
- Save it as:
keys/others/<name>.pem
Example: To add my public key:
cat > keys/others/rezak.pem << 'EOF'
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2JOlS70z/u7WvutfPRhv
YB8iDErPh+YyMmGxHgH/nbyAz/FVua+TF7d4Hj0EcNHEVouKTP2CSvm0GxCZGclh
b8tcqyUf1xeLkRH7IsMKW2GWSpWP/TopSfRv9EHD8OPE44x1RcFY1f8cMJh/m2NP
++TNsDhuT4YUA5xG68ESeOzZoXHPpN45z3NVgMf8NSNxucCTwl0gwTNkpB970jI8
HLNYzUgtFSZIs7RjZ1RZm4w/fSPf9ahgznB1ycTWaxYD4Ax/B1iJAxQWBRN/mc+r
WuKEqmCeFfsvis+lAsrD9V5dKCS1eilu8k3aSrBwjWBJ6dg0ihQr9iaR+hT91sQe
QQIDAQAB
-----END PUBLIC KEY-----
EOF- Always verify that a public key truly belongs to the intended person by checking their signed message after adding their public key. Below I'll explain the signature verification process:
Verify a signed message's authenticity:
python rsa_tool.py verify sender_name "original message" "signature"Example:
python rsa_tool.py verify rezak "I'm RezaK" "0RaTgzg9H/U38iODMXAI/rLfwoGf0kg7FeDQcGkvSGWCLJFKlXpvd3aoswMTVLtuR20z+1I+t9F+MTfTsnrSp5ILEsJvCGwpdFyuc+xbIsbgA+o+dvRDjLGVzIitbuAnbA0Rj49dsfSDPj9GQGErjtPXM32AJVSL5EWoI5sxzJRANEwsO/AFJM4mxtyyyHpT17FyT8KYTCcHYYBxy7VtdZM0lbJihmw68cj5viJqc1Hqk/uyFX2JMGrqyHZkGnqEN23zAmf+anMe5uc6jhh/eQSqHHqeWUJCL9K3UTkSmnQ/O21LE2S2vddT0F64o393Oipd0oTMdPB5chIPLFfEmw=="Output:
✅ Signature is VALID — The message is authentic
❌ Signature is INVALID — Possible tampering detected
Create a digital signature for your message:
python rsa_tool.py sign "Your message text"Output:
🔏 Your signature: 0RaTgz...LFfEmw==
When you want to send an encrypted message to someone:
- You must use their PUBLIC key to encrypt your message
- Only they can decrypt it with their PRIVATE key
- You can safely send the encrypted message through any channel
When you receive an encrypted message:
- The sender used YOUR public key to encrypt it
- Only YOU can decrypt it using your PRIVATE key
- This ensures the message remains confidential during transmission
Encrypt a message for a specific recipient:
python rsa_tool.py encrypt "Secret message" recipient_nameOutput:
🔒 Encrypted message: dGhpcy...ZQ==
Decrypt a received message:
python rsa_tool.py decrypt "ENCRYPTED_MESSAGE_BASE64"Output:
📩 Decrypted message: This is the original secret message
project/
├── keys/
│ ├── my/
│ │ └── private_key.pem # NEVER SHARE
│ └── others/
│ ├── alice.pem # Contacts' public keys
│ └── bob.pem
└── rsa_tool.py # Main application
- Always verify new public keys via secondary channel
- Set strict file permissions:
chmod 600 keys/my/private_key.pem - Backup keys securely
- Regenerate keys annually or after suspected compromise
- Use different keys for different purposes
- Always sign before encrypting
Based on NIST Special Publication 800-57B Revision 1 recommendations.
- Key Length: 2048 bits (NIST recommended minimum for RSA)
- Public Exponent (e): 65537 (F4)
- Padding Scheme: PKCS#1 v2.1 (OAEP for encryption, PSS for signatures)
- Hash Algorithm: SHA-256
- Signature Scheme: RSA-PSS (Probabilistic Signature Scheme)
- Key Encapsulation: RSA-OAEP
- Data Encryption: AES-256-GCM
-
"Invalid Key Format" Error
- Ensure the key file is in valid PEM format
- Check file permissions
- Verify the key hasn't been corrupted
-
"Signature Verification Failed"
- Confirm you're using the correct public key
- Ensure the message hasn't been modified
- Check if the signature was properly base64 encoded
-
"Encryption Failed"
- Verify the recipient's public key is valid
- Check if the message size is within RSA limits
- Ensure proper character encoding (UTF-8)