De/Encrypt in-place to enhance performance

This commit is contained in:
BtbN 2019-06-14 16:58:06 +02:00
parent 9e49e27833
commit d07e941272
4 changed files with 18 additions and 20 deletions

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -83,16 +83,15 @@ namespace ClanServer.Formatters
{
data = await Task.Run(() =>
{
IEnumerable<byte> rawData = data;
if (eAmuseInfo != null)
rawData = RC4.ApplyEAmuseInfo(eAmuseInfo, data);
RC4.ApplyEAmuseInfo(eAmuseInfo, data);
switch (compAlgo.ToLower())
{
case "lz77":
return LZ77.Decompress(rawData).ToArray();
return LZ77.Decompress(data).ToArray();
case "none":
return rawData.ToArray();
return data;
default:
return null;
}

View File

@ -48,7 +48,7 @@ namespace ClanServer.Formatters
compressed = null;
if (data.EamuseInfo != null)
resData = RC4.ApplyEAmuseInfo(data.EamuseInfo, resData).ToArray();
RC4.ApplyEAmuseInfo(data.EamuseInfo, resData);
return (resData, algo);
});

View File

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System;
@ -11,16 +11,16 @@ namespace eAmuseCore.Crypto
new byte[]{ 0x69, 0xd7, 0x46, 0x27, 0xd9, 0x85, 0xee, 0x21, 0x87, 0x16, 0x15, 0x70, 0xd0,
0x8d, 0x93, 0xb1, 0x24, 0x55, 0x03, 0x5b, 0x6d, 0xf0, 0xd8, 0x20, 0x5d, 0xf5 };
public static IEnumerable<byte> ApplyEAmuseInfo(string info, IEnumerable<byte> data)
public static void ApplyEAmuseInfo(string info, byte[] data)
{
if (!info.StartsWith("1-") || info.Count(c => c == '-') != 2 || info.Length != 15)
throw new ArgumentException("Unknown E-Amuse-Info format.", "info");
info = info.Substring(2).Replace("-", "");
byte[] key = Enumerable.Range(0, info.Length / 2).Select(i => Convert.ToByte(info.Substring(i * 2, 2), 16)).ToArray();
return ApplyEAmuse(key, data);
ApplyEAmuse(key, data);
}
public static IEnumerable<byte> ApplyEAmuse(byte[] key, IEnumerable<byte> data)
public static void ApplyEAmuse(byte[] key, byte[] data)
{
if (key.Length != 6)
throw new ArgumentException("Key length has to be exactly 6 bytes.", "key");
@ -28,13 +28,13 @@ namespace eAmuseCore.Crypto
using (MD5 md5 = MD5.Create())
{
byte[] realKey = md5.ComputeHash(key.Concat(konamiCode).ToArray());
return EncryptOutput(realKey, data);
EncryptOutput(realKey, data);
}
}
public static IEnumerable<byte> Apply(byte[] key, IEnumerable<byte> data)
public static void Apply(byte[] key, byte[] data)
{
return EncryptOutput(key, data);
EncryptOutput(key, data);
}
private static byte[] EncryptInitalize(byte[] key)
@ -50,21 +50,21 @@ namespace eAmuseCore.Crypto
return s;
}
private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
private static void EncryptOutput(byte[] key, byte[] data)
{
byte[] s = EncryptInitalize(key);
uint i = 0;
uint j = 0;
foreach (byte b in data)
for (int k = 0; k < data.Length; ++k)
{
i = (i + 1) & 0xff;
j = (j + s[i]) & 0xff;
Swap(s, i, j);
yield return (byte)(b ^ s[(s[i] + s[j]) & 0xff]);
data[k] ^= s[(s[i] + s[j]) & 0xff];
};
}

View File

@ -22,13 +22,12 @@ namespace eAmuseTest
compress = compress.ToLower();
IEnumerable<byte> decryptedData = data;
if (eamuse_info != null)
decryptedData = RC4.ApplyEAmuseInfo(eamuse_info, data);
RC4.ApplyEAmuseInfo(eamuse_info, data);
var rawData = decryptedData;
byte[] rawData = data;
if (compress == "lz77")
rawData = LZ77.Decompress(decryptedData);
rawData = LZ77.Decompress(data).ToArray();
else if (compress != "none")
throw new ArgumentException("Unsupported compression algorithm");