71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
|
"""
|
||
|
settingcrypt - Encrypt/decrypt setting.txt.
|
||
|
Interactive Python version of the setting.txt encryption
|
||
|
code from Wiibrew.
|
||
|
|
||
|
Copyright (c) 2023 Emily Daemon
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to deal
|
||
|
in the Software without restriction, including without limitation the rights
|
||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in all
|
||
|
copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
SOFTWARE.
|
||
|
"""
|
||
|
import argparse
|
||
|
|
||
|
DEBUG = False
|
||
|
|
||
|
parser = argparse.ArgumentParser(
|
||
|
prog="settingcrypt",
|
||
|
description="Encrypt/decrypt setting.txt.")
|
||
|
parser.add_argument(
|
||
|
"input",
|
||
|
help="Input filename. Set to - for stdin",
|
||
|
type=argparse.FileType("rb"))
|
||
|
parser.add_argument(
|
||
|
"output",
|
||
|
help="Output filename. Set to - for stdout.",
|
||
|
type=argparse.FileType("wb"))
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
def hushprint(text):
|
||
|
"""Debug print function."""
|
||
|
if DEBUG:
|
||
|
print(text)
|
||
|
|
||
|
def scrypt(buf: bytes) -> bytes:
|
||
|
"""
|
||
|
Encrypt a bytes object. Returns a bytes object.
|
||
|
Python rewrite of the following:
|
||
|
https://wiibrew.org/wiki//title/00000001/00000002/data/setting.txt#Encryption
|
||
|
"""
|
||
|
buf_arr = bytearray(buf)
|
||
|
key = 0x73B5DBFA
|
||
|
buf_crypted = []
|
||
|
|
||
|
for byte in buf_arr:
|
||
|
byte ^= key & 0xff
|
||
|
key = (key << 1) | (key >> 31)
|
||
|
buf_crypted.append(byte)
|
||
|
|
||
|
return bytes(buf_crypted)
|
||
|
|
||
|
input_file = args.input.read()
|
||
|
hushprint(f"Encrypting {args.input.name}.")
|
||
|
decoded = scrypt(input_file)
|
||
|
hushprint(f"Success. Writing to {args.output.name}.")
|
||
|
args.output.write(decoded)
|
||
|
hushprint("Success.")
|