Trying to crack XOR key for project (python2.7)
Question: Hi im trying to to encrypt a code then try and find the key for the code but
not getting the best results with dont know what i can do here

    
    
    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
    


Answer: I suspect that you want a call to `all` rather than a regular `for` loop when
you're checking if your decryption is valid. That will test all the characters
and only print the decryption if they're all valid:

    
    
    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
    

You can make your test even clearer than I have above by using a string or set
membership test, rather than explicitly checking ordinal ranges (e.g. `if
all(c in string.printable for c in decrypt)` is only a little more broad than
your test).

