A lightweight and robust Rust library for classical cryptography. This library provides a clean, trait-based design (Cipher) to easily encrypt and decrypt text using classic algorithms, currently supporting both Caesar and Vigenère ciphers.
The project is structured to be simple yet powerful:
-
CipherTrait: The core interface definingcipheranddecipheroperations. -
Error Handling: Custom
CipherErrortypes built usingthiserrorto handle invalid alphabet inputs gracefully. -
High Performance: Built-in lower and upper
$26 \times 26$ byte grids (TABULA_RECTA) for fast lookup-based polyalphabetic substitutions.
- Requirements: Rust toolchain (2024 edition or later).
- Installation: Add this to your project's
Cargo.toml:
[dependencies]
project_init = { path = "path/to/project_init" }To build the library directly from the source:
cargo buildHere is how you can use the ciphers in your Rust application. Both implementations automatically preserve capitalization, spacing, and punctuation.
A simple shift cipher requiring a u8 key:
use project_init::{Ceaser, Cipher};
fn main() {
let caesar = Ceaser::new(3);
let encrypted = caesar.cipher("hello, world").unwrap();
println!("Encrypted: {}", encrypted); // Output: "khoor, zruog"
let decrypted = caesar.decipher(&encrypted).unwrap();
println!("Decrypted: {}", decrypted); // Output: "hello, world"
}A polyalphabetic cipher requiring an alphabetic keyword:
use project_init::{Cipher, Vigenere};
fn main() {
let vigenere = Vigenere::new("test");
let encrypted = vigenere.cipher("hello, world").unwrap();
println!("Encrypted: {}", encrypted); // Output: "aideh, agkeh"
let decrypted = vigenere.decipher(&encrypted).unwrap();
println!("Decrypted: {}", decrypted); // Output: "hello, world"
}- Core framework: Shared
Ciphertrait and unified error types powered bythiserror. - Tabula Recta grids: Built-in 2D lookup grids for polyalphabetic operations.
- Caesar cipher: Shifts text with key, preserving non-alphabetic elements.
- Vigenère cipher: Keyword-based shifting that handles non-alphabetic spaces seamlessly.
- Testing: Integration tests setup for all cipher implementations (
tests/).
To run the test suite:
cargo test