[][src]Trait cipher_crypt::Cipher

pub trait Cipher {
    type Key;
    type Algorithm;
    fn new(key: Self::Key) -> Self::Algorithm;
fn encrypt(&self, message: &str) -> Result<String, &'static str>;
fn decrypt(&self, message: &str) -> Result<String, &'static str>; }

Associated Types

type Key

type Algorithm

Loading content...

Required methods

fn new(key: Self::Key) -> Self::Algorithm

Initialise a cipher given a specific key.

fn encrypt(&self, message: &str) -> Result<String, &'static str>

Encrypt a message using a cipher's algorithm.

fn decrypt(&self, message: &str) -> Result<String, &'static str>

Decrypt a message using a cipher's algorithm.

Loading content...

Implementors

impl Cipher for ADFGVX[src]

type Key = (String, String, Option<char>)

type Algorithm = ADFGVX

fn new(key: (String, String, Option<char>)) -> ADFGVX[src]

Initialise a ADFGVX cipher.

The key tuple maps to the following (String, String, Option<char>) = (polybius_key, columnar_key, null_char). Where ...

  • The polybius_key is used to init a polybius cipher. See it's documentation for more information.
  • The columnar_key is used to init a columnar transposition cipher. See it's documentation for more information.
  • The null_char is an optional character that will be used to pad uneven messages during the columnar transposition stage. See the columnar_transposition documentation for more information.

Panics

  • If a non-alphanumeric symbol is part of the key.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a ADFGVX cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, ADFGVX};

let polybius_key = String::from("ph0qg64mea1yl2nofdxkr3cvs5zw7bj9uti8");
let columnar_key = String::from("GERMAN");
let null_char = None;

let a = ADFGVX::new((
    polybius_key,
    columnar_key,
    null_char
));

let cipher_text = concat!(
    "gfxffgxgDFAXDAVGDgxvadaaxxXFDDFGGGFdfaxdavgdVDAGFAXVVxfdd",
    "fgggfVVVAGFFAvvvagffaGXVADAAXXvdagfaxvvGFXFFGXG"
);

assert_eq!(
    cipher_text,
    a.encrypt("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
        .unwrap()
);

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a ADFGVX cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, ADFGVX};

let polybius_key = String::from("ph0qg64mea1yl2nofdxkr3cvs5zw7bj9uti8");
let columnar_key = String::from("GERMAN");
let null_char = None;

let a = ADFGVX::new((
    polybius_key,
    columnar_key,
    null_char
));

let cipher_text = concat!(
    "gfxffgxgDFAXDAVGD gxvadaaxxXFDDFGGGFdfaxdav",
    "gdVDAGFAXVVxfddfgggfVVVAGFFA vvvagffaGXVADAAXX vdagfaxvvGFXFFGXG "
);

assert_eq!(
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
     a.decrypt(cipher_text).unwrap()
);

impl Cipher for Affine[src]

type Key = (usize, usize)

type Algorithm = Affine

fn new(key: (usize, usize)) -> Affine[src]

Initialise an Affine cipher given the key (a, b).

Panics

  • a or b are not in the inclusive range 1 - 26.
  • a has a factor in common with 26.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using an Affine cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Affine};

let a = Affine::new((3, 7));
assert_eq!("Hmmhnl hm qhvu!", a.encrypt("Attack at dawn!").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using an Affine cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Affine};

let a = Affine::new((3, 7));
assert_eq!("Attack at dawn!", a.decrypt("Hmmhnl hm qhvu!").unwrap());

impl Cipher for Autokey[src]

type Key = String

type Algorithm = Autokey

fn new(key: String) -> Autokey[src]

Initialise an Autokey cipher given a specific key.

Panics

  • The key contains non-alphabetic symbols.
  • The key is empty.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using an Autokey cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Autokey};

let a = Autokey::new(String::from("fort"));
assert_eq!("Fhktcd 🗡 mhg otzx aade", a.encrypt("Attack 🗡 the east wall").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using an Autokey cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Autokey};

let a = Autokey::new(String::from("fort"));;
assert_eq!("Attack 🗡 the east wall", a.decrypt("Fhktcd 🗡 mhg otzx aade").unwrap());

impl Cipher for Baconian[src]

type Key = (bool, Option<String>)

type Algorithm = Baconian

