IrregularTransposition

Struct IrregularTransposition 

Source
pub struct IrregularTransposition { /* private fields */ }

Implementations§

Source§

impl IrregularTransposition

Source

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

Source§

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

Encrypts the source data using irregular transposition.

The encryption process works in three phases:

  1. 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.

  2. 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] + r or c >= rank_pos[1] + r.

  3. 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 written
  • src - 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

Decrypts the source data using irregular transposition.

The decryption process reverses the encryption:

  1. 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.

  2. 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.

  3. 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 written
  • src - 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());
Source§

fn block_size(&self) -> usize

Source§

impl Debug for IrregularTransposition

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.