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]
impl Hill
pub fn from_phrase(
phrase: &str,
chunk_size: usize
) -> Result<Hill, &'static str>
[src]
pub fn from_phrase(
phrase: &str,
chunk_size: usize
) -> Result<Hill, &'static str>
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]
impl Cipher for Hill
type Key = Matrix<isize>
type Algorithm = Hill
fn new(key: Matrix<isize>) -> Result<Hill, &'static str>
[src]
fn new(key: Matrix<isize>) -> Result<Hill, &'static str>
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 thatd*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(); }
fn encrypt(&self, message: &str) -> Result<String, &'static str>
[src]
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])).unwrap(); //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]
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])).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()); }