/// Deciphers the given ciphertext (a str reference) using the Polybius Square
/// cipher and returns the plaintext as a `CipherResult`.
///
/// # Example
/// ```
/// use ciphers::{Cipher, PolybiusSquare};
///
/// let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ABCDE");
///
/// let ptext = ps.decipher("CEBCCDBCCBCEEBABBCBCBDEAEBEDBDCACACCCDEBABBCDDBDEAEBCABC");
/// assert_eq!(ptext.unwrap(), "DEFENDTHEEASTWALLOFTHECASTLE");
/// ```
Deciphers the given ciphertext (a str reference) using the Polybius Square
cipher and returns the plaintext as a `CipherResult`.
Deciphers the given ciphertext ( a str reference ) using the Polybius Square cipher and returns the plaintext as a ` CipherResult ` .
fn decipher(&self, ctext: &str) -> CipherResult {
        input::in_alphabet(ctext, &self.chars)?;
        if ctext.len() % 2 != 0 {
            return Err(CipherInputError::BadInput(String::from(
                "`ctext` must contain an even number of chars",
            )));
        }
        let key = self.key.as_bytes();
        let ctext = ctext.as_bytes();
        let mut ptext: Vec<u8> = Vec::with_capacity(ctext.len());
        for i in (0..ctext.len()).step_by(2) {
            let y = match self.chars.find(|c| c == ctext[i] as char) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let x = match self.chars.find(|c| c == ctext[i + 1] as char) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            ptext.push(key[y * self.chars.len() + x]);
        }
        Ok(String::from_utf8(ptext).unwrap())
    }
fn decipher ( & self , ctext : & str ) -> CipherResult { input :: in_alphabet ( ctext , & self . chars ) ? ; if ctext . len ( ) % 2 != 0 { return Err ( CipherInputError :: BadInput ( String :: from ( " " , ) ) ) ; } let key = self . key . as_bytes ( ) ; let ctext = ctext . as_bytes ( ) ; let mut ptext : Vec < u8 > = Vec :: with_capacity ( ctext . len ( ) ) ; for i in ( 0 .. ctext . len ( ) ) . step_by ( 2 ) { let y = match self . chars . find ( | c | c == ctext [ i ] as char ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; let x = match self . chars . find ( | c | c == ctext [ i + 1 ] as char ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; ptext . push ( key [ y * self . chars . len ( ) + x ] ) ; } Ok ( String :: from_utf8 ( ptext ) . unwrap ( ) ) }
Deciphers the given ciphertext (a str reference) using the Polybius Square
cipher and returns the plaintext as a `CipherResult`.
Deciphers the given ciphertext ( a str reference ) using the Polybius Square cipher and returns the plaintext as a ` CipherResult ` .
&self
ctext
&str
example
```
use ciphers::{Cipher, PolybiusSquare}.

let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ABCDE").


` ` ` use ciphers :: { Cipher PolybiusSquare } . let ps = PolybiusSquare :: new ( " PHQGIUMEAYLNOFDXKRCVSTZWB " " ABCDE " ) .