cryptograph
Educational Rust library for cryptography, bit manipulation, and number theory.
This crate is focused on understanding how algorithms work internally, with direct, readable implementations.
Current status
- Learning-focused, not production security-focused.
- Includes classical ciphers (Caesar, Affine), XOR stream cipher helpers, LFSR utilities, and DES components.
DESis partial:Des::encryptexists, butDes::decryptis currently a placeholder (returns7).- Backward-compatible aliases are still available for older names such as
cryptography::cesarandstreams_ciphers::generete_seed.
Installation
or in Cargo.toml:
[]
= "0.1"
Project structure
cryptograph/
├── Cargo.toml
├── README.md
└── src/
├── bits.rs
├── lib.rs
├── cryptoanalysis/
│ ├── mod.rs
│ └── reverse_lfsr.rs
├── cryptography/
│ ├── mod.rs
│ ├── affine/
│ │ ├── mod.rs
│ │ ├── encrypt.rs
│ │ └── decrypt.rs
│ ├── caesar/
│ │ ├── mod.rs
│ │ ├── encrypt.rs
│ │ └── decrypt.rs
│ ├── des/
│ │ ├── mod.rs
│ │ ├── encrypt.rs
│ │ ├── decrypt.rs
│ │ ├── e_box.rs
│ │ ├── f.rs
│ │ ├── key.rs
│ │ ├── permutation.rs
│ │ └── s_box.rs
│ └── streams_ciphers/
│ ├── mod.rs
│ ├── encrypt.rs
│ ├── decrypt.rs
│ └── generate_seed.rs
├── math/
│ ├── mod.rs
│ ├── euclides.rs
│ ├── bezout.rs
│ └── multiplicative_inverse.rs
├── pseudorandom_generator/
│ ├── mod.rs
│ └── lfsr.rs
└── tools/
├── mod.rs
└── flip_flop.rs
Public API (full summary)
bits
bits_to_bytes(bits: &[bool]) -> Result<Vec<u8>, Box<dyn Error>>bytes_to_bits(bytes: &[u8]) -> Vec<bool>
math
euclides::gcd<T>(a: T, b: T) -> Tbezout::bezout<T>(a: T, b: T) -> (T, T)multiplicative_inverse::multiplicative_inverse<T>(a: T, m: T) -> Option<T>
tools
FlipFlopnew(flag: bool) -> Selfget(&self) -> boolmutate(&mut self)mutate_and_get(&mut self) -> boolput(&mut self, f: bool)set(&mut self, f: bool) -> &mut Self
pseudorandom_generator
Lfsr<F: Fn(&[FlipFlop]) -> bool>(with public fieldsffandfb)new(ff: Vec<FlipFlop>, rule: F) -> Selfrotate(&mut self) -> &mut FlipFlopget(&self) -> &[FlipFlop]calculate_possibilities(&self) -> usize
cryptography::caesar
encrypt::caesar_encrypt(msg: &str, shift: u8) -> Result<String, FromUtf8Error>decrypt::caesar_decrypt(msg: &str, shift: u8) -> Result<String, FromUtf8Error>
cryptography::affine
encrypt::affine_encrypt(x: &str, a: u8, b: u8) -> Result<String, FromUtf8Error>decrypt::affine_decrypt(y: &str, a: i32, b: u8) -> Result<String, FromUtf8Error>
cryptography::streams_ciphers
encrypt::stream_cipher_crypt(x: Vec<bool>, seed: Vec<bool>) -> Vec<bool>encrypt::fut_stream_cipher_encrypt(x: Vec<bool>, seed: Vec<bool>) -> impl StreamExt<Item = bool>decrypt::fut_stream_cipher_decrypt(y, seed) -> Vec<bool>(async)generate_seed::generate_seed(n: usize) -> Vec<bool>
cryptography::des
encrypt::Desnew(x: u64) -> Selfencrypt(&self, key: u64) -> u64decrypt(y: u64) -> u64(current placeholder)
- Public internal helpers:
encrypt::round(...) -> (u32, u32)e_box::e_box(bits: u32) -> u64f::f(bits: u32, k: u64) -> u32key::permutated_choice_1(key: u64) -> u64key::permutated_choice_2(left: u32, right: u32) -> u64key::key_shift(left_key: u32, right_key: u32, n: u8) -> (u32, u32)permutation::initial_permutation(x: u64) -> u64permutation::final_permutation(x: u64) -> u64s_box::s_box(right_x: u64) -> u32s_box::SBOXES
cryptoanalysis
reverse_lfsr::reverse_lfsr(...) -> Result<Vec<FlipFlop>, Box<dyn Error>>
Usage examples
Bytes <-> bits conversion
use ;
let bytes = vec!;
let bits = bytes_to_bits;
let restored = bits_to_bytes.unwrap;
assert_eq!;
Modular math
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Caesar cipher
use caesar_decrypt;
use caesar_encrypt;
let encrypted = caesar_encrypt.unwrap;
let decrypted = caesar_decrypt.unwrap;
assert_eq!;
Affine cipher
use affine_decrypt;
use affine_encrypt;
let a: u8 = 3;
let b: u8 = 7;
let encrypted = affine_encrypt.unwrap;
let decrypted = affine_decrypt.unwrap;
assert_eq!;
Stream cipher (bitwise XOR)
use stream_cipher_crypt;
let x = vec!;
let seed = vec!;
let y = stream_cipher_crypt;
let restored = stream_cipher_crypt;
assert_eq!;
Random seed for stream cipher
use generate_seed;
let seed = generate_seed;
assert_eq!;
FlipFlop and LFSR
use Lfsr;
use FlipFlop;
let ff = vec!;
let mut lfsr = new;
lfsr.rotate;
let state = lfsr.get;
assert_eq!;
assert_eq!;
Reverse LFSR (cryptoanalysis)
use reverse_lfsr;
use FlipFlop;
let y = vec!;
let x = vec!;
let result = reverse_lfsr;
assert!;
DES encrypt (64-bit block)
use Des;
let plaintext = 0x0123_4567_89AB_CDEFu64;
let key = 0x1334_5779_9BBC_DFF1u64;
let des = new;
let encrypted = des.encrypt;
println!;
Known errors, panics, and limitations
math::euclides::gcdmay panic ifb == 0(division/modulo by zero).affine_decryptusesunwrap()on the modular inverse and may panic ifais not invertible modulo 256.stream_cipher_cryptandfut_stream_cipher_encryptuseassert_eq!to enforce matching message/seed length.reverse_lfsrreturns an error ifx.len() < n.Des::decryptis not implemented yet.
Security
This project is educational only.
- Not audited
- Not constant-time
- Not production-safe
If you need real-world cryptography, use audited libraries
(for example RustCrypto crates or ring).
Local development
License
MIT