Atbash Cipher
1. Readme
Atbash Cipher
创建 Atbash 密码的实现,这是在中东创建的古老加密系统.
Atbash 密码是一种简单的替换密码,它依赖于转置字母表中的所有字母,使得生成的字母表向后。
- 第一个字母替换为最后一个字母,
- 第二个字母替换为倒数第二个字母,
- 依此类推.
拉丁字母的 Atbash 密码如下:
明文: abcdefghijklmnopqrstuvwxyz
加密: zyxwvutsrqponmlkjihgfedcba
它是一个非常弱的加密方式,因为它只有一个加密可能性,且为一个简单的单字母替换密码。但是,这并不是现在’加密游戏-练习时间’的问题.
密文以固定长度的组写出,传统的组大小为 5 个字母,并且不包括标点符号。这是为了使单词边界,更难猜测。
例子
test
,编码为gvhg
gvhg
,解码为test
gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt
解码为thequickbrownfoxjumpsoverthelazydog
资源
维基百科http://en.wikipedia.org/wiki/Atbash
2. 开始你的表演
/// "Encipher" with the Atbash cipher. pub fn encode(plain: &str) -> String { unimplemented!("Encoding of {:?} in Atbash cipher.", plain); } /// "Decipher" with the Atbash cipher. pub fn decode(cipher: &str) -> String { unimplemented!("Decoding of {:?} in Atbash cipher.", cipher); }
3. 测试代码查看
# #![allow(unused_variables)] #fn main() { #[test] fn test_encode_yes() { assert_eq!("bvh", encode("yes")); } #[test] //#[ignore] fn test_encode_no() { assert_eq!("ml", encode("no")); } #[test] //#[ignore] fn test_encode_omg() { assert_eq!("lnt", encode("OMG")); } #[test] //#[ignore] fn test_encode_spaces() { assert_eq!("lnt", encode("O M G")); } #[test] //#[ignore] fn test_encode_mindblowingly() { assert_eq!("nrmwy oldrm tob", encode("mindblowingly")); } #[test] //#[ignore] fn test_encode_numbers() { assert_eq!("gvhgr mt123 gvhgr mt", encode("Testing,1 2 3, testing.")); } #[test] //#[ignore] fn test_encode_deep_thought() { assert_eq!("gifgs rhurx grlm", encode("Truth is fiction.")); } #[test] //#[ignore] fn test_encode_all_the_letters() { assert_eq!( "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", encode("The quick brown fox jumps over the lazy dog.") ); } #[test] //#[ignore] fn test_encode_ignores_non_ascii() { assert_eq!("mlmzh xrrrt mlivw", encode("non ascii éignored")); } #[test] //#[ignore] fn test_decode_exercism() { assert_eq!("exercism", decode("vcvix rhn")); } #[test] //#[ignore] fn test_decode_a_sentence() { assert_eq!( "anobstacleisoftenasteppingstone", decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v") ); } #[test] //#[ignore] fn test_decode_numbers() { assert_eq!("testing123testing", decode("gvhgr mt123 gvhgr mt")); } #[test] //#[ignore] fn test_decode_all_the_letters() { assert_eq!( "thequickbrownfoxjumpsoverthelazydog", decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt") ); } #}
4. 答案
# #![allow(unused_variables)] #fn main() { fn ascii(ch: char) -> u8 { ch as u8 } fn get_transpose(ch: char) -> char { if ch.is_digit(10) { ch } else { (ascii('z') - ascii(ch) + ascii('a')) as char } } pub fn encode(plaintext: &str) -> String { plaintext .to_lowercase() .chars() .filter(|&ch| ch.is_ascii()) .filter(|&ch| ch.is_alphanumeric()) .map(|ch| get_transpose(ch)) .collect::<Vec<char>>() .chunks(5) .map(|slice| slice.iter().cloned().collect::<String>()) .collect::<Vec<String>>() .join(" ") } pub fn decode(ciphertext: &str) -> String { ciphertext .split::<char>(' ') .collect::<String>() .chars() .map(|ch| get_transpose(ch)) .collect::<String>() } #}