Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[split #103] rewrite MacOS code #105

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions docs/topics/localfiles_encrypt_decrypt.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ On MacOS, decryption is quite simpler. Saves are encrypted with
### **Python**

```py
KEY = ( # python will automatically concatenate two parts
b"\x69\x70\x75\x39\x54\x55\x76\x35\x34\x79\x76\x5d\x69\x73\x46\x4d"
b"\x68\x35\x40\x3b\x74\x2e\x35\x77\x33\x34\x45\x32\x52\x79\x40\x7b"
)
KEY = b'ipu9TUv54yv]isFMh5@;t.5w34E2Ry@{'
```

<!-- tabs:end -->
Expand All @@ -78,19 +75,24 @@ Here is how actual decryption would be implemented:
### **Python**

```py
from Crypto.Cipher import AES

# using https://pypi.org/project/cryptography/
from cryptography.hazmat.primitives import ciphers, padding

def remove_pad(data: bytes) -> bytes:
last = data[-1]
if last < 16:
data = data[:-last]
return data
mac_cipher = ciphers.Cipher(
ciphers.algorithms.AES256(b'ipu9TUv54yv]isFMh5@;t.5w34E2Ry@{'),
ciphers.modes.ECB()
)
mac_padding = padding.PKCS7(128)


def mac_decrypt(data: bytes) -> str:
cipher = AES.new(KEY, AES.MODE_ECB)
return remove_pad(cipher.decrypt(data)).decode()
decryptor = mac_cipher.decryptor()
unpadder = mac_padding.unpadder()
return (b'%s%s%s' % (
unpadder.update(decryptor.update(data)),
unpadder.update(decryptor.finalize()),
unpadder.finalize()
)).decode()
```

<!-- tabs:end -->
Expand Down Expand Up @@ -123,20 +125,24 @@ Like on Windows, encryption and decrypion are almost the same:
### **Python**

```py
from Crypto.Cipher import AES
# using https://pypi.org/project/cryptography/
from cryptography.hazmat.primitives import ciphers, padding


def add_pad(data: bytes) -> bytes:
len_r = len(data) % 16
if len_r:
to_add = 16 - len_r
data += to_add.to_bytes(1, "little") * to_add
return data
mac_cipher = ciphers.Cipher(
ciphers.algorithms.AES256(b'ipu9TUv54yv]isFMh5@;t.5w34E2Ry@{'),
ciphers.modes.ECB()
)
mac_padding = padding.PKCS7(128)


def mac_encrypt(data: str) -> bytes:
cipher = AES.new(KEY, AES.MODE_ECB)
return cipher.encrypt(add_pad(data.encode()))
def mac_encrypt(xmlstring: str) -> bytes:
encryptor = mac_cipher.encryptor()
padder = mac_padding.padder()
return b'%s%s%s' % (
encryptor.update(padder.update(xmlstring.encode())),
encryptor.update(padder.finalize()),
encryptor.finalize()
)
```

<!-- tabs:end -->