Struct 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§

Source§

impl Hill

Source

pub fn from_phrase(phrase: &str, chunk_size: usize) -> Hill

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§

Source§

impl Cipher for Hill

Source§

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

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

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

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

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

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

type Key = Matrix<isize>

Source§

type Algorithm = Hill

Auto Trait Implementations§

§

impl Freeze for Hill

§

impl RefUnwindSafe for Hill

§

impl Send for Hill

§

impl Sync for Hill

§

impl Unpin for Hill

§

impl UnwindSafe for Hill

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.