SHARE
TWEET

CSV Decrypter

a guest Jun 13th, 2017 465 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import sys
  3. import glob
  4. import os
  5. import lzma
  6.  
  7. #
  8. # decode_file
  9. #
  10. def decode_file(path):
  11.     basename = os.path.splitext(path)[0]
  12.     decodedname = basename + ".decoded.csv"
  13.  
  14.     print("process:", path, "->", decodedname)
  15.  
  16.     with open(path, 'rb') as f:
  17.         data = f.read()
  18.  
  19.     tempdata = bytearray()
  20.  
  21.     for i in range(0, 8):
  22.         tempdata.append(data[i])
  23.  
  24.     for i in range(0, 4):
  25.         tempdata.append(0)
  26.  
  27.     for i in range(8, len(data)):
  28.         tempdata.append(data[i])
  29.  
  30.     try:
  31.         with open(decodedname, 'wb') as f:
  32.             unpack_data = lzma.decompress(tempdata)
  33.             f.write(unpack_data)
  34.     except:
  35.         print("invalid input:", path)
  36.  
  37. #
  38. # decode_file
  39. #
  40. def len_2_bytes(datalen, max_len=4):
  41.     data = []
  42.     while datalen > 0:
  43.         item = datalen % 256
  44.         datalen = int(datalen / 256)
  45.         data.append(item)
  46.     while len(data) < max_len:
  47.         data.append(0)
  48.     return data
  49.  
  50. def encode_file(path, max_len=4):
  51.     basename = os.path.splitext(path)[0]
  52.     encodedname = basename + ".encoded.csv"
  53.  
  54.     print("process:", path, "->", encodedname)
  55.  
  56.     with open(path, 'rb') as f:
  57.         data = f.read()
  58.  
  59.     filters = [
  60.         {
  61.             "id": lzma.FILTER_LZMA1,
  62.             "dict_size": 256 * 1024,
  63.             "lc": 3,
  64.             "lp": 0,
  65.             "pb": 2,
  66.             "mode": lzma.MODE_NORMAL
  67.         },
  68.     ]
  69.  
  70.     pack_data = lzma.compress(data, format=lzma.FORMAT_ALONE, filters=filters)
  71.  
  72.     lzmadata = bytearray()
  73.  
  74.     for i in range(0, 5):
  75.         lzmadata.append(pack_data[i])
  76.  
  77.     data_size = len_2_bytes(len(data), max_len)
  78.     for size in data_size:
  79.         lzmadata.append(size)
  80.  
  81.     for i in range(13, len(pack_data)):
  82.         lzmadata.append(pack_data[i])
  83.  
  84.     #for i in range(0, len(lzmadata)):
  85.     #    print("i: ", i, " val: ", lzmadata[i])
  86.  
  87.     with open(encodedname, 'wb') as f:
  88.         f.write(lzmadata)
  89.  
  90. #
  91. # restore_file
  92. #
  93. def restore_file(path):
  94.     with open(path, 'rb') as f:
  95.         data = f.read()
  96.  
  97.     tempdata = bytearray()
  98.  
  99.     for i in range(0, 8):
  100.         tempdata.append(data[i])
  101.  
  102.     for i in range(0, 4):
  103.         tempdata.append(0)
  104.  
  105.     for i in range(8, len(data)):
  106.         tempdata.append(data[i])
  107.  
  108.     with open('real.lzma', 'wb') as f:
  109.         f.write(tempdata)
  110. def decode_dir(path):
  111.     for filename in glob.iglob(path + '/**/*.csv', recursive=True):
  112.         if "decoded.csv" in filename:
  113.             continue
  114.         decode_file(filename)
  115.  
  116. decode_dir(sys.argv[1])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top