pub struct StraddlingCheckerboard { /* private fields */ }Expand description
A straddling checkerboard cipher implementation.
This cipher maps plaintext characters to variable-length digit sequences. High-frequency letters are encoded as single digits, while low-frequency letters are encoded as two digits prefixed by one of the “long” cipher digits.
§Examples
use old_crypto_rs::StraddlingCheckerboard;
use old_crypto_rs::Block;
let cipher = StraddlingCheckerboard::new("ARABESQUE", "89").unwrap();
let mut encrypted = vec![0u8; 100];
let len = cipher.encrypt(&mut encrypted, b"ATTACK");
encrypted.truncate(len);Implementations§
Source§impl StraddlingCheckerboard
impl StraddlingCheckerboard
Sourcepub fn new(key: &str, chrs: &str) -> Result<Self>
pub fn new(key: &str, chrs: &str) -> Result<Self>
Creates a new straddling checkerboard cipher with default frequency.
Uses “ESANTIRU” as the default high-frequency letters and the standard alphabet (A-Z plus ‘/’ and ‘-’).
§Arguments
key- The keyword used to shuffle the alphabet (must not be empty)chrs- A string of at least 2 digits that will be used as “long” cipher digits
§Returns
Returns Ok(StraddlingCheckerboard) on success, or Err(String) if validation fails.
§Errors
Returns an error if:
keyis emptychrscontains fewer than 2 characters
§Examples
use old_crypto_rs::StraddlingCheckerboard;
let cipher = StraddlingCheckerboard::new("ARABESQUE", "89").unwrap();Sourcepub fn new_with_freq(
key: &str,
chrs: &str,
freq_str: &str,
alphabet: &str,
) -> Result<Self>
pub fn new_with_freq( key: &str, chrs: &str, freq_str: &str, alphabet: &str, ) -> Result<Self>
Creates a new straddling checkerboard cipher with custom frequency and alphabet.
Allows full customization of which letters get single-digit codes and what alphabet to use.
§Arguments
key- The keyword used to shuffle the alphabet (must not be empty)chrs- A string of at least 2 digits for “long” cipher digit prefixesfreq_str- Letters that should receive single-digit encodingsalphabet- The alphabet to use for the checkerboard
§Returns
Returns Ok(StraddlingCheckerboard) on success, or Err(String) if validation fails.
§Errors
Returns an error if:
keyis emptychrscontains fewer than 2 characters
Trait Implementations§
Source§impl Block for StraddlingCheckerboard
impl Block for StraddlingCheckerboard
Source§fn block_size(&self) -> usize
fn block_size(&self) -> usize
Returns the block size, which equals the key length.
§Returns
The length of the cipher key in bytes.
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts plaintext into digit ciphertext.
Each plaintext letter is replaced with its corresponding digit code (either 1 or 2 digits). Numeric digits in the plaintext are escaped by surrounding them with the ‘/’ marker code and duplicating the digit.
§Arguments
dst- Output buffer for the encrypted digit stringsrc- Input plaintext bytes to encrypt
§Returns
The number of bytes written to dst.
§Examples
Encrypting “ATTACK” with key “ARABESQUE” and long digits “89” produces “07708081”.
Source§fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts digit ciphertext back into plaintext.
Processes the digit stream, recognizing both single-digit and two-digit codes. Handles escaped numeric digits by detecting the ‘/’ marker pattern.
§Arguments
dst- Output buffer for the decrypted plaintextsrc- Input ciphertext digit string to decrypt
§Returns
The number of bytes written to dst.
§Examples
Decrypting “07708081” with key “ARABESQUE” and long digits “89” produces “ATTACK”.