pub struct Playfair { /* private fields */ }Expand description
A Playfair cipher.
This struct is created by the new() method. See its documentation for more.
Trait Implementations§
Source§impl Cipher for Playfair
impl Cipher for Playfair
Source§fn new(key: (String, Option<char>)) -> Playfair
fn new(key: (String, Option<char>)) -> Playfair
Initialize a Playfair cipher.
The key tuple maps to the following (String, Option<char>) = (keystream, null_char).
Where …
- The
keystreamis used to generate a playfair table. - The
null_charis the character that is used to pad uneven messages during the encryption process. This value will default to ‘X’.
§Panics
- The
keystreammust not be empty. - The
keystreammust not exceed the length of the playfair alphabet (25 characters). - The
keystreammust not contain non-alphabetic symbols or the letter ‘J’.
Source§fn encrypt(&self, message: &str) -> Result<String, &'static str>
fn encrypt(&self, message: &str) -> Result<String, &'static str>
Encrypt a message with the Playfair cipher.
§Warning
- The 5x5 key table requires any ‘J’ characters in the message to be substituted with ‘I’ characters (i.e. I = J).
- The resulting ciphertext will be fully uppercase with no whitespace.
§Errors
- Message contains a non-alphabetic character.
- Message contains the null character.
- Message contains whitespace.
§Examples
Basic Usage:
use cipher_crypt::{Cipher, Playfair};
let c = Playfair::new(("playfairexample".to_string(), None));
assert_eq!(
c.encrypt("Hidethegoldinthetreestump").unwrap(),
"BMODZBXDNABEKUDMUIXMKZZRYI"
);Source§fn decrypt(&self, message: &str) -> Result<String, &'static str>
fn decrypt(&self, message: &str) -> Result<String, &'static str>
Decrypt a message with the Playfair cipher.
§Warning
- The 5x5 key table requires any ‘J’ characters in the message to be substituted with ‘I’ characters (i.e. I = J).
- The resulting plaintext will be fully uppercase with no whitespace.
- The resulting plaintext may contain added null characters.
§Errors
- Message contains a non-alphabetic character.
- Message contains whitespace.
§Examples
Basic Usage:
use cipher_crypt::{Cipher, Playfair};
let c = Playfair::new(("playfairexample".to_string(), None));
assert_eq!(
c.decrypt("BMODZBXDNABEKUDMUIXMKZZRYI").unwrap(),
"HIDETHEGOLDINTHETREXSTUMPX"
);
type Key = (String, Option<char>)
type Algorithm = Playfair
Auto Trait Implementations§
impl Freeze for Playfair
impl RefUnwindSafe for Playfair
impl Send for Playfair
impl Sync for Playfair
impl Unpin for Playfair
impl UnwindSafe for Playfair
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more