Struct cipher_crypt::hill::Hill

source ·
pub struct Hill { /* private fields */ }
Expand description

A Hill cipher.

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

Implementations§

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.

Panics
  • 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);
h.encrypt("thing");

Trait Implementations§

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

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

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

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.