The Vigenere cipher involves a key word.
Vigenere in Rust |
Test vectors
A sample run is:
Plain text: HelloHowAreYou Key: foxtrot Ciphertext: MSIEFVHBOOXPCN Plaintext: HELLOHOWAREYOU
How Vigenère works?
For example, if we use a key of KING:
Plain a b c d e f g h i j k l m n o p q r s t u v w x y z 6 g h i j k l m n o p q r s t u v w x y z a b c d e f 8 i j k l m n o p q r s t u v w x y z a b c d e f g h 10 k l m n o p q r s t u v w x y z a b c d e f g h i j 13 n o p q r s t u v w x y z a b c d e f g h i j k l m
For example phase becomes zpnyo, as p (read row 10 for K) gives Z, h (read row 8 for I) gives P, a (read row 13 for N) gives n), and so on.
The great advantage of this type of code is that the same plaintext character will be encrypted with different values, depending on the position of the keyword. For example, if the keyword is GREEN, ‘e’ can be encrypted as ‘K’ (for G), ‘V’ (for R), ‘I’ (for E) and ‘R’ (for N). To improve the security, the greater the size of the code word, the more the rows that can be included in the encryption process. Also, it is not possible to decipher the code by a frequency analysis, as letters will change their coding depending on the current position of the keyword. It is also safe from analysis of common two- and three-letter occurrences, if the keysize is relatively long. For example ‘ee’ could be encrypted with ‘KV’ (for GR), ‘VI’ (for RE), ‘II’ (for EE), ‘IR’ (for EN) and ‘RK’ (for NG).
Plain a b c d e f g h i j k l m n o p q r s t u v w x y z 1 b c d e f g h i j k l m n o p q r s t u v w x y z a 2 c d e f g h i j k l m n o p q r s t u v w x y z a b 3 d e f g h i j k l m n o p q r s t u v w x y z a b c 4 e f g h i j k l m n o p q r s t u v w x y z a b c d 5 f g h i j k l m n o p q r s t u v w x y z a b c d e 6 g h i j k l m n o p q r s t u v w x y z a b c d e f 7 h i j k l m n o p q r s t u v w x y z a b c d e f g 8 i j k l m n o p q r s t u v w x y z a b c d e f g h 9 j k l m n o p q r s t u v w x y z a b c d e f g h i 10 k l m n o p q r s t u v w x y z a b c d e f g h i j 11 l m n o p q r s t u v w x y z a b c d e f g h i j k 12 m n o p q r s t u v w x y z a b c d e f g h i j k l 13 n o p q r s t u v w x y z a b c d e f g h i j k l m 14 o p q r s t u v w x y z a b c d e f g h i j k l m n 15 p q r s t u v w x y z a b c d e f g h i j k l m n o 16 q r s t u v w x y z a b c d e f g h i j k l m n o p 17 r s t u v w x y z a b c d e f g h i j k l m n o p q 18 s t u v w x y z a b c d e f g h i j k l m n o p q r 19 t u v w x y z a b c d e f g h i j k l m n o p q r s 20 u v w x y z a b c d e f g h i j k l m n o p q r s t 21 v w x y z a b c d e f g h i j k l m n o p q r s t u 22 w x y z a b c d e f g h i j k l m n o p q r s t u v 23 x y z a b c d e f g h i j k l m n o p q r s t u v w 24 y z a b c d e f g h i j k l m n o p q r s t u v w x 25 z a b c d e f g h i j k l m n o p q r s t u v w x y
Code
First we create the with:
cargo new vig
We then go into the blake folder, and add the following to the cargo.toml file:
[package] name = "vig" version = "0.1.0" authors = ["billatnapier"] [dependencies] ciphers = "0.1.0"
Next we go into the src folder, and edit the main.rs file with:
use ciphers::{Cipher, Vigenere}; use std::env; fn main() { let mut msg= String::from("This is my message"); let mut key = String::from("mykey"); let args: Vec= env::args().collect(); if args.len() >2 { msg = args[1].clone(); key= args[2].clone(); } key = str::replace(&key," ",""); msg= str::replace(&msg, " ", ""); let vigenere = Vigenere::new(&key); let ctext = vigenere.encipher(&msg).unwrap(); let ptext = vigenere.decipher(&ctext).unwrap(); println!("Plain text:\t{}", msg); println!("Key:\t{}", key); println!("Ciphertext: {}", ctext); println!("Plaintext: {}", ptext); }
Finally we simply build with:
cargo build
and we then run with:
cargo run