pub struct IrregularTransposition { /* private fields */ }Implementations§
Source§impl IrregularTransposition
impl IrregularTransposition
Sourcepub fn new(key: &str) -> Result<Self>
pub fn new(key: &str) -> Result<Self>
Creates a new irregular transposition cipher.
The key is converted to a numeric representation where each character’s position in alphabetical order determines its rank. The positions of ranks 0 and 1 define the triangular areas used in the irregular transposition.
§Arguments
key- The key string used for transposition. Must not be empty.
§Returns
Returns Ok(IrregularTransposition) if the key is valid, or an error message
if the key is empty.
§Errors
Returns an error if the key is empty.
§Examples
use old_crypto_rs::IrregularTransposition;
let cipher = IrregularTransposition::new("SUBWAY").unwrap();Trait Implementations§
Source§impl Block for IrregularTransposition
impl Block for IrregularTransposition
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts the source data using irregular transposition.
The encryption process works in three phases:
-
Fill non-triangular areas: The plaintext is written into a grid row by row, filling only the cells that are NOT in the triangular areas (defined by ranks 0 and 1). This creates the “regular” part of the transposition.
-
Fill triangular areas: After the regular areas are filled, the remaining plaintext continues filling the triangular areas row by row. These are the cells at positions (r, c) where
c >= rank_pos[0] + rorc >= rank_pos[1] + r. -
Read column by column: The ciphertext is generated by reading the grid column by column in the order specified by
tkey_order(alphabetical order of the key). Only active cells (those that were filled) are read.
§Arguments
dst- The destination buffer where encrypted data will be writtensrc- The source plaintext data to encrypt
§Returns
Returns the number of bytes written to dst (equal to the length of src).
§Examples
use old_crypto_rs::{Block, IrregularTransposition};
let cipher = IrregularTransposition::new("SUBWAY").unwrap();
let plaintext = b"ATTACKATDAWN";
let mut ciphertext = vec![0u8; plaintext.len()];
let n = cipher.encrypt(&mut ciphertext, plaintext);
assert_eq!(n, plaintext.len());Source§fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts the source data using irregular transposition.
The decryption process reverses the encryption:
-
Determine active cells: First, we determine which cells in the grid are active by simulating the filling process (non-triangular areas first, then triangular areas). This tells us which cells contain actual ciphertext data.
-
Fill grid from columns: The ciphertext is read column by column in the order specified by
tkey_order, filling the grid’s active cells. This reverses the column-wise reading done during encryption. -
Read in two phases: The plaintext is recovered by reading the grid in two phases:
- First phase: Read row by row from non-triangular areas
- Second phase: Read row by row from triangular areas This reverses the two-phase filling done during encryption.
§Arguments
dst- The destination buffer where decrypted data will be writtensrc- The source ciphertext data to decrypt
§Returns
Returns the number of bytes written to dst (equal to the length of src).
§Examples
use old_crypto_rs::{Block, IrregularTransposition};
let cipher = IrregularTransposition::new("SUBWAY").unwrap();
let ciphertext = b"ATWATADCAKN";
let mut plaintext = vec![0u8; ciphertext.len()];
let n = cipher.decrypt(&mut plaintext, ciphertext);
assert_eq!(n, ciphertext.len());