deftest(msg_data,cip_data,o_k): tt=0 cip='' lk=len(o_k) for ii,m inenumerate(msg_data): c=(ord(m)+(o_k[ii%lk]^tt)+ii*ii)&0xff cip+=chr(c) tt=ord(m) returnall(x==c for x,c inzip(cip,cip_data))
defsolve(): withopen('msg001','r') as f: msg_data=f.read().strip() withopen('msg001.enc','r') as f: cip_data=f.read().strip() withopen('msg002.enc','r') as f: data=f.read().strip() k=get_true_key(msg_data,cip_data) print'the true key is : ',k o_k=[ord(c) for c in k] assert test(msg_data,cip_data,o_k) t=0 msg='' for ii,c inenumerate(data): p=(ord(c)-ii*ii-(o_k[ii%28]^t))&0xff t=p msg+=chr(p) withopen('msg002','w') as f: f.write(msg) return msg
if __name__=='__main__': # python solve.py print solve().strip()
解出加密所用密钥,破解密文:
1 2 3 4
$ python solve.py the true key is : VeryLongKeyYouWillNeverGuess The known-plaintext attack (KPA) is an attack model for cryptanalysis where the attacker has samples of both the plaintext (called a crib), and its encrypted version (ciphertext). These can be used to reveal further secret information such as secret keys and code books. The term "crib" originated at Bletchley Park, the British World War II decryption operation. The flag is CTF{6d5eba48508efb13dc87220879306619}