Skip to content

Instantly share code, notes, and snippets.

@Tony3-sec
Created November 23, 2017 13:01
Show Gist options
  • Save Tony3-sec/a915f3da8d06cc3bf2ea30610238875f to your computer and use it in GitHub Desktop.
Save Tony3-sec/a915f3da8d06cc3bf2ea30610238875f to your computer and use it in GitHub Desktop.
'''
This script will XOR encrypt the files in specified directory.
If you want to recover the encrypted files, simply run the script again with same XOR key.
'''
import os
import binascii
key = "This is the key"
key = binascii.hexlify(key)
key_hex = []
path = "/foo/bar/"
enc = ""
dir_list = os.listdir(path)
for file in dir_list:
if not os.path.isdir(path + file):
fout = open(path + file, 'rb')
file_data = binascii.hexlify(fout.read())
fout.close()
for a, b in zip(key[::2], key[1::2]): ## get 2chars each from hex and join
key_hex.append(''.join(a + b))
cnt = 0
for a, b in zip(file_data[::2], file_data[1::2]): ## get 2chars from hex and join
rawdata = ''.join(a + b)
if cnt >= len(key_hex): ## when the key gets to end, reset the key to beginning
cnt = 0
enc += hex(int(rawdata, 16) ^ int(key_hex[cnt], 16)).replace('0x', '').zfill(2)
cnt += 1
## I put zfill because I didn't want to lose the leading zero in hex
## eg) 0xd -> 0x0d (this is what I want)
fin = open(path + file, 'wb')
fin.write(binascii.unhexlify(enc))
fin.close()
enc = ""
print("Encrypted!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment