2016-08-03 58 views
0

我試圖解決Matasano挑戰集1-> 3是單字節XOR密碼並將其逆轉。我想我明白了,解決這個概念,但我的解決方案是給一些錯誤:Matasano挑戰Python集1-> 3,代碼中的未知錯誤

Traceback (most recent call last): 
File "C:\crypto\python\breakXor.py", line 59, in <module> 
print(transforma(unhex)) 
File "C:\crypto\python\breakXor.py", line 51, in transforma 
if(scoreBoard(xored) > r): 
File "C:\crypto\python\breakXor.py", line 40, in scoreBoard 
c=chr(i).lower() 
TypeError: an integer is required 

我給這個的錯誤及我真的不明白爲什麼

import binascii 

freqs = { 
'a': 0.08167, 
'b': 0.01492, 
'c': 0.02782, 
'd': 0.04253, 
'e': 0.12702, 
'f': 0.02228, 
'g': 0.02015, 
'h': 0.06094, 
'i': 0.06966, 
'j': 0.00153, 
'k': 0.00772, 
'l': 0.04025, 
'm': 0.02406, 
'n': 0.06749, 
'o': 0.07507, 
'p': 0.01929, 
'q': 0.00095, 
'r': 0.05987, 
's': 0.06327, 
't': 0.09056, 
'u': 0.02758, 
'v': 0.00978, 
'w': 0.02361, 
'x': 0.00150, 
'y': 0.01974, 
'z': 0.00074, 
' ': 0.19281 
} 

def xor(xs, ys): 
    return "".join(chr(ord(x)^ord(y)) for x, y in zip(xs, ys)) 


def scoreBoard(inp): 
    r = 0 
    for i in inp: 
     c=chr(i).lower() 
     if c in freqs : 
      r += freqs[c] 
    return r 



def transforma(inp): 
    r = 0 
    for i in range(0,256): 
     xored = xor(inp,list(str(i))) 
     if(scoreBoard(xored) > r): 
      resultado = scoreBoard(xored) 
      indice = i 
    return (indice,resultado) 

if __name__ == "__main__": 
    inp = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' 
    unhex = binascii.unhexlify(inp) 
    print(transforma(unhex)) 

到的解釋解決方案的任何改進爲的錯誤及如何解決這些問題將是真棒

PS:我是在Python和OOP一個小白所以任何意見是值得歡迎的

+0

@TessellatingHeckler這就是做使用的列表(STR(I))進行修復。要更新錯誤 –

+0

如果'i'是一個數字,你可以int(i),所以嘗試chr(int(i)) –

回答

0

XOR期待2個清單;你傳遞一個列表和一個整數。

0

你的算法是錯誤的,scoreBoard此類似,裁判Matasano-Crypto-Challenges from github

def scoreBoard(text): 
    text = text.upper() 
    frequencies = {} 
    for letter in text: 
     if letter in frequencies: 
      frequencies[letter] += 1. 
     else: 
      frequencies[letter] = 1. 

    total = sum(frequencies.values()) 
    for letter in frequencies.keys(): 
     frequencies[letter] /= total 

    score = 0.0 
    for l in freqs.keys(): 
     if l not in frequencies: 
      frequencies[l] = 0.0 
     score += math.sqrt(frequencies[l] * freqs[l]) 

    return score 
+0

我不明白爲什麼我的算法會出錯,關心解釋? –