詳細說明 埃特巴什密碼是一種替換密碼,在該密碼中字母表中的字母是反向對應的。例如,A 會被替換為 Z,B 會被替換為 Y,依此類推。
#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <map> /// for std::map
#include <string> /// for std::string/** \namespace ciphers* \brief Algorithms for encryption and decryption*/
namespace ciphers {/** \namespace atbash* \brief Functions for the [Atbash* Cipher](https://en.wikipedia.org/wiki/Atbash) implementation*/namespace atbash {std::map<char, char> atbash_cipher_map = {{'a', 'z'}, {'b', 'y'}, {'c', 'x'}, {'d', 'w'}, {'e', 'v'}, {'f', 'u'},{'g', 't'}, {'h', 's'}, {'i', 'r'}, {'j', 'q'}, {'k', 'p'}, {'l', 'o'},{'m', 'n'}, {'n', 'm'}, {'o', 'l'}, {'p', 'k'}, {'q', 'j'}, {'r', 'i'},{'s', 'h'}, {'t', 'g'}, {'u', 'f'}, {'v', 'e'}, {'w', 'd'}, {'x', 'c'},{'y', 'b'}, {'z', 'a'}, {'A', 'Z'}, {'B', 'Y'}, {'C', 'X'}, {'D', 'W'},{'E', 'V'}, {'F', 'U'}, {'G', 'T'}, {'H', 'S'}, {'I', 'R'}, {'J', 'Q'},{'K', 'P'}, {'L', 'O'}, {'M', 'N'}, {'N', 'M'}, {'O', 'L'}, {'P', 'K'},{'Q', 'J'}, {'R', 'I'}, {'S', 'H'}, {'T', 'G'}, {'U', 'F'}, {'V', 'E'},{'W', 'D'}, {'X', 'C'}, {'Y', 'B'}, {'Z', 'A'}, {' ', ' '}};/*** @brief atbash cipher encryption and decryption* @param text Plaintext to be encrypted* @returns encoded or decoded string*/std::string atbash_cipher(const std::string& text) {std::string result;for (char letter : text) {result += atbash_cipher_map[letter];}return result;}} // namespace atbash
} // namespace ciphers/*** @brief Self-test implementations* @returns void*/
static void test() {// 1st teststd::string text = "Hello World";std::string expected = "Svool Dliow";std::string encrypted_text = ciphers::atbash::atbash_cipher(text);std::string decrypted_text = ciphers::atbash::atbash_cipher(encrypted_text);assert(expected == encrypted_text);assert(text == decrypted_text);std::cout << "Original text: " << text << std::endl;std::cout << ", Expected text: " << expected << std::endl;std::cout << ", Encrypted text: " << encrypted_text << std::endl;std::cout << ", Decrypted text: " << decrypted_text << std::endl;std::cout << "\nAll tests have successfully passed!\n";
}/*** @brief Main function* @returns 0 on exit*/
int main() {test(); // run self-test implementationsreturn 0;
}
代碼詳細逐句解釋
1.?命名空間定義
namespace ciphers {namespace atbash {
定義了嵌套的命名空間 ciphers::atbash
,用于組織和封裝Atbash加密算法。
2.?映射表定義
std::map<char, char> atbash_cipher_map = {{'a', 'z'}, {'b', 'y'}, {'c', 'x'}, {'d', 'w'}, {'e', 'v'}, {'f', 'u'},{'g', 't'}, {'h', 's'}, {'i', 'r'}, {'j', 'q'}, {'k', 'p'}, {'l', 'o'},{'m', 'n'}, {'n', 'm'}, {'o', 'l'}, {'p', 'k'}, {'q', 'j'}, {'r', 'i'},{'s', 'h'}, {'t', 'g'}, {'u', 'f'}, {'v', 'e'}, {'w', 'd'}, {'x', 'c'},{'y', 'b'}, {'z', 'a'}, {'A', 'Z'}, {'B', 'Y'}, {'C', 'X'}, {'D', 'W'},{'E', 'V'}, {'F', 'U'}, {'G', 'T'}, {'H', 'S'}, {'I', 'R'}, {'J', 'Q'},{'K', 'P'}, {'L', 'O'}, {'M', 'N'}, {'N', 'M'}, {'O', 'L'}, {'P', 'K'},{'Q', 'J'}, {'R', 'I'}, {'S', 'H'}, {'T', 'G'}, {'U', 'F'}, {'V', 'E'},{'W', 'D'}, {'X', 'C'}, {'Y', 'B'}, {'Z', 'A'}, {' ', ' '}
};
這個映射表定義了每個字母(包括大寫和小寫)及其對應的Atbash加密字母之間的映射關系。例如:
-
小寫字母
'a'
映射到'z'
。 -
大寫字母
'A'
映射到'Z'
。 -
空格
' '
映射到自身。
3.?加密/解密函數
std::string atbash_cipher(const std::string& text) {std::string result;for (char letter : text) {result += atbash_cipher_map[letter];}return result;
}
-
參數:
-
text
:輸入的待加密或解密的字符串。
-
-
功能:
-
遍歷輸入字符串的每個字符。
-
使用
atbash_cipher_map
查找每個字符的對應值,并將其追加到結果字符串result
中。
-
-
返回值:
-
返回加密或解密后的結果字符串。
-
4.?測試函數
static void test() {// 1st teststd::string text = "Hello World";std::string expected = "Svool Dliow";std::string encrypted_text = ciphers::atbash::atbash_cipher(text);std::string decrypted_text = ciphers::atbash::atbash_cipher(encrypted_text);assert(expected == encrypted_text);assert(text == decrypted_text);std::cout << "Original text: " << text << std::endl;std::cout << ", Expected text: " << expected << std::endl;std::cout << ", Encrypted text: " << encrypted_text << std::endl;std::cout << ", Decrypted text: " << decrypted_text << std::endl;std::cout << "\nAll tests have successfully passed!\n";
}
-
功能:
-
測試
atbash_cipher
函數的正確性。 -
使用示例文本
"Hello World"
進行加密和解密測試。 -
驗證加密結果是否與預期一致,并驗證解密后的文本是否與原始文本一致。
-
-
斷言:
-
assert(expected == encrypted_text)
:確保加密結果正確。 -
assert(text == decrypted_text)
:確保解密結果與原始文本一致。
-
-
輸出:
-
打印原始文本、預期文本、加密文本和解密文本。
-
代碼執行流程
-
測試函數調用:
-
在
main
函數中調用test
函數。
-
-
加密測試:
-
將
"Hello World"
作為輸入,調用atbash_cipher
進行加密。
-
-
解密測試:
-
將加密后的字符串再次作為輸入,調用
atbash_cipher
進行解密。
-
-
驗證結果:
-
使用斷言驗證加密和解密的正確性。
-
-
輸出結果:
-
打印測試結果。
-
示例
假設輸入文本為 "Hello World"
:
-
加密過程:
-
'H' → 'S'
-
'e' → 'v'
-
'l' → 'o'
-
'l' → 'o'
-
'o' → 'l'
-
' ' → ' '
-
'W' → 'D'
-
'o' → 'l'
-
'r' → 'i'
-
'l' → 'o'
-
'd' → 'w'
-
-
加密結果為
"Svool Dliow"
。 -
解密過程:
-
再次調用
atbash_cipher
對加密結果進行解密,應得到原始文本"Hello World"
。
-