Chaocipher

Struct Chaocipher 

Source
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

Source

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

Source§

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

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 as src)
  • 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

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 as src)
  • 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);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.