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
impl Cipher for ColumnarTransposition
source§fn new(key: (String, Option<char>)) -> ColumnarTransposition
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 thekeystream
source§fn encrypt(&self, message: &str) -> Result<String, &'static str>
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>
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");