Caesar Cipher Encryption/Decryption
A comprehensive Caesar cipher implementation in Rust with both library and CLI functionality. Supports uppercase and lowercase letters with robust error handling.
Features
- ๐ Caesar cipher encryption and decryption
- ๐ค Both uppercase and lowercase letter support
- โก Fast and efficient implementation
- ๐ก๏ธ Safe functions with error handling
- ๐ป Command-line interface (CLI)
- ๐ File input/output support
- ๐ Brute force decryption
- ๐ฏ Interactive mode
- โ Comprehensive test coverage
Installation
Add this to your Cargo.toml:
[]
= "1.0.10"
Note: This version is pinned for documentation consistency with this repository (
Cargo.toml). For the latest release, always check crates.io.
Library Usage
Basic Usage
use ;
Basic vs Safe APIs
| API | Shift range | Empty / whitespace-only text |
|---|---|---|
encrypt / decrypt |
Any i16 (normalized mod 26) |
Allowed |
encrypt_safe / decrypt_safe |
-25 to 25 only | Returns CipherError::EmptyText |
Use *_safe when you want validation errors instead of silent normalization.
Safe Functions with Error Handling
use ;
Brute Force Decryption
use decrypt;
CLI Usage
Installation
Basic Commands
# Encrypt text
# Decrypt text
# Interactive mode
# Brute force decryption (try all shifts)
# Show help
File Operations
# Encrypt from file
# Decrypt from file
Safe Mode
# Use safe mode with error checking
Without --safe, the CLI accepts any i16 shift (values are normalized modulo 26) and allows empty input. With --safe, shift must be in -25..=25 and text must not be empty or whitespace-only.
Input limits and file requirements
Maximum payload size is 10 MB (MAX_INPUT_SIZE in config). How the cap is applied depends on the input path:
| Input path | What is limited | Notes |
|---|---|---|
--text |
String length | Exactly 10 MB is allowed (len > MAX is rejected). |
--file |
Entire file size | Exactly 10 MB files are allowed. Must be a regular file (not a directory, device, or FIFO). Read uses a streaming byte cap. |
| Stdin / interactive line | Line content before \n |
Up to 10 MB of content per line; \n is not counted toward the cap (buffer may be up to 10 MB + 1 byte). EOF without newline also allows exactly 10 MB. |
Additional notes:
- Shift prompts in interactive mode are capped at 64 bytes per line (
MAX_SHIFT_LINE_SIZE). - Stdin uses chunked reads (8 KB buffer) for performance; the 10 MB cap still applies to line content.
- If you raise
MAX_INPUT_SIZEin the future, consider tuning the read buffer inbounded_input.rs.
Supported Characters
- Uppercase letters: A-Z
- Lowercase letters: a-z
- Other characters: Numbers, symbols, and non-ASCII characters remain unchanged
Examples
use *;
Testing
Run the comprehensive test suite:
Error Types
The library includes custom error types for better error handling:
CipherError::EmptyText: When input text is emptyCipherError::InvalidShift: When shift value is outside the valid range (-25 to 25)
Performance
This implementation is optimized for performance with:
- Zero-allocation character processing
- Efficient modular arithmetic
- Minimal bounds checking
Links
License
This project is licensed under the MIT License.