Polibius密码
1. /// Encode an ASCII string into its location in a Polybius square.
2. /// Only alphabetical characters are encoded.
3. pub fn encode_ascii(string: &str) -> String {
4. string
5. .chars()
6. .map(|c| match c {
7. 'a' | 'A' => "11",
8. 'b' | 'B' => "12",
9. 'c' | 'C' => "13",
10. 'd' | 'D' => "14",
11. 'e' | 'E' => "15",
12. 'f' | 'F' => "21",
13. 'g' | 'G' => "22",
14. 'h' | 'H' => "23",
15. 'i' | 'I' | 'j' | 'J' => "24",
16. 'k' | 'K' => "25",
17. 'l' | 'L' => "31",
18. 'm' | 'M' => "32",
19. 'n' | 'N' => "33",
20. 'o' | 'O' => "34",
21. 'p' | 'P' => "35",
22. 'q' | 'Q' => "41",
23. 'r' | 'R' => "42",
24. 's' | 'S' => "43",
25. 't' | 'T' => "44",
26. 'u' | 'U' => "45",
27. 'v' | 'V' => "51",
28. 'w' | 'W' => "52",
29. 'x' | 'X' => "53",
30. 'y' | 'Y' => "54",
31. 'z' | 'Z' => "55",
32. _ => "",
33. })
34. .collect()
35. }
37. /// Decode a string of ints into their corresponding
38. /// letters in a Polybius square.
39. ///
40. /// Any invalid characters, or whitespace will be ignored.
41. pub fn decode_ascii(string: &str) -> String {
42. string
43. .chars()
44. .filter(|c| !c.is_whitespace())
45. .collect::<String>()
46. .as_bytes()
47. .chunks(2)
48. .map(|s| match std::str::from_utf8(s) {
49. Ok(v) => v.parse::<i32>().unwrap_or(0),
50. Err(_) => 0,
51. })
52. .map(|i| match i {
53. 11 => 'A',
54. 12 => 'B',
55. 13 => 'C',
56. 14 => 'D',
57. 15 => 'E',
58. 21 => 'F',
59. 22 => 'G',
60. 23 => 'H',
61. 24 => 'I',
62. 25 => 'K',
63. 31 => 'L',
64. 32 => 'M',
65. 33 => 'N',
66. 34 => 'O',
67. 35 => 'P',
68. 41 => 'Q',
69. 42 => 'R',
70. 43 => 'S',
71. 44 => 'T',
72. 45 => 'U',
73. 51 => 'V',
74. 52 => 'W',
75. 53 => 'X',
76. 54 => 'Y',
77. 55 => 'Z',
78. _ => ' ',
79. })
80. .collect::<String>()
81. .replace(" ", "")
82. }
84. #[cfg(test)]
85. mod tests {
86. use super::{decode_ascii, encode_ascii};
88. #[test]
89. fn encode_empty() {
90. assert_eq!(encode_ascii(""), "");
91. }
93. #[test]
94. fn encode_valid_string() {
95. assert_eq!(encode_ascii("This is a test"), "4423244324431144154344");
96. }
98. #[test]
99. fn encode_emoji() {
100. assert_eq!(encode_ascii("🙂"), "");
101. }
103. #[test]
104. fn decode_empty() {
105. assert_eq!(decode_ascii(""), "");
106. }
108. #[test]
109. fn decode_valid_string() {
110. assert_eq!(
111. decode_ascii("44 23 24 43 24 43 11 44 15 43 44 "),
112. "THISISATEST"
113. );
114. }
116. #[test]
117. fn decode_emoji() {
118. assert_eq!(decode_ascii("🙂"), "");
119. }
121. #[test]
122. fn decode_string_with_whitespace() {
123. assert_eq!(
124. decode_ascii("44\n23\t\r24\r\n43 2443\n 11 \t 44\r \r15 \n43 44"),
125. "THISISATEST"
126. );
127. }
129. #[test]
130. fn decode_unknown_string() {
131. assert_eq!(decode_ascii("94 63 64 83 64 48 77 00 05 47 48 "), "");
132. }
134. #[test]
135. fn decode_odd_length() {
136. assert_eq!(decode_ascii("11 22 33 4"), "AGN");
137. }
139. #[test]
140. fn encode_and_decode() {
141. let string = "Do you ever wonder why we're here?";
142. let encode = encode_ascii(string);
143. assert_eq!(
144. "1434543445155115425234331415425223545215421523154215",
145. encode,
146. );
147. assert_eq!("DOYOUEVERWONDERWHYWEREHERE", decode_ascii(&encode));
148. }
149. }