forked from shalzuth/ROMEncryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROMUnityXor.cs
54 lines (52 loc) · 1.95 KB
/
ROMUnityXor.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ROMEncryption
{
public static class ROMUnityXor
{
public static Byte[] ROMXorKey = new Byte[] { 0xde, 0xff, 0xdc, 0x60, 0xfe, 0xfe, 0xdf, 0xff };
public static void XorChain(Byte[] input, Byte[] xorChain)
{
for (int i = 0; i < input.Length; i++)
{
var xorChainOffset = i % xorChain.Length;
input[i] = (Byte)(input[i] ^ xorChain[xorChainOffset]);
}
}
public static void FixHeader(Byte[] fileBytes)
{
// not sure, correct header? still partially encrypted, but hardcoding this hack...
fileBytes[0] = 0x4d;
fileBytes[1] = 0x5a;
fileBytes[2] = 0x90;
fileBytes[3] = 0;
fileBytes[4] = 3;
fileBytes[5] = 0;
var sigIndex = 0;
for (int i = 0; i < fileBytes.Length - 4; i++)
{
if (Encoding.ASCII.GetString(fileBytes, i, 4) == "BSJB")
{
sigIndex = i;
break;
}
}
var cor20Header = BitConverter.ToUInt32(fileBytes, 0x18c);
var virtualAddress = BitConverter.ToUInt32(fileBytes, 0x184);
var metadataAddr = sigIndex + virtualAddress - cor20Header;
var metadataAddrBytes = BitConverter.GetBytes(metadataAddr);
var oldMetadataAddr = BitConverter.ToUInt32(fileBytes, (int)cor20Header + 0x10);
Array.Copy(metadataAddrBytes, 0, fileBytes, cor20Header + 0x10, 4);
}
public static void DecryptFile(String file)
{
var fileBytes = File.ReadAllBytes(file).Skip(0x10).ToArray();
XorChain(fileBytes, ROMXorKey);
FixHeader(fileBytes);
File.WriteAllBytes(file.Replace(".dll", ".fixed.dll"), fileBytes);
}
}
}