Eat.随 发表于 2023-12-7 11:51:31

求助

请问这一题的代码(使用c语言)
10.2. Exercise
Dougie Dog has invented an "encryption" technique to keep secrets from the Ferocious Kittens. Fortunately, cats are extremely intelligent, and have cracked the simple code:
  1. Letters are grouped into pairs. Add a space to the end of the stringif necessary to give it an even number of characters (here "character" means char.
  2. Make an int from each pair by sticking the bits from the first letterin front of the bits from the second letter. You may assume that we are using 8-bit ASCII.
3. XOR the result with 31337.
Here's two examples of encryption:"cats" and "kittens".
1. Pairs:"ca ts"
"ki tt en s_"(_ represents a space)
2. into ints:25441 29811
27497 29812 25966 29472
3. XOR with 31337:6408 3610
4352 3613 7943 2377
Decryption is performed with the same steps, but in reverse order.
·The Ferocious Kittens have intercepted two secret messagesfrom Dougie Dog:
  15643 6913 6916 23040 2377 6985 6408 3657 56383084 2119 15910 23079 13629 23101 10300 1055723073 13092 23369
·Write a program that decrypts them.
  (hint: this will be a lot easier if you begin by writing a program toencrypt values -- you can check each step with"cats" and "kittens" to make sure you understand the process!)
  ·You must use a function to split a large integer into two separateletters. This function may not print anything to the screen. (hint: how can a function return two values?)

FishC_GPT 发表于 2023-12-7 11:52:13

你好!这是一个有趣的加密解密问题。首先,我们需要一个函数将一个整数拆分成两个字母。由于C语言中的函数只能返回一个值,我们可以通过指针参数来实现返回多个值的效果。

下面是解密程序的完整代码:

#include <stdio.h>

void splitInt(int num, char *letter1, char *letter2) {
    *letter1 = (num >> 8) & 0xFF;
    *letter2 = num & 0xFF;
}

int main() {
    int encrypted[] = {15643, 6913, 6916, 23040, 2377, 6985, 6408, 3657,
                     5638, 3084, 2119, 15910, 23079, 13629, 23101, 10300,
                     10557, 23073, 13092, 23369};
    int i, length = sizeof(encrypted) / sizeof(int);
    char letter1, letter2;

    for (i = 0; i < length; i++) {
      int decrypted;

      // XOR with 31337
      decrypted = encrypted ^ 31337;

      // Split into two letters
      splitInt(decrypted, &letter1, &letter2);

      // Print the decrypted letters
      printf("%c%c", letter1, letter2);
    }

    printf("\n");

    return 0;
}

在上面的代码中,我们定义了一个splitInt函数,这个函数将整数拆分成两个字母(字符)。然后,在主函数中,我们遍历加密的整数数组,进行解密操作。

我们首先将每个整数与31337进行XOR运算,得到解密后的结果。然后,我们调用splitInt函数将解密后的整数拆分成两个字母。最后,我们打印出解密后的字母。

你可以尝试运行这段代码并查看结果。希望能对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 求助