鱼C论坛

 找回密码
 立即注册
查看: 390|回复: 1

[已解决]求思路

[复制链接]
发表于 2023-12-7 10:13:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问这一题的思路
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?)
最佳答案
2023-12-7 10:13:36
这道题要求我们解密一个加密的消息。解密的过程和加密相反,分为三步:

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

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

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

以下是一个完整的解密程序的示例代码:
  1. # 定义函数用于将一个整数拆分成两个字母
  2. def split_int(n):
  3.     letter1 = chr((n >> 8) & 0xFF)
  4.     letter2 = chr(n & 0xFF)
  5.     return letter1, letter2
  6. # 加密后的消息
  7. encrypted_message = [15643, 6913, 6916, 23040, 2377, 6985, 6408, 3657, 5638,
  8.                      3084, 2119, 15910, 23079, 13629, 23101, 10300, 10557,
  9.                      23073, 13092, 23369]
  10. # 解密消息
  11. decrypted_message = ""
  12. for num in encrypted_message:
  13.     # 异或运算解密
  14.     decrypted_num = num ^ 31337
  15.     # 拆分整数为两个字母
  16.     letter1, letter2 = split_int(decrypted_num)
  17.     # 添加到解密后的消息中
  18.     decrypted_message += letter1 + letter2
  19. # 打印结果
  20. print(decrypted_message)
复制代码

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

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-7 10:13:36 | 显示全部楼层    本楼为最佳答案   
这道题要求我们解密一个加密的消息。解密的过程和加密相反,分为三步:

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

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

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

以下是一个完整的解密程序的示例代码:
  1. # 定义函数用于将一个整数拆分成两个字母
  2. def split_int(n):
  3.     letter1 = chr((n >> 8) & 0xFF)
  4.     letter2 = chr(n & 0xFF)
  5.     return letter1, letter2
  6. # 加密后的消息
  7. encrypted_message = [15643, 6913, 6916, 23040, 2377, 6985, 6408, 3657, 5638,
  8.                      3084, 2119, 15910, 23079, 13629, 23101, 10300, 10557,
  9.                      23073, 13092, 23369]
  10. # 解密消息
  11. decrypted_message = ""
  12. for num in encrypted_message:
  13.     # 异或运算解密
  14.     decrypted_num = num ^ 31337
  15.     # 拆分整数为两个字母
  16.     letter1, letter2 = split_int(decrypted_num)
  17.     # 添加到解密后的消息中
  18.     decrypted_message += letter1 + letter2
  19. # 打印结果
  20. print(decrypted_message)
复制代码

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

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 03:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表