凯撒密码解密源代码

上传人:小** 文档编号:86345827 上传时间:2019-03-18 格式:DOCX 页数:3 大小:17.96KB
返回 下载 相关 举报
凯撒密码解密源代码_第1页
第1页 / 共3页
凯撒密码解密源代码_第2页
第2页 / 共3页
凯撒密码解密源代码_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《凯撒密码解密源代码》由会员分享,可在线阅读,更多相关《凯撒密码解密源代码(3页珍藏版)》请在金锄头文库上搜索。

1、# include # include # include using namespace std;void main(int argc, char *argv)FILE *fp_ciper, *fp_plain; /密文与明文的文件指针char ch_ciper, ch_plain;int i, temp = 0; /i用来存储最多次数出现的下标 /temp用在求最多次数时用int key;int j;int num26; /保存密文中字母出现次数for (i = 0; i 26; i+)cout = endl;cout = endl;if (argc != 3)cout 此为KAISER解

2、密用法:文件名密文路径明文路径 endl;cout 例如: decryption F:ciper_2_1.txt F:plain.txt endl;/判断输入参数是否正确if (fp_ciper = fopen argv1, r) = NULL)cout 打开密文出错!解密失败 endl;exit(0);while (ch_ciper = fgetc(fp_ciper) != EOF)/ EOF=End Of File 定义: const int EOF = -1;switch (ch_ciper) /统计密文各字母出现的次数case A:num0 = num0 + 1;break;case

3、B:num1 = num1 + 1;break;case C:num2 = num2 + 1;break;case D:num3 = num3 + 1;break;case E:num4 = num4 + 1;break;case F:num5 = num5 + 1;break;case G:num6 = num6 + 1;break;case H:num7 = num7 + 1;break;case I:num8 = num8 + 1;break;case J:num9 = num9 + 1;break;case K:num10 = num10 + 1;break;case L:num11

4、= num11 + 1;break;case M:num12 = num12 + 1;break;case N:num13 = num13 + 1;break;case O:num14 = num14 + 1;break;case P:num15 = num15 + 1;break;case Q:num16 = num16 + 1;break;case R:num17 = num17 + 1;break;case S:num18 = num18 + 1;break;case T:num19 = num19 + 1;break;case U:num20 = num20 + 1;break;cas

5、e V:num21 = num21 + 1;break;case W:num22 = num22 + 1;break;case X:num23 = num23 + 1;break;case Y:num24 = num24 + 1;break;case Z:num25 = num25 + 1;break;fclose(fp_ciper);for (i = 0; i temp)i = j; /求出最大次数的下标temp = numi;if (j 5)key = (j + 1 + 26) - 5;/是按照字母表的第几位计算,而不是按下标, /故加1,5是指E在字母表中的位置elsekey = (j

6、+ 1) - 5;if (fp_ciper = fopen(argv1, r) = NULL)cout 再次打开密文出错!解密失败 endl;exit(0); /再次打开密文,进行解密if (fp_plain = fopen(argv2, W) = NULL) cout 打开或建立明文文件出错!解密失败 E)ch_plain = (ch_ciper - A - key) % 26) + A); / 解密else ch_plain = (ch_ciper - A - key + 26) % 26) + A);ch_plain = tolower(ch_plain); /把大写明文转换为小写fputc(ch_plain, fp_plain); /把明文卸载文件plain中fclose(fp_ciper);fclose(fp_plain);cout 解密成功!密钥 KEY= key 明文已保存在文件中,谢谢使用 endl;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号