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 squarechrs- The character set used for bigram generation (e.g., “ADFGVX” or “012345”)alpha- The condensed alphabet used to populate the cipher squareenc_table- Fast encryption table from plaintext byte to bigram bytesdec_table- Fast decryption table from bigram bytes to plaintext byte
Implementations§
Source§impl SquareCipher
impl SquareCipher
Sourcepub fn new(key: &str, chrs: &str) -> Result<Self>
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 squarechrs- A non-empty character set for bigram generation (length should match square dimensions)
§Returns
Ok(SquareCipher)- Successfully created cipherErr(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
impl Block for SquareCipher
Source§fn block_size(&self) -> usize
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
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
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.