Matasano挑战Python Set 1-> 3,代码中的未知错误

时间:2016-08-03 00:52:55

标签: python cryptography

我正在尝试解决Matasano挑战集1-> 3即单字节XOR密码并将其反转。我想我理解要解决的概念,但我的解决方案是给出了一些错误:

 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:我是蟒蛇和OOP中的菜鸟所以欢迎任何建议

2 个答案:

答案 0 :(得分:0)

xor期待2个名单;你传给它一个列表和一个整数。

答案 1 :(得分:0)

您的算法错误,var promises = []; rows.forEach(function (key, value) { var p = new Promise(function (resolve, reject) { request("...", function (error, response, body) { if (error) { //handle error } else { resolve(body); } }); }); promises.push(p); }); //this function will not be called until all requests have returned Promise.all(promises).then(function (resp) { //these will be in order console.log(resp[0]); console.log(resp[1]); console.log(resp[2]); }); 与此类似,参考Matasano-Crypto-Challenges from github

scoreBoard