fn new(key: (bool, Option<String>)) -> Baconian[src]

Initialise a Baconian cipher

The key tuple maps to the following: (bool, Option<str>) = (use_distinct_alphabet, decoy_text). Where ...

  • The encoding will be use_distinct_alphabet for all alphabetical characters, or classical where I, J, U and V are mapped to the same value pairs
  • An optional decoy message that will will be used to hide the message - default is boilerplate "Lorem ipsum" text.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using the Baconian cipher

  • The message to be encrypted can only be ~18% of the decoy_text as each character of message is encoded by 5 encoding characters AAAAA, AAAAB, etc.
  • The italicised ciphertext is then hidden in a decoy text, where, for each 'B' in the ciphertext, the character is italicised in the decoy_text.

Examples

Basic usage:

use cipher_crypt::{Cipher, Baconian};

let b = Baconian::new((false, None));;
let message = "Hello";
let cipher_text = "Lo𝘳𝘦𝘮 ip𝘴um d𝘰l𝘰𝘳 s𝘪t 𝘢𝘮e𝘵, 𝘤𝘰n";

assert_eq!(cipher_text, b.encrypt(message).unwrap());

fn decrypt(&self, message: &str) -> Result<String, &'static str>[src]

Decrypt a message that was encrypted with the Baconian cipher

Examples

Basic usage:

use cipher_crypt::{Cipher, Baconian};

let b = Baconian::new((false, None));;
let cipher_text = "Lo𝘳𝘦𝘮 ip𝘴um d𝘰l𝘰𝘳 s𝘪t 𝘢𝘮e𝘵, 𝘯𝘦 t";

assert_eq!("HELLO", b.decrypt(cipher_text).unwrap());

impl Cipher for Caesar[src]

type Key = usize

type Algorithm = Caesar

fn new(shift: usize) -> Caesar[src]

Initialise a Caesar cipher given a specific shift value.

Panics

  • shift is not in the inclusive range 1 - 26.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Caesar cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Caesar};

let c = Caesar::new(3);
assert_eq!("Dwwdfn dw gdzq!", c.encrypt("Attack at dawn!").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Caesar cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Caesar};

let c = Caesar::new(3);
assert_eq!("Attack at dawn!", c.decrypt("Dwwdfn dw gdzq!").unwrap());

impl Cipher for ColumnarTransposition[src]

type Key = (String, Option<char>)

type Algorithm = ColumnarTransposition

fn new(key: (String, Option<char>)) -> ColumnarTransposition[src]

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 the keystream

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

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());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

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");

impl Cipher for FractionatedMorse[src]

type Key = String

type Algorithm = FractionatedMorse

fn new(key: String) -> FractionatedMorse[src]

Initialise a Fractionated Morse cipher given a specific key.

Panics

  • The key is empty.
  • The key contains a non-alphabetic symbol.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Fractionated Morse cipher.

Morse code supports the characters a-z, A-Z, 0-9 and the special characters @ ( ) . , : ' " ! ? - ; =. This function will return Err if the message contains any symbols that do not meet this criteria. As morse code does not preserve case, all messages will be transposed to uppercase automatically.

Examples

Basic usage:

use cipher_crypt::{Cipher, FractionatedMorse};

let fm = FractionatedMorse::new(String::from("key"));;
assert_eq!("CPSUJISWHSSPFANR", fm.encrypt("AttackAtDawn!").unwrap());

