我正在 Coursera 上斯坦福大学的密码学课程(实际上落后了几个星期:/),我在第一次作业时遇到了一些麻烦。下面的代码可能,或者肯定是,过度杀伤......当我遇到这种方法的问题时,我缩小并尝试使用两个密文的一个异或结果进行婴儿床拖动,然后手动检查结果列表。
虽然我了解破坏多用途垫的原理,并且了解婴儿床拖动的工作原理,但我没有看到评估婴儿床拖动结果的实用方法......我花了大约一两个小时插入不同的词并用眼睛试图在结果中找到部分英语单词,但没有取得多大成功。我觉得应该有一种方法可以使用可能术语的简短字典列表来搜索并以某种方式自动检查结果。我该怎么做?
这是我最好的尝试:
# XOR two hex encoded strings
def XOR(a, b):
return " ".join("".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a ,b)]).split())
# Compares each of 10 ciphers with each other...results in 45 XORed results
def compareCiphers():
with open("ciphers.txt", "r") as f:
ciphers = f.readlines()
with open("XORedCiphers.txt", "w") as f:
n = 0
for c in ciphers:
print "Cipher: " + c + "-----------------------------------------\n"
for x in range(n, len(ciphers)):
if ciphers[x] != c:
print "\tXORed with cipher: " + ciphers[x]
result = XOR(ciphers[x], c)
print "\t\tResult:\t" + result + "\n\n"
f.write(result + "\n")
n += 1
# Drags each word of the dictionary across each XORed cipher text
def cribDrag():
with open("XORedCiphers.txt", "r") as f:
XORs = f.readlines()
with open("/Users/aweeeezy/bin/dictionaries/dictionary.txt", "r") as d:
for word in d:
for x in XORs:
for index in range(0, len(x)):
test = XOR(x[index:], word)
if test.isalpha():
""" Something should go here to check if 'test' is an English
word...but I can't think of a practical way to do this """
print "\t" + test
compareCiphers()
cribDrag()