欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > C++实现Atbash密码

C++实现Atbash密码

2025/4/26 17:35:07 来源:https://blog.csdn.net/qq_56536309/article/details/147515279  浏览:    关键词:C++实现Atbash密码
详细说明 埃特巴什密码是一种替换密码,在该密码中字母表中的字母是反向对应的。例如,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):确保解密结果与原始文本一致。

  • 输出

    • 打印原始文本、预期文本、加密文本和解密文本。

代码执行流程

  1. 测试函数调用

    • main 函数中调用 test 函数。

  2. 加密测试

    • "Hello World" 作为输入,调用 atbash_cipher 进行加密。

  3. 解密测试

    • 将加密后的字符串再次作为输入,调用 atbash_cipher 进行解密。

  4. 验证结果

    • 使用断言验证加密和解密的正确性。

  5. 输出结果

    • 打印测试结果。

示例

假设输入文本为 "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"

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词