Skip to content

Commit

Permalink
make encryption public
Browse files Browse the repository at this point in the history
  • Loading branch information
K0lb3 committed Jun 9, 2022
1 parent c277565 commit c26f1eb
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 76 deletions.
6 changes: 3 additions & 3 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
</configuration>
27 changes: 11 additions & 16 deletions AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Linq;

namespace TroubleTool
{
Expand Down Expand Up @@ -37,7 +38,8 @@ internal void LoadIndex()
if (indexXml.DocumentElement.HasAttribute("mod"))
{
indexXml = IndexHelper.LoadIndex(indexBackUpPath);
} else
}
else
{
if ((!File.Exists(indexBackUpPath)) || (new FileInfo(indexPath).Length != new FileInfo(indexBackUpPath).Length))
File.Copy(indexPath, indexBackUpPath, true);
Expand Down Expand Up @@ -73,19 +75,12 @@ internal List<XmlElement> FindUnExtractedAndUpdatedEntries()

internal List<XmlElement> BackUpPackageFiles(List<String> files)
{
List<XmlElement> toBackUp = new List<XmlElement>();
foreach (String file in files)
{
XmlElement entry = indexXml.DocumentElement;
String src = Path.Combine(package, entry.GetAttribute("pack"));
String dst = Path.Combine(data, entry.GetAttribute("original"));

if (((!File.Exists(dst)) || (Int32.Parse(entry.GetAttribute("size")) != new FileInfo(dst).Length)) && (Int32.Parse(entry.GetAttribute("csize")) == new FileInfo(src).Length))
{
toBackUp.Add(entry);
}
}
return toBackUp;
return (from String file in files
let entry = indexXml.DocumentElement
let src = Path.Combine(package, entry.GetAttribute("pack"))
let dst = Path.Combine(data, entry.GetAttribute("original"))
where ((!File.Exists(dst)) || (Int32.Parse(entry.GetAttribute("size")) != new FileInfo(dst).Length)) && (Int32.Parse(entry.GetAttribute("csize")) == new FileInfo(src).Length)
select entry).ToList();
}

internal void ExtractAllEntries()
Expand Down Expand Up @@ -126,7 +121,7 @@ public static void Extract(string src, String dst, XmlElement entry)

case "encrypted_zip":
byte[] data = File.ReadAllBytes(src);
Crypt.decrypt(data, data.Length);
data = Crypt.Decrypt(data);
using (var stream = new MemoryStream(data))
{
using (ZipArchive z = new ZipArchive(stream))
Expand Down Expand Up @@ -173,7 +168,7 @@ public static void Pack(string src, String dst, XmlElement entry)
}
data = stream.ToArray();
}
Crypt.encrypt(data, data.Length);
data = Crypt.Encrypt(data);
File.WriteAllBytes(dst, data);
break;

Expand Down
167 changes: 162 additions & 5 deletions Crypt.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,169 @@
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Cryptography;
using System.Text;


namespace TroubleTool
{
static class Crypt
{
[DllImport("troublecrypt.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern int encrypt(byte[] data, int size);
[DllImport("troublecrypt.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern int decrypt(byte[] data, int size);
private static readonly string keyInit = "d4152d461ab5308429e49774a042a318";
private static readonly string ivInit = "86afc43868fea6abd40fbf6d5ed50905";
private static byte[] GenerateIV(byte[] word)
{
byte[] crypto = new byte[16];
int v4 = 0;
int v5 = 0;
int v6, v7, v8, v9;
while (v4 < 0x20)
{
v6 = word[v5];
v7 = word[v4 + 1];

if ((v6 & 0x40) != 0)
{
if ((v6 & 0x20) != 0)
v8 = v6 - 87;
else
v8 = v6 - 55;
}
else
v8 = v6 - 48;

if ((v7 & 0x40) != 0)
{
if ((v7 & 0x20) != 0)
v9 = v7 - 87;
else
v9 = v7 - 55;
}
else
v9 = v7 - 48;


crypto[v5 >> 1] = (byte)(v9 | (16 * v8));
v4 += 2;
v5 = v4;
}
return crypto;
}

private static byte[] GenerateKey(byte[] a1)
{
long r9_1; // r9
long r10_1; // r10
long rax1; // rax
int ebx5; // ebx
long r11_6; // r11
byte dl7; // dl
byte r8_7; // r8
byte al9; // al
byte cl14; // cl
byte[] key = new byte[16];

r9_1 = a1.Length;
rax1 = a1.Length;

if (rax1 == 16)
{
ebx5 = 0;
if (r9_1 > 0)
{
r11_6 = 0;
do
{
dl7 = a1[r11_6];
r8_7 = a1[ebx5 + 1];
if ((dl7 & 0x40) != 0)
{
if ((dl7 & 0x20) != 0)
al9 = ((byte)(dl7 - 87));
else
al9 = (byte)(dl7 - 55);
}
else
{
al9 = (byte)(dl7 - 48);
}
if ((r8_7 & 0x40) != 0)
{
if ((r8_7 & 0x20) != 0)
cl14 = (byte)(r8_7 - 87);
else
cl14 = (byte)(r8_7 - 55);
}
else
{
cl14 = (byte)(r8_7 - 48);
}
ebx5 += 2;
rax1 = cl14 | (16 * al9);
key[r11_6 >> 1] = (byte)rax1;
r11_6 = ebx5;
} while (ebx5 < r9_1);
}
}
return key;
}

public static byte[] Encrypt(byte[] cipherText)
{
byte[] encrypted;

using (var aes = new AesManaged
{
KeySize = 16 * 8,
BlockSize = 16 * 8,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
})
{
using (var encrypter = aes.CreateEncryptor(GenerateKey(Encoding.ASCII.GetBytes(keyInit)), GenerateIV(Encoding.ASCII.GetBytes(ivInit))))
{
using (var cipherStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
//Encrypt Data
binaryWriter.Write(cipherText);
}

encrypted = cipherStream.ToArray();
}

}
}
return encrypted;
}

public static byte[] Decrypt(byte[] cipherText)
{
byte[] decrypted;
using (var aes = new AesManaged
{
KeySize = 16 * 8,
BlockSize = 16 * 8,
Mode = CipherMode.CBC,
Padding = PaddingMode.Zeros
})
{
using (var encrypter = aes.CreateDecryptor(GenerateKey(Encoding.ASCII.GetBytes(keyInit)), GenerateIV(Encoding.ASCII.GetBytes(ivInit))))
{
using (var cipherStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
binaryWriter.Write(cipherText);
}

decrypted = cipherStream.ToArray();
}

}
}
return decrypted;
}
}
}
14 changes: 7 additions & 7 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c26f1eb

Please sign in to comment.