Initial commit

This commit is contained in:
BtbN 2019-04-13 17:17:16 +02:00
commit 18a12f86ca
6 changed files with 149 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vs/
bin/
obj/

80
eAmuseCore/Crypto/RC4.cs Normal file
View File

@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System;
namespace eAmuseCore.Crypto
{
public static class RC4
{
private static readonly byte[] konamiCode =
new byte[]{ 105, 215, 70, 39, 217, 133, 238, 33, 135,
22, 21, 112, 208, 141, 147, 177, 36, 85,
3, 91, 109, 240, 216, 32, 93, 245 };
public static IEnumerable<byte> ApplyEAmuseInfo(string info, IEnumerable<byte> data)
{
if (!info.StartsWith("1-") || info.Count(c => c == '-') != 3 || 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))).ToArray();
return ApplyEAmuse(key, data);
}
public static IEnumerable<byte> ApplyEAmuse(byte[] key, IEnumerable<byte> data)
{
if (key.Length != 6)
throw new ArgumentException("Key length has to be exactly 6 bytes.", "key");
using (MD5 md5 = MD5.Create())
{
byte[] realKey = md5.ComputeHash(key.Concat(konamiCode).ToArray());
return Apply(realKey, data);
}
}
public static IEnumerable<byte> Apply(byte[] key, IEnumerable<byte> data)
{
return EncryptOutput(key, data);
}
private static byte[] EncryptInitalize(byte[] key)
{
byte[] s = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray();
for (uint i = 0, j = 0; i < 256; i++)
{
j = (j + key[i % key.Length] + s[i]) & 255;
Swap(s, i, j);
}
return s;
}
private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
{
byte[] s = EncryptInitalize(key);
uint i = 0;
uint j = 0;
return data.Select(b =>
{
i = (i + 1) & 255;
j = (j + s[i]) & 255;
Swap(s, i, j);
return (byte)(b ^ s[(s[i] + s[j]) & 255]);
});
}
private static void Swap(byte[] s, uint i, uint j)
{
byte c = s[i];
s[i] = s[j];
s[j] = c;
}
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
</ItemGroup>
</Project>

31
eAmuseSharp.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "eAmuseCore", "eAmuseCore\eAmuseCore.csproj", "{016891F1-140A-4DDF-B036-BDFAF98D4414}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eAmuseTest", "eAmuseTest\eAmuseTest.csproj", "{0BB29C0C-2ED9-4D1A-BD03-908BEAAD463C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{016891F1-140A-4DDF-B036-BDFAF98D4414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{016891F1-140A-4DDF-B036-BDFAF98D4414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{016891F1-140A-4DDF-B036-BDFAF98D4414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{016891F1-140A-4DDF-B036-BDFAF98D4414}.Release|Any CPU.Build.0 = Release|Any CPU
{0BB29C0C-2ED9-4D1A-BD03-908BEAAD463C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0BB29C0C-2ED9-4D1A-BD03-908BEAAD463C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BB29C0C-2ED9-4D1A-BD03-908BEAAD463C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BB29C0C-2ED9-4D1A-BD03-908BEAAD463C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5060C3AB-087C-41A8-96FF-CFB9AA8011B7}
EndGlobalSection
EndGlobal

12
eAmuseTest/Program.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
namespace eAmuseTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\eAmuseCore\eAmuseCore.csproj" />
</ItemGroup>
</Project>