Eat.随 发表于 2023-12-7 10:13:06

求思路

请问这一题的思路
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 10:13:36

这道题要求我们解密一个加密的消息。解密的过程和加密相反,分为三步:

1. 把加密后的消息拆分成两个字母一组,如果最后不够一组,则在末尾添加一个空格,使其拥有偶数个字符。

2. 从每一组中的第一个字母获取前8位,从第二个字母获取后8位,将它们组合成一个整数。

3. 使用异或运算符(^)将上一步中得到的整数与31337进行异或运算,得到解密后的整数。

以下是一个完整的解密程序的示例代码:

# 定义函数用于将一个整数拆分成两个字母
def split_int(n):
    letter1 = chr((n >> 8) & 0xFF)
    letter2 = chr(n & 0xFF)
    return letter1, letter2

# 加密后的消息
encrypted_message = [15643, 6913, 6916, 23040, 2377, 6985, 6408, 3657, 5638,
                     3084, 2119, 15910, 23079, 13629, 23101, 10300, 10557,
                     23073, 13092, 23369]

# 解密消息
decrypted_message = ""
for num in encrypted_message:
    # 异或运算解密
    decrypted_num = num ^ 31337
    # 拆分整数为两个字母
    letter1, letter2 = split_int(decrypted_num)
    # 添加到解密后的消息中
    decrypted_message += letter1 + letter2

# 打印结果
print(decrypted_message)

最后,我们将得到解密后的消息。注意,这只是一个示例代码,你需要根据具体情况进行调整。

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