-
Notifications
You must be signed in to change notification settings - Fork 2
/
elite-decrypt.py
122 lines (92 loc) · 3.32 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
#!/usr/bin/env python
#
# ******************************************************************************
#
# DISC 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 "D.CODE.bin" and "T.CODE.bin"
# binaries and generates decrypted versions as "D.CODE.decrypt.bin" and
# "T.CODE.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 -rel1" for example:
#
# -rel1 Decrypt the release from Ian Bell's site
# -rel2 Decrypt the Stairway to Hell release
#
# If unspecified, the default is rel2
#
# ******************************************************************************
from __future__ import print_function
import sys
print()
print("BBC disc Elite decryption")
argv = sys.argv
release = 2
folder = "sth"
for arg in argv[1:]:
if arg == "-rel1":
release = 1
folder = "ib-disc"
if arg == "-rel2":
release = 2
folder = "sth"
if arg == "-rel3":
print("Sideways RAM variant is not encrypted")
exit()
# Configuration variables for D.CODE
load_address = 0x11E3
unscramble_from = 0x5600
unscramble_to = 0x1300
scramble_eor = 0x33
data_block = bytearray()
# Load assembled code file
elite_file = open("4-reference-binaries/" + folder + "/D.CODE.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/D.CODE.bin")
# Do decryption
# SC routine, which EORs bytes between &1300 and &9FFF
# Can be reversed by simply repeating the EOR
for n in range(unscramble_to, unscramble_from):
data_block[n - load_address] = data_block[n - load_address] ^ (n % 256) ^ scramble_eor
print("[ Decrypt ] 4-reference-binaries/" + folder + "/D.CODE.bin")
# Write output file for D.CODE.decrypt
output_file = open("4-reference-binaries/" + folder + "/D.CODE.decrypt.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/D.CODE.decrypt.bin")
# Configuration variables for T.CODE
load_address = 0x11E3
unscramble_to = 0x1300
unscramble_from = 0x6000
scramble_eor = 0x33
data_block = bytearray()
# Load assembled code file
elite_file = open("4-reference-binaries/" + folder + "/T.CODE.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
print()
print("[ Read ] 4-reference-binaries/" + folder + "/T.CODE.bin")
# Do decryption
# SC routine, which EORs bytes between &1300 and &9FFF
# Can be reversed by simply repeating the EOR
for n in range(unscramble_to, unscramble_from):
data_block[n - load_address] = data_block[n - load_address] ^ (n % 256) ^ scramble_eor
print("[ Decrypt ] 4-reference-binaries/" + folder + "/T.CODE.bin")
# Write output file for T.CODE.decrypt
output_file = open("4-reference-binaries/" + folder + "/T.CODE.decrypt.bin", "wb")
output_file.write(data_block)
output_file.close()
print("[ Save ] 4-reference-binaries/" + folder + "/T.CODE.decrypt.bin")
print()