Rail Cipher/Caeser Shift in Rust
Rail Cipher/Caeser Shift in Rust |
Method
For this we add a number of "rails", and add the characters in sequence, such as for three rails (taken from Wikipedia):
'WE ARE DISCOVERED. FLEE AT ONCE', gives:
W . . . E . . . C . . . R . . . L . . . T . . . E . E . R . D . S . O . E . E . F . E . A . O . C . . . A . . . I . . . V . . . D . . . E . . . N . .
to give:
WECRL TEERD SOEEF EAOCA IVDEN
Test vectors
A sample run is:
Message: Do you want to know a secret Shift/rails: 4 Caeaser: Hs csy aerx xs orsa e wigvix Rail code: D twcouw oo er oat naseynk t Decrypt Caeaser: Zk ukq swjp pk gjks w oaynap Decrypt Rail code: Du ek owncoa nwr ty ae toost
Code
First we create the Rust project with:
cargo new rail
We then go into the rail folder, and add the following to the cargo.toml file:
[package] name = "rail" version = "0.1.0" authors = ["billatnapier"] [dependencies] cipher-crypt = "0.18.0"
Next we go into the src folder, and edit the main.rs file with:
extern crate cipher_crypt; use cipher_crypt::{Cipher, Caesar, Railfence}; use std::env; fn main(){ let mut m = String::from("Attack at dawn"); let mut shift = 3; let args: Vec= env::args().collect(); if args.len() >1 { m = args[1].clone();} if args.len()> 2 { shift = args[2].clone().parse:: ().unwrap(); } let c = Caesar::new(shift); let r = Railfence::new(shift); println!("\nMessage:\t\t{}",m); println!("\nShift/rails:\t\t{}\n",shift); println!("\nCaeaser:\t\t{}",c.encrypt(m.as_str()).unwrap()); println!("Rail code:\t\t{}",r.encrypt(m.as_str()).unwrap()); println!("\n\nDecrypt Caeaser:\t{}",c.decrypt(m.as_str()).unwrap()); println!("Decrypt Rail code:\t{}",r.decrypt(m.as_str()).unwrap()); }
Finally we simply build with:
cargo build
For 512-bit RSA encryption of the message "Hello" we get:
cargo run "Hello" 3