试图破解项目的异或密钥(python2.7)


嗨,我

试图加密代码,然后尝试找到代码的密钥,但没有得到最好的结果,不知道我能在这里做什么

from itertools import izip, cycle
import itertools
import binascii
a = 0
message = "Hello friend"
length = len(message)
key = "s"
c = 0
def xor_crypt_string(data, key):
    return "".join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
encrypt = xor_crypt_string(message, key)
while (c <= length):
    res = itertools.permutations('abcdefghijklmnopqrstuvwxyz', c) # 3 is the length of your result.
    c = c + 1
    for i in res: 
        keys = ''.join(i)
        decrypt = xor_crypt_string(encrypt, keys)
        for d in decrypt:
            if (ord(d) > 47 and ord(d) < 58) or (ord(d) == 32) or (ord(d) > 64 and ord(d) < 91) or (ord(d) > 96 and ord(d) <123):
                print decrypt
            else: 
                a = 0

我怀疑您在检查解密是否有效时希望调用all而不是常规for循环。这将测试所有字符,并且仅在它们都有效时才打印解密:

if all(x == 32 or 47 < x < 58 or 64 < x < 91 or 96 < x < 123
       for x in (ord(c) for c in decrypt)):
   print decrypt

你可以通过使用字符串或集合成员资格测试来使你的测试比我上面更清晰,而不是显式检查序号范围(例如 if all(c in string.printable for c in decrypt)只比你的测试宽泛一点)。

最新更新