好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

凯撒密码解密源代码.docx

3页
  • 卖家[上传人]:小**
  • 文档编号:86345827
  • 上传时间:2019-03-18
  • 文档格式:DOCX
  • 文档大小:17.96KB
  • / 3 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 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 num[26]; //保存密文中字母出现次数 for (i = 0; i < 26; i++) { cout << "=================================" << endl; cout << "=================================" << endl; if (argc != 3) { cout << "此为KAISER解密用法:[文件名][密文路径][明文路径]" << endl; cout << "例如: decryption F:\ciper_2_1.txt F:\plain.txt" << endl; //判断输入参数是否正确 } if ((fp_ciper = fopen argv[1], "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':num[0] = num[0] + 1; break; case 'B':num[1] = num[1] + 1; break; case 'C':num[2] = num[2] + 1; break; case 'D':num[3] = num[3] + 1; break; case 'E':num[4] = num[4] + 1; break; case 'F':num[5] = num[5] + 1; break; case 'G':num[6] = num[6] + 1; break; case 'H':num[7] = num[7] + 1; break; case 'I':num[8] = num[8] + 1; break; case 'J':num[9] = num[9] + 1; break; case 'K':num[10] = num[10] + 1; break; case 'L':num[11] = num[11] + 1; break; case 'M':num[12] = num[12] + 1; break; case 'N':num[13] = num[13] + 1; break; case 'O':num[14] = num[14] + 1; break; case 'P':num[15] = num[15] + 1; break; case 'Q':num[16] = num[16] + 1; break; case 'R':num[17] = num[17] + 1; break; case 'S':num[18] = num[18] + 1; break; case 'T':num[19] = num[19] + 1; break; case 'U':num[20] = num[20] + 1; break; case 'V':num[21] = num[21] + 1; break; case 'W':num[22] = num[22] + 1; break; case 'X':num[23] = num[23] + 1; break; case 'Y':num[24] = num[24] + 1; break; case 'Z':num[25] = num[25] + 1; break; } fclose(fp_ciper); for (i = 0; i < 26; i++) { if (num[i] > temp) { i = j; //求出最大次数的下标 temp = num[i]; } } if (j < 5) key = (j + 1 + 26) - 5;//是按照字母表的第几位计算,而不是按下标, //故加1,5是指E在字母表中的位置 else { key = (j + 1) - 5; } if ((fp_ciper = fopen(argv[1], "r")) == NULL) { cout << "再次打开密文出错!解密失败" << endl; exit(0); } //再次打开密文,进行解密 if ((fp_plain = fopen(argv[2], "W")) == NULL) { cout << "打开或建立明文文件出错!解密失败" << endl; exit(0); } // 把明文存放在此文件中 while ((ch_ciper = fgetc(fp_ciper)) != EOF) { if (ch_ciper > '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; } }}。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.