Created
May 16, 2025 13:00
-
-
Save snowyegret23/b16567b14bb39065bec7965a64c7c91e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import io | |
| import gzip | |
| import hashlib | |
| import msgpack | |
| from Crypto.Cipher import AES | |
| from Crypto.Util.Padding import pad, unpad | |
| import json | |
| import base64 | |
| ENCRYPT_PASSWORD = "cLRzSgBxoP47Yntz" | |
| def get_key(password): | |
| return hashlib.sha256(password.encode("utf-8")).digest() | |
| class BytesEncoder(json.JSONEncoder): | |
| def default(self, obj): | |
| if isinstance(obj, bytes): | |
| return {"__bytes__": base64.b64encode(obj).decode("ascii")} | |
| return super().default(obj) | |
| def bytes_decoder(obj): | |
| if "__bytes__" in obj: | |
| return base64.b64decode(obj["__bytes__"]) | |
| return obj | |
| def restore_obfuscate_string(obfuscate_bytes): | |
| if not isinstance(obfuscate_bytes, bytes): | |
| if isinstance(obfuscate_bytes, dict) and "__bytes__" in obfuscate_bytes: | |
| obfuscate_bytes = base64.b64decode(obfuscate_bytes["__bytes__"]) | |
| else: | |
| return obfuscate_bytes | |
| unobfuscated = bytes(~b & 0xFF for b in obfuscate_bytes) | |
| try: | |
| return unobfuscated.decode("utf-8") | |
| except UnicodeDecodeError: | |
| return {"__bytes__": base64.b64encode(obfuscate_bytes).decode("ascii")} | |
| def obfuscate_string(text): | |
| if not isinstance(text, str): | |
| return text | |
| encoded = text.encode("utf-8") | |
| obfuscated = bytes(~b & 0xFF for b in encoded) | |
| return obfuscated | |
| def process_localization_data(data, mode="decrypt"): | |
| if isinstance(data, bytes): | |
| if mode == "decrypt": | |
| return restore_obfuscate_string(data) | |
| return data | |
| elif isinstance(data, dict): | |
| result = {} | |
| for key, value in data.items(): | |
| if key == "__bytes__" and mode == "decrypt": | |
| return restore_obfuscate_string({"__bytes__": value}) | |
| else: | |
| result[key] = process_localization_data(value, mode) | |
| return result | |
| elif isinstance(data, list): | |
| return [process_localization_data(item, mode) for item in data] | |
| elif isinstance(data, str) and mode == "encrypt": | |
| return obfuscate_string(data) | |
| else: | |
| return data | |
| def decrypt_file(file_path): | |
| output_path = f"{file_path}.json" | |
| key = get_key(ENCRYPT_PASSWORD) | |
| with open(file_path, "rb") as f: | |
| iv = f.read(16) | |
| encrypted_data = f.read() | |
| try: | |
| cipher = AES.new(key, AES.MODE_CBC, iv) | |
| decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size) | |
| with io.BytesIO(decrypted_data) as compressed_io: | |
| with gzip.GzipFile(fileobj=compressed_io, mode="rb") as f: | |
| decompressed_data = f.read() | |
| msgpack_data = msgpack.unpackb(decompressed_data, raw=False) | |
| text_data = process_localization_data(msgpack_data, mode="decrypt") | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| json.dump(text_data, f, ensure_ascii=False, indent=2, cls=BytesEncoder) | |
| print(f"복호화 완료: {output_path}") | |
| return output_path | |
| except Exception as e: | |
| print(f"복호화 중 오류 발생: {e}") | |
| raise | |
| def encrypt_file(file_path, output_hash=None): | |
| if output_hash: | |
| output_path = output_hash | |
| else: | |
| output_path = f"{os.path.splitext(file_path)[0]}" | |
| key = get_key(ENCRYPT_PASSWORD) | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| processed_data = process_localization_data(data, mode="encrypt") | |
| packed_data = msgpack.packb(processed_data, use_bin_type=True) | |
| compressed_data = io.BytesIO() | |
| with gzip.GzipFile(fileobj=compressed_data, mode="wb") as f: | |
| f.write(packed_data) | |
| compressed_bytes = compressed_data.getvalue() | |
| iv = os.urandom(16) | |
| cipher = AES.new(key, AES.MODE_CBC, iv) | |
| padded_data = pad(compressed_bytes, AES.block_size) | |
| encrypted_data = cipher.encrypt(padded_data) | |
| with open(output_path, "wb") as f: | |
| f.write(iv + encrypted_data) | |
| print(f"암호화 완료: {output_path}") | |
| return output_path | |
| def find_file_path(base_file_name, culture_name): | |
| combined = f"{culture_name.lower()}.{base_file_name}.msgpack" | |
| hash_obj = hashlib.sha1(combined.encode("utf-8")) | |
| return hash_obj.hexdigest() | |
| if __name__ == "__main__": | |
| # decrypt_file("./localize/4d60762622c8739989661a9419f70fc78f9b7885") | |
| encrypt_file("./localize/4d60762622c8739989661a9419f70fc78f9b7885.json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment