Transposition

Struct Transposition 

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

A columnar transposition cipher.

Implementations§

Source§

impl Transposition

Source

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

Creates a new regular columnar transposition cipher.

Trait Implementations§

Source§

impl Block for Transposition

Source§

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

Encrypts the source data using columnar transposition.

The encryption process works as follows:

  1. Write plaintext in rows: The plaintext is written into a grid row by row, with each row having a length equal to the key length.

  2. Read columns in key order: The ciphertext is generated by reading the grid column by column in the order determined by the alphabetical order of the key letters. For example, if the key is “ZEBRAS”, the columns are read in the order corresponding to “ABERSZ” (alphabetical order of key letters).

§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, Transposition};

let cipher = Transposition::new("ZEBRAS").unwrap();
let plaintext = b"WEAREDISCOVEREDFLEEATONCE";
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 columnar transposition.

The decryption process reverses the encryption:

  1. Determine column structure: Calculate how many complete rows exist and how many columns have an extra character (when the plaintext length doesn’t divide evenly by the key length).

  2. Read columns in key order: Read the ciphertext column by column in the order determined by the alphabetical order of the key letters. Each column is read according to its calculated length.

  3. Write back row by row: Place each character into its original position in the grid, effectively reconstructing the original row-by-row layout of the plaintext.

§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), or 0 if the key length is 0 or the source is empty.

§Examples
use old_crypto_rs::{Block, Transposition};

let cipher = Transposition::new("ZEBRAS").unwrap();
let ciphertext = b"EVLNACDTESEAROFODEECWIREE";
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 Transposition

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.