-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelite-decrypt.py
123 lines (94 loc) · 3.15 KB
/
elite-decrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
#
# ******************************************************************************
#
# BBC MASTER ELITE DECRYPTION SCRIPT
#
# Written by Mark Moxon
#
# This script removes encryption and checksums from the compiled binaries for
# the main game code. It reads the encrypted "BCODE.bin" and "BDATA.bin"
# binaries and generates decrypted versions as "BCODE.decrypt.bin" and
# "BDATA.decrypt.bin
#
# Files are saved using the decrypt.bin suffix so they don't overwrite any
# existing unprot.bin files, so they can be compared if required
#
# Run this script by changing directory to the repository's root folder and
# running the script with "python 2-build-files/elite-decrypt.py"
#
# You can decrypt specific releases by adding the following arguments, as in
# "python 2-build-files/elite-decrypt.py -rel2" for example:
#
# -rel1 Decrypt the SNG47 release
# -rel2 Decrypt the Master Compact release
#
# If unspecified, the default is rel1
#
# ******************************************************************************
from __future__ import print_function
import sys
print()
print("BBC Master Elite decryption")
argv = sys.argv
release = 1
folder = "sng47"
for arg in argv[1:]:
if arg == "-rel1":
release = 1
folder = "sng47"
if arg == "-rel2":
release = 2
folder = "compact"
# Configuration variables for BCODE
load_address = 0x1300
seed = 0x19
unscramble_to = 0x2CC1
if release == 1:
# SNG47
unscramble_from = 0x7F47
elif release == 2:
# Compact
unscramble_from = 0x7FEC
data_block = bytearray()
# Load assembled code file
elite_file = open("4-reference-binaries/" + folder + "/BCODE.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/BCODE.bin")
# Do decryption
for n in range(unscramble_from, unscramble_to - 1, -1):
new = (data_block[n - load_address] - seed) % 256
data_block[n - load_address] = new
seed = new
print("[ Decrypt ] 4-reference-binaries/" + folder + "/BCODE.bin")
# Write output file for BCODE.decrypt
output_file = open("4-reference-binaries/" + folder + "/BCODE.decrypt.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/BCODE.decrypt.bin")
# Configuration variables for BDATA
load_address = 0x1300 + 0x5D00
seed = 0x62
unscramble_from = 0xB1FF
unscramble_to = 0x8000
data_block = bytearray()
# Load assembled code file
elite_file = open("4-reference-binaries/" + folder + "/BDATA.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/BDATA.bin")
# Do decryption
for n in range(unscramble_from, unscramble_to - 1, -1):
new = (data_block[n - load_address] - seed) % 256
data_block[n - load_address] = new
seed = new
print("[ Decrypt ] 4-reference-binaries/" + folder + "/BDATA.bin")
# Write output file for BDATA.decrypt
output_file = open("4-reference-binaries/" + folder + "/BDATA.decrypt.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/BDATA.decrypt.bin")
print()