2014-05-07 156 views
1

所以我報名參加了在Coursera斯坦福大學的加密類,我一直在與第一編程任務掙扎(我在幾個星期後。)麻煩實施嬰兒牀拖

我一直在玩弄此代碼的幾個星期的不同變化,以嘗試並刪除下面提到的問題...

起初,我以爲我得到了一個成功的嬰兒牀拖,但後來我意識到很多問題,沒有一個我已經能夠解決:

  • 「破譯」的結果是由於嬰兒牀被拖過兩個密碼文本的異或(「The」>「Th」>「T」而不是「The」>「he」>「e」),所以從該單詞的錯誤末尾縮短。
  • 嬰兒牀拖動的結果不是其他信息的文本,而是嬰兒牀本身...換句話說,無論我選擇哪個嬰兒牀,第一個X數量的指數總是返回嬰兒牀本身

下面的代碼:

def string_xor(a, b): 
    return "".join([chr(ord(x)^ord(y)) for (x, y) in zip(a ,b)]) 

def manual_crib_drag(word): 
    with open("two_ciphers.txt", "r") as f: 
     cipher1 = f.readline() 
     cipher2 = f.readline() 

    xor = string_xor(cipher1, cipher2) 
    word_hex = word.encode("hex") 

    for x in range(len(xor)): 
     try: 
      result = string_xor(xor[x:x+len(word_hex)], word_hex)\ 
              .strip().decode("hex") 
      print x, ":", result 
     except TypeError, e: print 

這裏運行manual_crib_drag時是結果(以下簡稱 「」):

0 : The 
1 : The 
2 : The 
3 : The 
4 : The 
5 : The 
6 : The 
7 : The 
8 : The 
9 : The 
10 : The 
11 : The 
12 : The 
13 : The 
14 : The 
15 : The 
16 : The 
17 : The 
18 : The 
19 : The 
20 : The 
21 : The 
22 : The 
23 : The 
24 : The 
25 : The 
26 : The 
27 : The 
28 : The 
29 : The 
30 : The 
31 : The 
32 : The 
33 : The 
34 : The 
35 : The 
36 : The 
37 : The 
38 : The 
39 : The 
40 : The 
41 : The 
43 : The? 
46 : Tcn$ 
53 : ??S 
71 : PN? 
80 : CT"# 
83 : ?Q? 
88 : `n$ 
94 : P'e< 
99 : U?? 
118 : b}l 
123 : Ǹd? 
132 : Tokf 
138 : X6% 
148 : YW0- 
155 : ??4d 
161 : ??? 
171 : ??! 
173 : ??d1 
177 : Uy?G 
200 : hm 
202 : de*t 
218 : Xn q 
238 : Ti0: 
249 : 4|5! 
253 : i?u 
258 : ;G+ 
263 : t?Qq 
269 :)? 
275 : t?? 
282 : Z 
285 : G?d 
313 : sLtU 
319 : !9u? 
320 : yo 
325 : ?kv0 
329 : Gx?? 
331 : Dﺹ? 

爲了完整起見,這裏是本例中使用兩個密文:

32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4 

32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce 

而異或這兩個密文的結果是:

PRX] 
TS\TW]SW\[\VTS\^W[ 
TVSPZWSQV 
[TZ[P\Q[PZRUS[TVTU[ZUQT[][SZRTWV 
h 

我不知道爲什麼它的分解成單獨的行,但我的猜測是它與密碼文本異或結果中的空格有關......將string_xor函數的返回封裝到另一個連接內部似乎可以做到這一點,但因爲它沒有似乎影響了嬰兒牀拖動的結果,我把它從所提供的代碼中排除:

" ".join("".join([chr(ord(x)^ord(y)) for (x, y) in zip(a ,b)]).split()) 

我很感激任何幫助!提前致謝。

回答

0

首先嚐試將其轉換爲ASCII,然後執行XOR

x = strxor(unhexlify(ciphertexts[0]),unhexlify(target)) 
    print "Ciphertext[0] xor Target\n" 
    crib = raw_input("Enter Crib:>") 
    print "Crib\n-->%s<--"%crib 
# Crib Drag 
    for i in range(len(x)): 
    z = x[i:] 
    print "\n[%d]"%i 
    print "%s"%strxor(z,crib)