/// Deciphers the given ciphertext (a str reference) using the Playfair cipher
/// and returns the plaintext as a `CipherResult`.
///
/// # Example
/// ```
/// use ciphers::{Cipher, Playfair};
///
/// let playfair = Playfair::new("ZGPTFOIHMUWDRCNYKEQAXVSBL", 'X');
///
/// let ptext = playfair.decipher("RKPAWRPMYSELZCLFXUZFRSNQBPSA");
/// assert_eq!(ptext.unwrap(), "DEFENDTHEXASTWALLOFTHECASTLE");
/// ```
Deciphers the given ciphertext (a str reference) using the Playfair cipher
and returns the plaintext as a `CipherResult`.
Deciphers the given ciphertext ( a str reference ) using the Playfair cipher and returns the plaintext as a ` CipherResult ` .
fn decipher(&self, ctext: &str) -> CipherResult {
        input::in_alphabet(ctext, &self.key)?;
        if ctext.len() % 2 != 0 {
            return Err(CipherInputError::BadInput(String::from(
                "`ctext` must contain an even number of chars",
            )));
        }
        let ctext = ctext.as_bytes();
        let key = self.key.as_bytes();
        let mut ptext = Vec::with_capacity(ctext.len());
        for i in (0..ctext.len()).step_by(2) {
            let yx1 = match key.iter().position(|&c| c == ctext[i]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let yx2 = match key.iter().position(|&c| c == ctext[i + 1]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let (y1, x1) = (yx1 / 5, yx1 % 5);
            let (y2, x2) = (yx2 / 5, yx2 % 5);
            if y1 != y2 && x1 != x2 {
                // different rows and columns
                ptext.push(key[y1 * 5 + x2]);
                ptext.push(key[y2 * 5 + x1]);
            } else if y1 == y2 {
                // same row
                ptext.push(key[y1 * 5 + (x1 + 5 - 1) % 5]);
                ptext.push(key[y2 * 5 + (x2 + 5 - 1) % 5]);
            } else if x1 == x2 {
                // same column
                ptext.push(key[(y1 + 5 - 1) % 5 * 5 + x1]);
                ptext.push(key[(y2 + 5 - 1) % 5 * 5 + x2]);
            }
        }
        Ok(String::from_utf8(ptext).unwrap())
    }
fn decipher ( & self , ctext : & str ) -> CipherResult { input :: in_alphabet ( ctext , & self . key ) ? ; if ctext . len ( ) % 2 != 0 { return Err ( CipherInputError :: BadInput ( String :: from ( " " , ) ) ) ; } let ctext = ctext . as_bytes ( ) ; let key = self . key . as_bytes ( ) ; let mut ptext = Vec :: with_capacity ( ctext . len ( ) ) ; for i in ( 0 .. ctext . len ( ) ) . step_by ( 2 ) { let yx1 = match key . iter ( ) . position ( | & c | c == ctext [ i ] ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; let yx2 = match key . iter ( ) . position ( | & c | c == ctext [ i + 1 ] ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; let ( y1 , x1 ) = ( yx1 / 5 , yx1 % 5 ) ; let ( y2 , x2 ) = ( yx2 / 5 , yx2 % 5 ) ; if y1 != y2 && x1 != x2 { // different rows and columns ptext . push ( key [ y1 * 5 + x2 ] ) ; ptext . push ( key [ y2 * 5 + x1 ] ) ; } else if y1 == y2 { // same row ptext . push ( key [ y1 * 5 + ( x1 + 5 - 1 ) % 5 ] ) ; ptext . push ( key [ y2 * 5 + ( x2 + 5 - 1 ) % 5 ] ) ; } else if x1 == x2 { // same column ptext . push ( key [ ( y1 + 5 - 1 ) % 5 * 5 + x1 ] ) ; ptext . push ( key [ ( y2 + 5 - 1 ) % 5 * 5 + x2 ] ) ; } } Ok ( String :: from_utf8 ( ptext ) . unwrap ( ) ) }
Deciphers the given ciphertext (a str reference) using the Playfair cipher
and returns the plaintext as a `CipherResult`.
Deciphers the given ciphertext ( a str reference ) using the Playfair cipher and returns the plaintext as a ` CipherResult ` .
&self
ctext
&str
example
```
use ciphers::{Cipher, Playfair}.

let playfair = Playfair::new("ZGPTFOIHMUWDRCNYKEQAXVSBL", 'X').

let ptext = playfair.decipher("RKPAWRPMYSELZCLFXUZFRSNQBPSA");
assert_eq!(ptext.unwrap(), "DEFENDTHEXASTWALLOFTHECASTLE");
```
` ` ` use ciphers :: { Cipher Playfair } . let playfair = Playfair :: new ( " ZGPTFOIHMUWDRCNYKEQAXVSBL " ' X ' ) . let ptext = playfair . decipher ( " RKPAWRPMYSELZCLFXUZFRSNQBPSA " ) ; assert_eq! ( ptext . unwrap () " DEFENDTHEXASTWALLOFTHECASTLE " ) ; ` ` `