fn decrypt(&self, cipher_text: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Fractionated Morse cipher.

The Fractionated Morse alphabet only contains the normal alphabetic characters a-z, therefore this function will return Err if the message contains any non-alphabetic characters. Furthermore, it is possible that a purely alphabetic message will not produce valid Morse code, in which case an Err will again be returned.

Examples

Basic usage:

use cipher_crypt::{Cipher, FractionatedMorse};

let fm = FractionatedMorse::new(String::from("key"));;
assert_eq!("ATTACKATDAWN!", fm.decrypt("cpsujiswhsspfanr").unwrap());

impl Cipher for Hill[src]

type Key = Matrix<isize>

type Algorithm = Hill

fn new(key: Matrix<isize>) -> Hill[src]

Initialise a Hill cipher given a key matrix.

Panics

  • The key matrix is not a square
  • The key matrix is non-invertible
  • The inverse determinant of the key matrix cannot be calculated such that d*d^-1 == 1 mod 26

Examples

extern crate rulinalg;
extern crate cipher_crypt;

use rulinalg::matrix::Matrix;
use cipher_crypt::{Cipher, Hill};

fn main() {
    //Initialise a Hill cipher from a 3 x 3 matrix
    let m = Matrix::new(3, 3, vec![2, 4, 5, 9, 2, 1, 3, 17, 7]);
    let h = Hill::new(m);
}

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Hill cipher.

It is expected that this message contains alphabetic characters only. Due to the nature of the hill cipher it is very difficult to transpose whitespace or symbols during the encryption process. It will reject with Err if the message contains any non-alphabetic symbols.

You may also notice that your encrypted message is longer than the original. This will occur when the length of the message is not a multiple of the key matrix size. To accommodate for this potential difference, the algorithm will add n amount of padding characters so that encryption can occur. It is important that these extra padding characters are not removed till after the decryption process, otherwise the message will not be transposed properly.

Example

Basic usage:

extern crate rulinalg;
extern crate cipher_crypt;

use rulinalg::matrix::Matrix;
use cipher_crypt::{Cipher, Hill};

fn main() {
    let h = Hill::new(Matrix::new(3, 3, vec![2, 4, 5, 9, 2, 1, 3, 17, 7]));
    //Padding characters are added during the encryption process
    assert_eq!("PFOGOAUCIMpf", h.encrypt("ATTACKEAST").unwrap());
}

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Hill cipher.

It is expected that this message contains alphabetic characters only. Due to the nature of the hill cipher it is very difficult to transpose whitespace or symbols during the encryption process. It will reject with Err if the message contains any non-alphabetic symbols.

You may also notice that your encrypted message is longer than the original. This will occur when the length of the message is not a multiple of the key matrix size. See encrypt function for more information.

Examples

Example with stripping out padding:

extern crate rulinalg;
extern crate cipher_crypt;

use rulinalg::matrix::Matrix;
use cipher_crypt::{Cipher, Hill};

fn main() {
    let m = "ATTACKEAST";
    let h = Hill::new(Matrix::new(3, 3, vec![2, 4, 5, 9, 2, 1, 3, 17, 7]));;

    let c = h.encrypt(m).unwrap();
    let padding = c.len() - m.len();

    let p = h.decrypt(&c).unwrap();
    assert_eq!(m, p[0..(p.len() - padding)].to_string());
}

impl Cipher for Playfair[src]

type Key = (String, Option<char>)

type Algorithm = Playfair

fn new(key: (String, Option<char>)) -> Playfair[src]

Initialize a Playfair cipher.

The key tuple maps to the following (String, Option<char>) = (keystream, null_char). Where ...

  • The keystream is used to generate a playfair table.
  • The null_char is the character that is used to pad uneven messages during the encryption process. This value will default to 'X'.

Panics

  • The keystream must not be empty.
  • The keystream must not exceed the length of the playfair alphabet (25 characters).
  • The keystream must not contain non-alphabetic symbols or the letter 'J'.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

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"
);

fn decrypt(&self, message: &str) -> Result<String, &'static str>[src]

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"
);

impl Cipher for Polybius[src]

type Key = (String, [char; 6], [char; 6])

type Algorithm = Polybius

fn new(key: (String, [char; 6], [char; 6])) -> Polybius[src]

Initialise a Polybius square cipher.

In this implementation each part of the key is used to initialise a 6x6 polybius square. The key tuple maps to the following (String, [char; 6], [char; 6]) = (phase, column_ids, row_ids).

Where ...

  • phrase is used to generate an alphanumeric keyed alphabet. It can contain characters a-z 0-9.
  • column_ids are unique identifiers used for each column of the polybius square. Valid characters are alphabetic only (a-z).
  • row_ids are unique identifiers used for each row of the polybius square. Valid characters can be alphabetic only (a-z).

Panics

  • If a non-alphanumeric symbol is part of the key.
  • The key must have a length of 36.
  • The key must contain each character of the alphanumeric alphabet a-z, 0-9.
  • The column and row_ids must contain alphabetic characters only.
  • The column or row_ids contain repeated characters.

Example

Lets say the phrase was or0an3ge the column_ids were ['A','Z','C','D','E','F'] and the row_ids were ['A','B','G','D','E','F']. Then the polybius square would look like ...

__ A Z C D E F
A| o r 0 a n 3
B| g e b c d f
G| h i j k l m
D| p q s t u v
E| w x y z 1 2
F| 4 5 6 7 8 9

Basic usage:

use cipher_crypt::{Cipher, Polybius};

let p = Polybius::new((String::from("or0an3ge"), ['A','Z','C','D','E','F'],
    ['A','B','G','D','E','F']));;

assert_eq!("EEAC AAazadaebabzdc adaebe EF ADdadagebzdc!",
   p.encrypt("10 Oranges and 2 Apples!").unwrap());

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Polybius square cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Polybius};

let p = Polybius::new((String::from("p0lyb1us"), ['A','Z','C','D','E','F'],
    ['A','B','G','D','E','F']));;

assert_eq!("BCdfdfbcbdgf 🗡️ dfgcbf bfbcbzdf ezbcacac",
   p.encrypt("Attack 🗡️ the east wall").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Polybius square cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Polybius};

let p = Polybius::new((String::from("p0lyb1us"), ['A','Z','C','D','E','F'],
    ['A','B','G','D','E','F']));;

assert_eq!("Attack 🗡️ the east wall",
   p.decrypt("BCdfdfbcbdgf 🗡️ dfgcbf bfbcbzdf ezbcacac").unwrap());

impl Cipher for Porta[src]

type Key = String

type Algorithm = Porta

fn new(key: String) -> Porta[src]

Initialize a Porta cipher given a specific key.

Panics

  • The key is empty.
  • The key contains a non-alphabetic symbol.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Porta cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Porta};

let v = Porta::new("melon".into());
assert_eq!(v.encrypt("We ride at dawn!").unwrap(), "Dt mpwx pb xtdl!");

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Porta cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Porta};

let v = Porta::new(String::from("melon"));
assert_eq!(v.decrypt("Dt mpwx pb xtdl!").unwrap(), "We ride at dawn!");

impl Cipher for Railfence[src]

type Key = usize

type Algorithm = Railfence

fn new(key: usize) -> Railfence[src]

Initialise a Railfence cipher given a specific key (number of rails).

Panics

  • The key is 0.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Railfence cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Railfence};

let r = Railfence::new(3);
assert_eq!("Src s!ue-ertmsaepseeg", r.encrypt("Super-secret message!").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Railfence cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Railfence};

let r = Railfence::new(3);
assert_eq!("Super-secret message!", r.decrypt("Src s!ue-ertmsaepseeg").unwrap());

impl Cipher for Scytale[src]

type Key = usize

type Algorithm = Scytale

fn new(key: usize) -> Scytale[src]

Initialize a Scytale cipher with a specific cylinder height.

Panics

  • The key is 0.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Scytale cipher.

Whilst all characters (including utf8) can be encrypted during the transposition process, it is important to note that the space character is also treated as padding. As such, whitespace characters at the end of a message are not preserved during the decryption process.

Examples

Basic usage:

use cipher_crypt::{Cipher, Scytale};

let s = Scytale::new(6);
assert_eq!("Pegr lefoporaryr !", s.encrypt("Prepare for glory!").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Scytale cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Scytale};

let ct = Scytale::new(6);
assert_eq!("Prepare for glory!", ct.decrypt("Pegr lefoporaryr !").unwrap());

impl Cipher for Vigenere[src]

type Key = String

type Algorithm = Vigenere

fn new(key: String) -> Vigenere[src]

Initialise a Vigenère cipher given a specific key.

Panics

  • The key is empty.
  • The key contains a non-alphabetic symbol.

fn encrypt(&self, message: &str) -> Result<String, &'static str>[src]

Encrypt a message using a Vigenère cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Vigenere};

let v = Vigenere::new(String::from("giovan"));
assert_eq!("O vsqee mmh vnl izsyig!", v.encrypt("I never get any credit!").unwrap());

fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str>[src]

Decrypt a message using a Vigenère cipher.

Examples

Basic usage:

use cipher_crypt::{Cipher, Vigenere};

let v = Vigenere::new(String::from("giovan"));
assert_eq!("I never get any credit!", v.decrypt("O vsqee mmh vnl izsyig!").unwrap());
Loading content...