[−][src]Struct cipher_crypt::hill::Hill
A Hill cipher.
This struct is created by the new()
method. See its documentation for more.
Methods
impl Hill
[src]
pub fn from_phrase(phrase: &str, chunk_size: usize) -> Hill
[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.
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
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 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); }
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()); }
Auto Trait Implementations
impl Send for Hill
impl Unpin for Hill
impl Sync for Hill
impl UnwindSafe for Hill
impl RefUnwindSafe for Hill
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,