pub struct Chaocipher { /* private fields */ }Expand description
A Chaocipher instance with two permutation alphabets.
The Chaocipher struct maintains two 26-character alphabet keys (plaintext and cipher keys)
and an internal state that is updated after each character encryption/decryption.
The state is kept in a RefCell to allow interior mutability during const operations.
Implementations§
Source§impl Chaocipher
impl Chaocipher
Sourcepub fn new(pkey: &str, ckey: &str) -> Result<Self>
pub fn new(pkey: &str, ckey: &str) -> Result<Self>
Creates a new Chaocipher instance with the provided keys.
Both keys must be exactly 26 characters long (matching the standard English alphabet).
§Arguments
pkey- The plaintext alphabet key (right alphabet)ckey- The cipher alphabet key (left alphabet)
§Returns
Returns Ok(Chaocipher) if the keys are valid, or Err(String) if either key
has an incorrect length.
§Example
use old_crypto_rs::Chaocipher;
let cipher = Chaocipher::new(
"PTLNBQDEOYSFAVZKGJRIHWXUMC",
"HXUCZVAMDSLKPEFJRIGTWOBNYQ"
).unwrap();Trait Implementations§
Source§impl Block for Chaocipher
impl Block for Chaocipher
Source§fn block_size(&self) -> usize
fn block_size(&self) -> usize
Returns the block size of the cipher.
Chaocipher operates on single characters, so the block size is always 1.
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts the source data into the destination buffer.
The cipher state is reset before encryption begins, ensuring that each encryption operation starts from the initial key configuration.
§Arguments
dst- The destination buffer for encrypted data (must be at least as long assrc)src- The source plaintext data to encrypt
§Returns
The number of bytes encrypted (equal to the length of src)
§Example
use old_crypto_rs::{Block, Chaocipher};
let cipher = Chaocipher::new(
"PTLNBQDEOYSFAVZKGJRIHWXUMC",
"HXUCZVAMDSLKPEFJRIGTWOBNYQ"
).unwrap();
let plaintext = b"HELLO";
let mut ciphertext = vec![0u8; plaintext.len()];
cipher.encrypt(&mut ciphertext, plaintext);Source§fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts the source data into the destination buffer.
The cipher state is reset before decryption begins, ensuring that each decryption operation starts from the initial key configuration.
§Arguments
dst- The destination buffer for decrypted data (must be at least as long assrc)src- The source ciphertext data to decrypt
§Returns
The number of bytes decrypted (equal to the length of src)
§Example
use old_crypto_rs::{Block, Chaocipher};
let cipher = Chaocipher::new(
"PTLNBQDEOYSFAVZKGJRIHWXUMC",
"HXUCZVAMDSLKPEFJRIGTWOBNYQ"
).unwrap();
let ciphertext = b"OAHQH";
let mut plaintext = vec![0u8; ciphertext.len()];
cipher.decrypt(&mut plaintext, ciphertext);