Training: Crypto - Caesar II (Crypto, Training)
题目描述
I guess you are done with Caesar I, aren’t you?
The big problem with caesar is that it does not allow digits or other characters.
I have fixed this, and now I can use any ascii character in the plaintext.
The keyspace has increased from 26 to 128 too. \o/
Enjoy!
我想你通过Caesar I,对吧?
caesar密码最大的问题是它不允许数字或其他字符。
我已经解决了这个问题,现在我可以在明文中使用任何ascii字符。
键空间也从26个增加到128个。\o/
享受吧!
密文
66 0E 0E 03 20 09 0E 01 4B 20 18 0E 14 20 12 0E
0B 15 04 03 20 0E 0D 04 20 0C 0E 11 04 20 02 07
00 0B 0B 04 0D 06 04 20 08 0D 20 18 0E 14 11 20
09 0E 14 11 0D 04 18 4D 20 73 07 08 12 20 0E 0D
04 20 16 00 12 20 05 00 08 11 0B 18 20 04 00 12
18 20 13 0E 20 02 11 00 02 0A 4D 20 76 00 12 0D
46 13 20 08 13 5E 20 50 51 57 20 0A 04 18 12 20
08 12 20 00 20 10 14 08 13 04 20 12 0C 00 0B 0B
20 0A 04 18 12 0F 00 02 04 4B 20 12 0E 20 08 13
20 12 07 0E 14 0B 03 0D 46 13 20 07 00 15 04 20
13 00 0A 04 0D 20 18 0E 14 20 13 0E 0E 20 0B 0E
0D 06 20 13 0E 20 03 04 02 11 18 0F 13 20 13 07
08 12 20 0C 04 12 12 00 06 04 4D 20 76 04 0B 0B
20 03 0E 0D 04 4B 20 18 0E 14 11 20 12 0E 0B 14
13 08 0E 0D 20 08 12 20 00 02 12 04 12 11 02 11
02 0F 12 0C 4D
解
这道题是很简单的,题目描述中,健空间扩大从26(a-z)个扩大到128(ascii表)个,我查阅了ascii表发现128个ASCII码中的可显示字符的范围在32(空格)-126(~)总共有95个,故题目应该利用了全部ASCII码进行凯撒变化,对密文解密后,输出可显示字符即可。
首先将16进制字符转换成10进制,然后进行128次凯撒移位,保留可显示ASCII字符的结果,舍弃不可显示的结果,解出密文。(题目中所给的密文中0x20(32)没有进行凯撒位移,及0x20其实就是空格符(ASCII码32),这是通过结果发现的,虽然对结果无太大影响但还是说明一下)
Python3代码
def str2dec(s):
sl = s.lower()
result = 0
#print(sl)
for c in sl:
#print(c)
if c <= '9':
result *= 16
result += ord(c) - ord('0')
elif 'a' <= c <= 'f':
result *= 16
result += ord(c) - ord('W')
return result
#16进制字符转10进制
"""
f = open('Caesar2.txt','r')
r = f.read()
"""
r = '''
66 0E 0E 03 20 09 0E 01 4B 20 18 0E 14 20 12 0E
0B 15 04 03 20 0E 0D 04 20 0C 0E 11 04 20 02 07
00 0B 0B 04 0D 06 04 20 08 0D 20 18 0E 14 11 20
09 0E 14 11 0D 04 18 4D 20 73 07 08 12 20 0E 0D
04 20 16 00 12 20 05 00 08 11 0B 18 20 04 00 12
18 20 13 0E 20 02 11 00 02 0A 4D 20 76 00 12 0D
46 13 20 08 13 5E 20 50 51 57 20 0A 04 18 12 20
08 12 20 00 20 10 14 08 13 04 20 12 0C 00 0B 0B
20 0A 04 18 12 0F 00 02 04 4B 20 12 0E 20 08 13
20 12 07 0E 14 0B 03 0D 46 13 20 07 00 15 04 20
13 00 0A 04 0D 20 18 0E 14 20 13 0E 0E 20 0B 0E
0D 06 20 13 0E 20 03 04 02 11 18 0F 13 20 13 07
08 12 20 0C 04 12 12 00 06 04 4D 20 76 04 0B 0B
20 03 0E 0D 04 4B 20 18 0E 14 11 20 12 0E 0B 14
13 08 0E 0D 20 08 12 20 00 02 12 04 12 11 02 11
02 0F 12 0C 4D
'''
s = [i for i in r.split()]
#print(len(s))
#print(s)
for i in range(len(s)):
s[i] = str2dec(s[i])
#print(s)
for i in range(128):
result = ''
temp = 0
for j in range(len(s)):
if s[j] == 32:
result += ' '
continue
temp = (s[j] + i)%128
result += chr(temp)
if all(' ' <= k <= '~' for k in result):#打印可显示ASCII字符的结果
print(result)
#Good job, you solved one more challenge in your journey. This one was fairly easy to crack. Wasn't it? 128 keys is a quite small keyspace, so it shouldn't have taken you too long to decrypt this message. Well done, your solution is acsesrcrcpsm.
如图所示