Created
February 23, 2016 11:28
-
-
Save tamamu/538e24805a579551f02e to your computer and use it in GitHub Desktop.
シーザー暗号 in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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