-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathelite-decrypt.py
More file actions
226 lines (174 loc) · 6.15 KB
/
elite-decrypt.py
File metadata and controls
226 lines (174 loc) · 6.15 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
#
# ******************************************************************************
#
# COMMODORE 64 ELITE DECRYPTION SCRIPT
#
# Written by Mark Moxon
#
# This script removes encryption and checksums from the compiled binaries for
# the Commodore 64 version of Elite
#
# 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 GMA85 NTSC release
# -rel2 Decrypt the GMA86 PAL release
# -rel3 Decrypt the version built by the source disk
# -rel4 Decrypt the binaries already on the source disk
#
# If unspecified, the default is rel1
#
# ******************************************************************************
from __future__ import print_function
import sys
print()
print("Commodore 64 Elite decryption")
argv = sys.argv
release = 1
folder = "gma85-ntsc"
for arg in argv[1:]:
if arg == "-rel1":
release = 1
folder = "gma85-ntsc"
if arg == "-rel2":
release = 2
folder = "gma86-pal"
if arg == "-rel3":
release = 3
folder = "source-disk-build"
if arg == "-rel4":
release = 4
folder = "source-disk-files"
# Configuration variables for scrambling code and calculating checksums
#
# Values must match those in 3-assembled-output/compile.txt
#
# If you alter the source code, then you should extract the correct values for
# the following variables and plug them into the following, otherwise the game
# will fail the checksum process and will hang on loading
#
# You can find the correct values for these variables by building your updated
# source, and then searching compile.txt for "elite-checksum.py", where the new
# values will be listed
if release == 1 or release == 2:
# GMA85/GMA86
b = 0x1D00 # B%
g = 0x1D81 # G%
w = 0x4000 # W%
x = 0x7590 # X%
u = 0x75E4 # U%
prg = True # Convert gma4, gma5, gma6
padding_comlod = 5 # Unencrypted bytes at the end of each file
padding_locode = 3
padding_hicode = 9
elif release == 3 or release == 4:
# Source disk
b = 0x1D00 # B%
g = 0x1D7E # G%
w = 0x4000 # W%
x = 0x7601 # X%
u = 0x7655 # U%
prg = False # Convert COMLOD, LOCODE, HICODE
padding_comlod = 0 # Unencrypted bytes at the end of each file
padding_locode = 0
padding_hicode = 0
# Load assembled HICODE/gma6 file
data_block = bytearray()
if prg:
elite_file = open("4-reference-binaries/" + folder + "/gma6.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
data_block = data_block[2:]
else:
elite_file = open("4-reference-binaries/" + folder + "/HICODE.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/HICODE.bin")
# Do decryption
seed = 0x49
unscramble_from = len(data_block) - 1 - padding_hicode
unscramble_to = 0 - 1
updated_seed = seed
for n in range(unscramble_from, unscramble_to, -1):
new = (data_block[n] - updated_seed) % 256
data_block[n] = new
updated_seed = new
print("[ Decrypt ] 4-reference-binaries/" + folder + "/HICODE.bin")
# Save decrypted file
output_file = open("4-reference-binaries/" + folder + "/HICODE.decrypted.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/HICODE.decrypted.bin")
# Load assembled LOCODE/gma5 file
data_block = bytearray()
if prg:
elite_file = open("4-reference-binaries/" + folder + "/gma5.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
data_block = data_block[2:]
else:
elite_file = open("4-reference-binaries/" + folder + "/LOCODE.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/LOCODE.bin")
# Do decryption
seed = 0x36
unscramble_from = len(data_block) - 1 - padding_locode
unscramble_to = g - b - 1
updated_seed = seed
for n in range(unscramble_from, unscramble_to, -1):
new = (data_block[n] - updated_seed) % 256
data_block[n] = new
updated_seed = new
print("[ Decrypt ] 4-reference-binaries/" + folder + "/LOCODE.bin")
# Save decrypted file
output_file = open("4-reference-binaries/" + folder + "/LOCODE.decrypted.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/LOCODE.decrypted.bin")
# Load assembled COMLOD/gma4 file
data_block = bytearray()
if prg:
elite_file = open("4-reference-binaries/" + folder + "/gma4.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
data_block = data_block[2:]
else:
elite_file = open("4-reference-binaries/" + folder + "/COMLOD.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/COMLOD.bin")
# Do decryption
seed = 0x8E
unscramble_from = len(data_block) - 1 - padding_comlod
unscramble_to = u - w - 1
updated_seed = seed
for n in range(unscramble_from, unscramble_to, -1):
new = (data_block[n] - updated_seed) % 256
data_block[n] = new
updated_seed = new
seed = 0x6C
unscramble_from = x - w - 1
unscramble_to = 0 - 1
updated_seed = seed
for n in range(unscramble_from, unscramble_to, -1):
new = (data_block[n] - updated_seed) % 256
data_block[n] = new
updated_seed = new
print("[ Decrypt ] 4-reference-binaries/" + folder + "/COMLOD.bin")
# Save decrypted file
output_file = open("4-reference-binaries/" + folder + "/COMLOD.decrypted.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/COMLOD.decrypted.bin")
print()