Skip to content

Instantly share code, notes, and snippets.

@StarrFox
Created December 12, 2022 22:38
Show Gist options
  • Save StarrFox/109b2178ba1cf8afa0a9e3ed39759c21 to your computer and use it in GitHub Desktop.
Save StarrFox/109b2178ba1cf8afa0a9e3ed39759c21 to your computer and use it in GitHub Desktop.
def scramble_buffer(data: bytes, key: int):
buf = bytearray()
key_bytes = key.to_bytes(4, "little")
step = (((((key >> 3 & 0x8000 | key & 0x4000) >> 6 | key & 0x80) >> 1 | key & 0x20) >> 1 | key & 8) >> 3)
for b in data:
if ((step != 0) and (len(buf) != 0)) and (len(buf) % step == 0):
buf.append(buf[-1])
buf.append(key_bytes[len(buf) & 3] ^ b)
return buf
def unscramble_buffer(data: bytes, key: int):
buf = bytearray()
key_bytes = key.to_bytes(4, "little")
step = (((((key >> 3 & 0x8000 | key & 0x4000) >> 6 | key & 0x80) >> 1 | key & 0x20) >> 1 | key & 8) >> 3)
i = 0
while i < len(data):
if ((step != 0) and (len(buf) != 0)) and (i % step == 0):
i += 1
buf.append(key_bytes[i & 3] ^ data[i])
i += 1
return buf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment