Skip to main content

PlayFairKey

Struct PlayFairKey 

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

Struct represents a PlayFaire Cipher. It’s holding the key and the position of any character in the key.

Implementations§

Source§

impl PlayFairKey

Source

pub fn new_5_to_5(key: &str) -> Self

Constructs a new PlayFaire cipher based on a 5 to 5 square. J is replaced by I, no digits. Passkey can only contain A-I and K-Z.

§Example
use playfair_cipher::playfair::PlayFairKey as PlayFairKey;

let pfc = PlayFairKey::new_5_to_5("Secret");
Source

pub fn new_6_to_6(key: &str) -> Self

Constructs a new PlayFaire cipher based on a 6 to 6 square. A-Z and 0-9 are encryptable too. Passkey can contain A-Z and 0-9.

§Example
use playfair_cipher::playfair::PlayFairKey as PlayFairKey;

let pfc = PlayFairKey::new_6_to_6("Secret");

Trait Implementations§

Source§

impl Cipher for PlayFairKey

Source§

fn encrypt(&self, payload: &str) -> Result<String, CharNotInKeyError>

Encrypts a string. Note as the PlayFair cipher is only able to encrypt the characters A-I and L-Z any spaces and J are cleared off.

§Example 5 to 5

As described at https://en.wikipedia.org/wiki/Playfair_cipher

use playfair_cipher::{playfair::PlayFairKey, errors::CharNotInKeyError};
use playfair_cipher::cryptable::Cypher;

let pfc = PlayFairKey::new_5_to_5("playfair example");
match pfc.encrypt("hide the gold in the tree stump") {
  Ok(crypt) => {
    assert_eq!(crypt, "BMODZBXDNABEKUDMUIXMMOUVIF");
  }
  Err(e) => panic!("CharNotInKeyError {}", e),
};
§Example 6 to 6
use playfair_cipher::{playfair::PlayFairKey, errors::CharNotInKeyError};
use playfair_cipher::cryptable::Cypher;

let pfc = PlayFairKey::new_6_to_6("play 3645 fair 8760 example");
match pfc.encrypt("hide the gold in the tree stump at 5 o'clock.") {
  Ok(crypt) => {
    assert_eq!(crypt, "SXG0SJGQW5H5OUGX2MXMXQUN733Q0WDPNDHB");
  }
  Err(e) => panic!("CharNotInKeyError {}", e),
};
Source§

fn decrypt(&self, payload: &str) -> Result<String, CharNotInKeyError>

Decrypts a string.

§Example 5 to 5

As described at https://en.wikipedia.org/wiki/Playfair_cipher

use playfair_cipher::playfair::PlayFairKey as PlayFairKey;
use playfair_cipher::errors::CharNotInKeyError as CharNotInKeyError;
use playfair_cipher::cryptable::Cypher;

let pfc = PlayFairKey::new_5_to_5("playfair example");
match pfc.decrypt("BMODZBXDNABEKUDMUIXMMOUVIF") {
  Ok(crypt) => {
    assert_eq!(crypt, "HIDETHEGOLDINTHETREXESTUMP");
  }
  Err(e) => panic!("CharNotInKeyError {}", e),
};    
§Example 6 to 6
use playfair_cipher::{playfair::PlayFairKey, errors::CharNotInKeyError};
use playfair_cipher::cryptable::Cypher;

let pfc = PlayFairKey::new_6_to_6("play 3645 fair 8760 example");
match pfc.decrypt("SXG0SJGQW5H5OUGX2MXMXQUN733Q0WDPNDHB") {
  Ok(crypt) => {
    assert_eq!(crypt, "HIDETHEGOLDINTHETREXESTUMPAT5OCLOCKX");
  }
  Err(e) => panic!("CharNotInKeyError {}", e),
};
Source§

impl Debug for PlayFairKey

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.