Struct ColumnarTransposition

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

A Columnar Transposition cipher. This struct is created by the new() method. See its documentation for more.

Trait Implementations§

Source§

impl Cipher for ColumnarTransposition

Source§

fn new(key: (String, Option<char>)) -> ColumnarTransposition

Initialize a Columnar Transposition cipher.

Where…

  • Elements of keystream are used as the column identifiers.
  • The optional null_char is used to pad messages of uneven length.
  • The derived_key is used to initialise the column structures in the cipher.
§Panics
  • The keystream length is 0.
  • The keystream contains non-alphanumeric symbols.
  • The keystream contains duplicate characters.
  • The null_char is a character within the keystream
Source§

fn encrypt(&self, message: &str) -> Result<String, &'static str>

Encrypt a message with a Columnar Transposition cipher.

All characters (including utf8) can be encrypted during the transposition process. However, it is important to note that if padding characters are being used (null_char), the user must ensure that the message does not contain these padding characters, otherwise problems will occur during decryption. For this reason, the function will Err if it detects padding characters in the message to be encrypted.

§Examples

Basic usage:

use cipher_crypt::{Cipher, ColumnarTransposition};

let key_word = String::from("zebras");
let null_char = None;

let ct = ColumnarTransposition::new((key_word, null_char));;

assert_eq!("respce!uemeers-taSs g", ct.encrypt("Super-secret message!").unwrap());
Source§

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>

Decrypt a ciphertext with a Columnar Transposition cipher.

§Examples

Basic usage:

use cipher_crypt::{Cipher, ColumnarTransposition};

let key_word = String::from("zebras");
let null_char = None;

let ct = ColumnarTransposition::new((key_word, null_char));;
assert_eq!("Super-secret message!", ct.decrypt("respce!uemeers-taSs g").unwrap());

Using whitespace as null (special case): This will strip only trailing whitespace in message during decryption

use cipher_crypt::{Cipher, ColumnarTransposition};

let key_word = String::from("zebras");
let null_char = None;
let message = "we are discovered  "; // Only trailing spaces will be stripped

let ct = ColumnarTransposition::new((key_word, null_char));;

assert_eq!(ct.decrypt(&ct.encrypt(message).unwrap()).unwrap(),"we are discovered");
Source§

type Key = (String, Option<char>)

Source§

type Algorithm = ColumnarTransposition

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.