Skip to content

Instantly share code, notes, and snippets.

@tamamu
Created February 23, 2016 11:28
Show Gist options
  • Save tamamu/538e24805a579551f02e to your computer and use it in GitHub Desktop.
Save tamamu/538e24805a579551f02e to your computer and use it in GitHub Desktop.
シーザー暗号 in Rust
static SHIFT:u8=3;
fn main() {
use std::env;
let args: Vec<_> = env::args().collect();
if args.len() > 2 {
if args[1] == "encrypt" {
println!("encrypt!");
for s in &args[2..] {
println!("{}", encrypt(&s, true));
}
}else if args[1] == "decrypt" {
println!("decrypt!");
for s in &args[2..] {
println!("{}", encrypt(&s, false));
}
}
}
}
fn encrypt(s: &str, is_encrypt: bool) -> String{
let mut result = Vec::new();
if is_encrypt {
for c in s.bytes(){
result.push(c+SHIFT);
}
}else{
for c in s.bytes(){
result.push(c-SHIFT);
}
}
return String::from_utf8(result).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment