打开压缩包得到两个文件夹和两个文件,其中 encrypt.py 中是加密函数的代码, flag.enc 中是被加密 的秘钥, original 中是被打乱的小说片段, encrypt 中是加密并打乱后的小说片段。 分析代码可知,加密方法使用的是维吉尼亚密码,所以每次加密只会加密字符串中的字母,所以我们就可 以通过剩下没加密的字符来匹配明文和密文。 将明文和密文匹配之后,我们可以通过已知明文攻击快速的得到维吉尼亚密码的秘钥。 使用得到的秘钥解密 flag.enc 文件即可得到秘钥。
def decrypt(data, key):
assert len(data) <= len(key)
result = ""
for i in range(len(data)):
if data[i].isupper():
result += chr((ord(data[i]) - ord('A') - key[i] + 26) % 26 + ord('A'))
elif data[i].islower():
result += chr((ord(data[i]) - ord('a') - key[i] + 26) % 26 + ord('a'))
else:
result += data[i]
return result
m = 'Xiwoihp, Gaeasrvd jsf Sliuwe kkqpd ryix hh gkbh Omxnn ezxbi lvaqxxt nrflh, gpv pt yxx iw nls qioeds li qin bds vnef rts ghe ljez fu iywy vbm bwbe. Eke jvytzjn njps uag fh iujt qxehqqp rvseepu djdmg cep nmooryqzyav uu j gcr, nhag cybl kzvx aqsvvdvujy vm lhk Rmooeqfn eaww pthlowyav btng cpy yhpsnyael ov tae xbbf ufzfpbszk, oshfucs ql pma jol po lcp ysywc. Aa jgz heu vcdzf cmqy oufk pmag umt kcss ljml Fniejxqf'
c = 'However, Benjamin and Clover could only be with Boxer after working hours, and it was in the middle of the day when the van came to take him away. The animals were all at work weeding turnips under the supervision of a pig, when they were astonished to see Benjamin come galloping from the direction of the farm buildings, braying at the top of his voice. It was the first time that they had ever seen Benjamin'
k = []
for i in range(len(m)):
t = ord(m[i]) - ord(c[i])
k.append(t)
c = "xaawr{B0_d0l_cs0m_'Pp0mn-odn1vpabt_deqzcq'?}"
t = decrypt(c, k)
print(t)
|