in ctf tutorial

Cracking Single Byte Repeated Xor

Update 3/29: If hex encoded string you can use this script credit(https://repl.it/@sfb/single-byte-xor-cipher):

encoded = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
import binascii
nums = binascii.unhexlify(encoded)
strings = (''.join(chr(num ^ key) for num in nums) for key in range(256))
print(max(strings, key=lambda s: s.count(' ')))

A common ctf problem is cracking an encrypted message that has been xor'ed by one byte. This will not be hard to crack because there are only 256 (2^8) different possible keys to try. Knowing this we can just try all 256 different key values and look through the output for the right deciphered text. Here is a python script that will do that for us:


def solution(input_data):
    result=""
    for i in range(0,256):
        result+="NEXT KEY"
        for ch in input_data:
            result+= chr(ord(ch) ^ i)
    return result

def main():
    input_data = open('encrypted', 'r').read()
    result_data=''
    result_data=solution(input_data)
    out_file = open("solution.txt", 'w')
    out_file.write(result_data)
    out_file.close()

main()

input_data = the contents of the file encrypted
result_data = the result of xoring every character of input_data by all 256 possible keys
After we have all of the result_data we write it to a file called 'solution.txt'. We can now parse through solution.txt for the right answer.