Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- 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!")
Add Comment
Please, Sign In to add comment