Struct cipher_crypt::hill::Hill [] [src]

pub struct Hill { /* fields omitted */ }

A Hill cipher.

This struct is created by the new() method. See its documentation for more.

Methods

impl Hill
[src]

[src]

Initialise a Hill cipher given a phrase.

The position of each character within the alphabet is used to construct the matrix key of the cipher. The variable chunk_size defines how many chars (or chunks) of a message will be transposed during encryption/decryption.

Will return Err if one of the following conditions is detected:

  • The chunk_size is less than 2
  • The square of chunk_size is not equal to the phrase length
  • The phrase contains non-alphabetic symbols
  • Any of the Err conditions as stipulated by the new() fn

Example

use cipher_crypt::{Cipher, Hill};

let h = Hill::from_phrase("CEFJCBDRH", 3).unwrap();
h.encrypt("thing");

Trait Implementations

impl Cipher for Hill
[src]

[src]

Initialise a Hill cipher given a key matrix.

Will return Err if one of the following conditions is detected:

  • 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).unwrap();
}

[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])).unwrap();
    //Padding characters are added during the encryption process
    assert_eq!("PFOGOAUCIMpf", h.encrypt("ATTACKEAST").unwrap());
}

[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])).unwrap();

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

Auto Trait Implementations

impl Send for Hill

impl Sync for Hill