Guest User

Untitled

a guest
Nov 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. '''
  2. This script will XOR encrypt the files in specified directory.
  3. If you want to recover the encrypted files, simply run the script again with same XOR key.
  4. '''
  5.  
  6. import os
  7. import binascii
  8.  
  9. key = "This is the key"
  10. key = binascii.hexlify(key)
  11. key_hex = []
  12. path = "/foo/bar/"
  13. enc = ""
  14.  
  15. dir_list = os.listdir(path)
  16.  
  17. for file in dir_list:
  18. if not os.path.isdir(path + file):
  19. fout = open(path + file, 'rb')
  20. file_data = binascii.hexlify(fout.read())
  21. fout.close()
  22.  
  23. for a, b in zip(key[::2], key[1::2]): ## get 2chars each from hex and join
  24. key_hex.append(''.join(a + b))
  25.  
  26. cnt = 0
  27.  
  28. for a, b in zip(file_data[::2], file_data[1::2]): ## get 2chars from hex and join
  29. rawdata = ''.join(a + b)
  30. if cnt >= len(key_hex): ## when the key gets to end, reset the key to beginning
  31. cnt = 0
  32. enc += hex(int(rawdata, 16) ^ int(key_hex[cnt], 16)).replace('0x', '').zfill(2)
  33. cnt += 1
  34. ## I put zfill because I didn't want to lose the leading zero in hex
  35. ## eg) 0xd -> 0x0d (this is what I want)
  36. fin = open(path + file, 'wb')
  37. fin.write(binascii.unhexlify(enc))
  38. fin.close()
  39. enc = ""
  40. print("Encrypted!")
Add Comment
Please, Sign In to add comment