One of the oldest ciphers is the Scytale cipher. It was used by the Greeks within their military operations, and where they would create a cylinder of a given radius, and wrap a ribbon around it. Then, only the person with the right radius would be able to read the ciphered message. While it might not seem secure in our modern world, this secret message, at the time, was fairly secure (until the enemy knew the secret behind the cipher, of course). In this case, we will implement the code in the Rust programming language.
Scytale in Rust |
Method
It was used by the Greeks within their military operations, and where they would create a cylinder of a given radius, and wrap a ribbon around it. Only the person with the right radius could then read the ciphered message. While it might not seem secure in our modern world, the secret message, at the time, was fairly secure (until the enemy knew the secret behind the cipher, of course).
In the following, we define a cylinder with 10 characters - defined as the height:
If we take the phrase of "letthebattlebeginwhenthesunhasrisenintheeast", we get:
llnreeetiatbhssteeethgsn eiui bnnn awht thah tese
If we wrap, we get every 10th character:
LetthebattLebeginwheNthesunhasRisenintheEast
and so on. In this way, we could produce a cyliner with 10 segments, and then take our cipher message, and then wrap a ribbon around the cylinder, and write our message across it in a vertical way. Next we unwind the ribbon, and can pass onto our troops. They just take their cylinder, and wrap the cipher ribbon around it, and read the message.
Test vectors
A sample run is:
Message: letthebattlebeginwhenthesunhasrisenintheeast Height: 10 Scytale encrypt: llnreeetiatbhssteeethgsn eiui bnnn awht thah tese Scytale decrypt: lelinurie ebentnina tabwhhsts tteheaeht htgessne
Code
First we create the Rust project with:
cargo new scy
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, Scytale}; use std::env; fn main(){ let mut m = String::from("Attack at dawn"); let mut height = 3; let args: Vec= env::args().collect(); if args.len() >1 { m = args[1].clone();} if args.len()> 2 { height = args[2].clone().parse:: ().unwrap(); } let cy = Scytale::new(height); println!("\nMessage:\t\t{}",m); println!("\nHeight:\t\t\t{}\n",height); println!("Scytale encrypt:\t{}",cy.encrypt(m.as_str()).unwrap()); println!("Scytale decrypt:\t{}",cy.decrypt(m.as_str()).unwrap()); }
Finally we simply build with:
cargo build
For scytale encryption of the message "Attack at dawn" and a height of 3, we get:
>> cargo run "Hello" 3 Message: Attack at dawn Height: 3 Scytale encrypt: Aa wtcadntkta Scytale decrypt: Akdt atawatnc