Visual_Studio

Terraria Mobile Save Encrypt/Decrypt

Jun 22nd, 2016
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. import os
  2. import sys
  3. import struct
  4.  
  5. from Crypto.Cipher import Blowfish  #pip install pycrypto
  6.  
  7. FILE_DIR = "files"
  8. OUT_DIR = "output"
  9.  
  10. BLOCK_SIZE = 8
  11.  
  12. for single in [FILE_DIR, OUT_DIR]:
  13.     if not os.path.isdir(single):
  14.         os.mkdir(single)
  15.  
  16. player_key = "0d710d1d63ff36fe9bafd4c3874c64dd"
  17. world_key = "9ae3efe6a4debdeb8290eabd5f832ad4"
  18.  
  19. def encrypt_player(dec_data, version = 24):
  20.     bf = Blowfish.new(player_key, Blowfish.MODE_ECB)
  21.     return struct.pack("<h", version) + bf.encrypt(dec_data)
  22.  
  23. def decrypt_player(enc_data):
  24.     bf = Blowfish.new(player_key, Blowfish.MODE_ECB)
  25.     #might do something with this
  26.     save_version = struct.unpack("<h", enc_data[:2])
  27.     return bf.decrypt(enc_data[:2])
  28.  
  29. def encrypt_world(dec_data, version = 24):
  30.     bf = Blowfish.new(world_key, Blowfish.MODE_ECB)
  31.     return struct.pack("<h", version) + bf.encrypt(dec_data)
  32.  
  33. def decrypt_world(enc_data):
  34.     bf = Blowfish.new(world_key, Blowfish.MODE_ECB)
  35.     #might do something with this
  36.     save_version = struct.unpack("<h", enc_data[:2])
  37.     return bf.decrypt(enc_data[2:])
  38.  
  39. argc = len(sys.argv)
  40.  
  41. if argc == 3:
  42.     mode = sys.argv[1]
  43.     filename = sys.argv[2]
  44.     load_path = os.path.join(FILE_DIR, filename)
  45.     if os.path.isfile(load_path):
  46.         save_file = open(load_path, "rb").read()
  47.         (base_name, ext) = os.path.basename(filename).split(".", 1)
  48.         if ext == "player":
  49.             if mode == "encrypt":
  50.                 encrypted = encrypt_player(save_file)
  51.                 save_path = os.path.join(OUT_DIR, "enc-" + base_name + "." + ext)
  52.                 open(save_path, "wb").write(encrypted)
  53.             elif mode == "decrypt":
  54.                 decrypted = decrypt_player(save_file)
  55.                 save_path = os.path.join(OUT_DIR, "dec-" + base_name + "." + ext)
  56.                 open(save_path, "wb").write(decrypted)
  57.             else:
  58.                 print "Invalid Mode"
  59.         elif ext == "world":
  60.             if mode == "encrypt":
  61.                 encrypted = encrypt_world(save_file)
  62.                 save_path = os.path.join(OUT_DIR, "enc-" + base_name + "." + ext)
  63.                 open(save_path, "wb").write(encrypted)
  64.             elif mode == "decrypt":
  65.                 decrypted = decrypt_world(save_file)
  66.                 save_path = os.path.join(OUT_DIR, "dec-" + base_name + "." + ext)
  67.                 open(save_path, "wb").write(decrypted)
  68.             else:
  69.                 print "Invalid Mode"
  70.         else:
  71.             print "Invalid File Type"
  72.     else:
  73.         print "File Doesn't Exist"
  74. else:
  75.     print "Usage: python terraria.py (encrypt/decrypt) (filename)"
Advertisement
Add Comment
Please, Sign In to add comment