Toolbox for analyzing and editing pkg application files for psp,ps3, ps4 and ps5, includes the most useful functions you might need.
| 1 | from Crypto.Cipher import AES |
| 2 | from Crypto.Util.Padding import pad, unpad |
| 3 | from Crypto.Util.number import bytes_to_long |
| 4 | from Crypto.Random import get_random_bytes |
| 5 | |
| 6 | # Constants |
| 7 | AES_KEY_LEN_128 = 16 |
| 8 | |
| 9 | # AES context |
| 10 | class AES_ctx: |
| 11 | def __init__(self): |
| 12 | self.key = None |
| 13 | self.iv = None |
| 14 | |
| 15 | def set_key(self, key): |
| 16 | self.key = key |
| 17 | |
| 18 | def set_iv(self, iv): |
| 19 | self.iv = iv |
| 20 | |
| 21 | def encrypt(self, data): |
| 22 | cipher = AES.new(self.key, AES.MODE_CBC, self.iv) |
| 23 | return cipher.encrypt(pad(data, AES.block_size)) |
| 24 | |
| 25 | def decrypt(self, data): |
| 26 | cipher = AES.new(self.key, AES.MODE_CBC, self.iv) |
| 27 | return unpad(cipher.decrypt(data), AES.block_size) |
| 28 | |
| 29 | # AES functions |
| 30 | def AES_set_key(ctx, key, key_len): |
| 31 | ctx.key = key[:key_len] |
| 32 | ctx.iv = get_random_bytes(AES.block_size) |
| 33 | |
| 34 | def AES_cbc_encrypt(ctx, data, out): |
| 35 | out[:] = ctx.encrypt(data) |
| 36 | |
| 37 | def AES_cbc_decrypt(ctx, data, out): |
| 38 | out[:] = ctx.decrypt(data) |