pub struct Transposition { /* private fields */ }Expand description
A columnar transposition cipher.
Implementations§
Trait Implementations§
Source§impl Block for Transposition
impl Block for Transposition
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts the source data using columnar transposition.
The encryption process works as follows:
-
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.
-
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 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, 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
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts the source data using columnar transposition.
The decryption process reverses the encryption:
-
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).
-
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.
-
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 writtensrc- 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());