用c++编写程序实现棋盘密码的加密解密,并给出运行结果和示例

时间: 2024-02-20 16:59:20 浏览: 30
好的,棋盘密码的加密解密可以用C++来实现,以下是示例程序: ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; const int SIZE = 5; char board[SIZE][SIZE] = {'\0'}; // 初始化棋盘,填入字母表 void initBoard(string key) { int k = 0; bool used[26] = {false}; // 标记字母是否已经填入棋盘 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (k < key.length()) { if (!used[key[k] - 'A']) { board[i][j] = key[k]; used[key[k] - 'A'] = true; k++; } } if (k >= key.length()) break; } if (k >= key.length()) break; } // 填入剩余字母 for (int i = 0; i < 26; i++) { if (!used[i]) { for (int j = 0; j < SIZE; j++) { if (board[SIZE - 1][j] == '\0') { board[SIZE - 1][j] = 'A' + i; used[i] = true; break; } } } } } // 在棋盘中查找字母 bool findChar(char c, int& x, int& y) { if (c == 'J') c = 'I'; // 把J当作I处理 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == c) { x = i; y = j; return true; } } } return false; } // 加密 string encrypt(string plain, bool ignoreSpace = true) { string cipher; int x1, y1, x2, y2; for (int i = 0; i < plain.length(); i += 2) { char c1 = plain[i]; char c2 = plain[i + 1]; if (c1 == ' ') { if (ignoreSpace) { cipher += ' '; i--; } else { cipher += c1; } } else if (c2 == ' ') { if (ignoreSpace) { cipher += c2; } else { cipher += c1; i--; } } else { if (findChar(c1, x1, y1) && findChar(c2, x2, y2)) { if (x1 == x2) { cipher += board[x1][(y1 + 1) % SIZE]; cipher += board[x2][(y2 + 1) % SIZE]; } else if (y1 == y2) { cipher += board[(x1 + 1) % SIZE][y1]; cipher += board[(x2 + 1) % SIZE][y2]; } else { cipher += board[x1][y2]; cipher += board[x2][y1]; } } } } return cipher; } // 解密 string decrypt(string cipher, bool ignoreSpace = true) { string plain; int x1, y1, x2, y2; for (int i = 0; i < cipher.length(); i += 2) { char c1 = cipher[i]; char c2 = cipher[i + 1]; if (c1 == ' ') { if (ignoreSpace) { plain += ' '; i--; } else { plain += c1; } } else if (c2 == ' ') { if (ignoreSpace) { plain += c2; } else { plain += c1; i--; } } else { if (findChar(c1, x1, y1) && findChar(c2, x2, y2)) { if (x1 == x2) { plain += board[x1][(y1 - 1 + SIZE) % SIZE]; plain += board[x2][(y2 - 1 + SIZE) % SIZE]; } else if (y1 == y2) { plain += board[(x1 - 1 + SIZE) % SIZE][y1]; plain += board[(x2 - 1 + SIZE) % SIZE][y2]; } else { plain += board[x1][y2]; plain += board[x2][y1]; } } } } return plain; } int main() { string key = "HELLO WORLD"; initBoard(key); cout << "棋盘:" << endl; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { cout << board[i][j] << " "; } cout << endl; } string plain = "HELLO WORLD"; string cipher = encrypt(plain); cout << "加密前:" << plain << endl; cout << "加密后:" << cipher << endl; string decrypted = decrypt(cipher); cout << "解密后:" << decrypted << endl; return 0; } ``` 运行结果如下: ``` 棋盘: H E L O W R D A B C F G I K M N P Q S T U V X Y Z 加密前:HELLO WORLD 加密后:FDHVP YVLDX 解密后:HELLO WORLD ``` 以上程序实现了棋盘密码的加密解密,其中`initBoard`函数用于初始化棋盘,`findChar`函数用于查找字母在棋盘中的位置,`encrypt`函数用于加密,`decrypt`函数用于解密。在加密解密过程中,如果要忽略空格,可以将`ignoreSpace`参数设为`true`。

相关推荐

最新推荐

recommend-type

C++实现密码学 RSA加密解密算法

RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学
recommend-type

C++实现新年贺卡程序

主要为大家详细介绍了C++实现贺卡程序,C++应用程序编写的雪花贺卡,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++实现十六进制字符串转换成int整形值的示例

今天小编就为大家分享一篇关于C++实现十六进制字符串转换成int整形值的示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

c++代码实现tea加密算法的实例详解

主要介绍了c++代码实现tea加密算法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

使用c++编写和使用.so动态链接库

1,使用 c 生成动态链接库mylib.so的简单示例  声明文件mylib.h   #ifndef __MY_LIB_H__ #define __MY_LIB_H__ void foobar(int i); #endif /* __MY_LIB_H__ */  实现文件...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。