SquareCipher

Struct SquareCipher 

Source
pub struct SquareCipher { /* private fields */ }
Expand description

A Square Cipher that implements fractionating substitution.

The cipher maintains both encryption and decryption mappings between single characters and character pairs (bigrams). The key determines the arrangement of characters in the square, while the character set (chrs) determines the symbols used for encoding coordinates.

§Fields

  • key - The keyword used to initialize the cipher square
  • chrs - The character set used for bigram generation (e.g., “ADFGVX” or “012345”)
  • alpha - The condensed alphabet used to populate the cipher square
  • enc_table - Fast encryption table from plaintext byte to bigram bytes
  • dec_table - Fast decryption table from bigram bytes to plaintext byte

Implementations§

Source§

impl SquareCipher

Source

pub fn new(key: &str, chrs: &str) -> Result<Self>

Creates a new Square Cipher with the given key and character set.

The key is combined with BASE36 alphabet and condensed to remove duplicate characters. The character set determines which symbols will be used for the bigram encoding.

§Arguments
  • key - A non-empty keyword to initialize the cipher square
  • chrs - A non-empty character set for bigram generation (length should match square dimensions)
§Returns
  • Ok(SquareCipher) - Successfully created cipher
  • Err(String) - Error message if key or chrs is empty
§Example
use old_crypto_rs::SquareCipher;

// Create ADFGVX cipher with "PORTABLE" key
let cipher = SquareCipher::new("PORTABLE", "ADFGVX").unwrap();

// Create numeric variant with "ARABESQUE" key
let cipher2 = SquareCipher::new("ARABESQUE", "012345").unwrap();
§Errors

Returns an error if either key or chrs is an empty string.

Trait Implementations§

Source§

impl Block for SquareCipher

Source§

fn block_size(&self) -> usize

Returns the block size for this cipher.

The block size is equal to the key length. This determines how many characters are processed together during encryption/decryption operations.

§Returns

The length of the cipher key in bytes.

Source§

fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize

Encrypts plaintext into ciphertext using the Square Cipher.

Each byte in the source is replaced by a two-character bigram, effectively doubling the length of the output. The bigram represents the row and column coordinates of the character in the cipher square.

§Arguments
  • dst - Destination buffer for ciphertext (must be at least 2 * src.len())
  • src - Source plaintext bytes to encrypt
§Returns

The number of bytes written to dst (always 2 * src.len())

§Note

Characters not found in the encryption table are silently skipped.

Source§

fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize

Decrypts ciphertext back into plaintext using the Square Cipher.

Processes the source in pairs of characters (bigrams), looking up each bigram in the decryption table to recover the original character.

§Arguments
  • dst - Destination buffer for plaintext (must be at least src.len() / 2)
  • src - Source ciphertext bytes to decrypt (must have even length)
§Returns

The number of bytes written to dst (always src.len() / 2)

§Note

Bigrams not found in the decryption table are silently skipped.

Source§

impl Debug for SquareCipher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.