【问题标题】:Shift Cipher without knowing the key in c在不知道 c 中的密钥的情况下移位密码
【发布时间】:2021-12-20 10:31:43
【问题描述】:

我正在编写一个程序来解决一个随机代码。(代码不会有大写字母,任何形式的符号,如句号)代码使用移位密码而不知道密钥,它也可以加密代码,但我会稍后再处理。现在我尝试先输出所有 25 个结果。

但是,如果我输入“背靠背”之类的内容,它会在最后输出一个问号。

这是我的代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
  char code[400];
  char after[400];  // variable initialization
  int i, x, choice, z, temp, y;
  printf("                     Welcome\n");
  printf("Enter your code here:\n");
  scanf("%[^'\n']s", code);
  printf("Press 1 to Decrypt\nPress 2 to encrypt\nPress 3 to Exit\n");
  scanf("%d", &choice);

  // case 1 is decryption
  switch (choice) {
    case 1:
      for (x = 1; x <= 25; x++) {
        for (i = 0; (i <= 400 && code[i] != 0); i++)  // for loop
        {
          if (code[i] == 32) {  // 32 means space in ascii code
            after[i] = 32;
            continue;  // if there is a space it will continue
          }
          if (code[i] + x > 122) {  // 122 is z in ascii code
            after[i] = code[i] + x;  // if it is higher than 'z'
            after[i] = (after[i] - 122) + 96;  // it start counting form 'a'
            continue;
          } else {
            after[i] = code[i] + x;
            // if nothing is wrong,it starting adding the key
          }
        }
        printf("After decrypting:%s\n", after);
        printf("%s\n", code);
      }
      break;

    // case 2 is ecnrypt so its not important
    case 2:
      x = 5;
      for (i = 0; (i <= 400 && code[i] != 0); i++)  // for loop
      {
        if (code[i] == ' ') {
          continue;
        }
        if (code[i] + x > 122) {
          code[i] = code[i] + x;
          code[i] = (code[i] - 122) + 96;
          continue;
        } else {
          code[i] = code[i] + x;
        }
      }
      printf("After decrypting:%s", code);
      break;

    // not important as well
    case 3:
      printf("GOODBYE");
      break;

    // not important as well
    default:
      printf("Errors");
      return 0;
  }
}

【问题讨论】:

    标签: arrays c for-loop switch-statement


    【解决方案1】:

    要解决移位密码,您只需要一本字典和密文的前十个单词左右。尝试所有可能性并根据您的字典测试每个“解码”的明文单词。选择匹配率最高的密码。

    【讨论】:

    • 这似乎没有回答他们的问题:“但是,如果我输入“背靠背”之类的内容,它会在最后输出一个问号。”
    • 是否有可能找到出现频率最高的代码,因为它是英语中最流行的字母?我认为很难复制整个字典并与代码匹配。感谢您的建议:D
    • @SirJamesLongbottom 您需要一个相对较长的密文才能可靠地工作。考虑一下您的“背靠背”示例,它没有 e。
    • 我的错,我忘了说所有代码都有大约 400 字,背靠背只是一个测试用例尝试:(
    【解决方案2】:

    你忘了空终止after

    也许你应该这样做:

        for (i = 0; (i <= 400 && code[i] != 0); i++) {
          if (code[i] == 32) {
            after[i] = ...
          } else if (code[i] + x > 122) {
            after[i] = ...
          } else {
            after[i] = ...
          }
        }
        after[i] = '\0'; // <-- This line is missing.
        printf("After decrypting:%s\n", after);
        printf("%s\n", code);
    

    【讨论】:

    • 谢谢,现在可以使用了:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2013-08-19
    • 2020-01-02
    • 2023-03-24
    • 2022-09-24
    相关资源
    最近更新 更多