cryptrsa: RSA CLI and library
A small Rust CLI/library that demonstrates RSA primitives for learning, and provides secure flows using OAEP (encryption), PSS (signing), and a hybrid RSA+AES-GCM scheme for large files.
Highlights
- Raw RSA primitives (BigUint) for teaching and simple demos
- Chunked raw RSA file encryption with a length header
- RSA-OAEP (SHA-256) encryption/decryption with PEM keys
- RSA-PSS (SHA-256) signing/verification with PEM keys
- Hybrid RSA+AES-256-GCM for large files
- JSON keypair I/O for raw RSA; PEM I/O for OAEP/PSS
- Clean CLI plus a usable library API
Installation
Build from source
Quick start (raw RSA, JSON keys)
- Generate JSON keypair:
- Encrypt/Decrypt a short UTF-8 message (must be numerically < n):
- Encrypt/Decrypt arbitrary files with raw RSA chunking:
Secure modes (PEM keys)
- Generate PEM keys (PKCS#8 private, SPKI public):
- RSA-OAEP (SHA-256) encryption/decryption:
- RSA-PSS (SHA-256) signing/verification:
Hybrid RSA + AES-GCM (recommended for large files)
- Encrypt: AES-256-GCM for data, RSA-OAEP for the AES key. Output is JSON with base64 fields enc_key, nonce, ct.
- Decrypt:
CLI reference (subcommands)
- gen: Generate a JSON keypair; optionally write public part separately
- encrypt / decrypt: Raw RSA for short messages (as base64)
- sign / verify: Raw RSA signing; use --hash to sign SHA-256(msg)
- fingerprint: Print SHA-256 fingerprint of a public key (e||n)
- encrypt-bytes / decrypt-bytes: Raw RSA for arbitrary files (chunked); decrypt supports --base64
- info: Print basic info (type, bits, e, n)
- pubout: Extract public key JSON from a keypair JSON
- gen-pem: Generate RSA PEM keys
- encrypt-oaep / decrypt-oaep: RSA-OAEP (SHA-256) with PEM
- sign-pss / verify-pss: RSA-PSS (SHA-256) with PEM
- encrypt-hybrid / decrypt-hybrid: Hybrid RSA-OAEP + AES-256-GCM for large files
Library usage (examples)
- Raw RSA keygen and encrypt/decrypt:
use ;
use BigUint;
let kp = generate;
let m = from;
let c = encrypt;
let p = decrypt;
assert_eq!;
- OAEP/PSS with PEM:
use RsaOaep;
let kp = generate?;
let ct = kp.encrypt?;
let pt = kp.decrypt?;
assert_eq!;
let sig = kp.sign_pss?;
assert!;
- Hybrid AES-GCM:
use ;
let key = gen_random_key;
let = encrypt_aes_gcm?;
let pt = decrypt_aes_gcm?;
assert_eq!;
Security notes
- Prefer OAEP/PSS and the hybrid mode for any real use. Raw RSA (no padding) is for learning and is not CCA-secure.
- AES-GCM uses a random 96-bit nonce from the OS RNG; never reuse nonces with the same key.
- Always keep private keys secure; PEM files are written in plaintext by default.
License
This project is licensed under the MIT License - see the LICENSE file for details.