Partial file coding (C#)

Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2020-09-13 19:34:35 +07:00
commit 9f9a3f1d7f
14 changed files with 2843 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
bin/
obj/

74
Program.cs Normal file
View File

@@ -0,0 +1,74 @@
/*
* Created by SharpDevelop.
* User: JDM17
* Date: 23.09.2020
* Time: 17:48
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using Tea;
using Utils;
namespace ModelCoder
{
class Program
{
private static readonly int encodeSize = 3000;
private static bool encodeFile(string path, uint[] key)
{
Console.WriteLine("[OUTPUT] Reading: " + path);
byte[] file_bytes;
try {
file_bytes = File.ReadAllBytes(path);
} catch {
Console.WriteLine("[OUTPUT] File '" + path + "' not found");
return false;
}
ITeaCoding tea = new TeaCoding();
var block = new uint[2];
for (uint i = 0; i < encodeSize; i += 2) {
Console.WriteLine(i + " start iteration");
block[0] = file_bytes[i];
block[1] = file_bytes[i + 1];
Console.WriteLine(i + " start encoding");
tea.code(block, key);
}
return true;
}
[STAThread]
static void Main(string[] args)
{
IUtil util = new Util();
string fpath;
if (args.Length > 0) {
fpath = args[0];
} else {
Console.WriteLine("[OUTPUT] You can open file(s) with this program");
Console.WriteLine("[OUTPUT] Or drag'n'drop on it");
Console.Write("[INPUT] Enter filename (without spaces): ");
fpath = Console.ReadLine();
}
Console.Write("[INPUT] Enter key: ");
uint[] uKey = util.ConvertKey(Console.ReadLine());
Console.WriteLine("[OUTPUT] Reading file(s)...");
if (args.Length > 1) {
foreach (string i in args)
encodeFile(i, uKey);
} else {
encodeFile(fpath, uKey);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}

View File

@@ -0,0 +1,27 @@
#region Using directives
using System;
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle ("model_coder")]
[assembly: AssemblyDescription ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("model_coder")]
[assembly: AssemblyCopyright ("Copyright 2020")]
[assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture ("")]
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (false)]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion ("1.0.*")]

1
README.md Normal file
View File

@@ -0,0 +1 @@
#### Thanks [Toliak](https://github.com/Toliak) for his [MTA-TEA-Encoder](https://github.com/Toliak/MTA-TEA-Encoder)

6
app.config Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>

1
model_coder.Settings Normal file
View File

@@ -0,0 +1 @@
<OpenCoverSettings />

69
model_coder.csproj Normal file
View File

@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{794C3722-03FF-400A-A0EA-DD5FD78AE01D}</ProjectGuid>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Exe</OutputType>
<RootNamespace>model_coder</RootNamespace>
<AssemblyName>model_coder</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<NoWin32Manifest>False</NoWin32Manifest>
<SignAssembly>False</SignAssembly>
<DelaySign>False</DelaySign>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>False</RunCodeAnalysis>
<SourceAnalysisOverrideSettingsFile>C:\Users\JDM17\AppData\Roaming\ICSharpCode\SharpDevelop5\Settings.SourceAnalysis</SourceAnalysisOverrideSettingsFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<BaseAddress>4194304</BaseAddress>
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
<StartAction>Project</StartAction>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="utils.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="tea.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

2
model_coder.sdsettings Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<Properties />

18
model_coder.sln Normal file
View File

@@ -0,0 +1,18 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
# SharpDevelop 5.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model_coder", "model_coder.csproj", "{E7DCB830-5ACF-4B60-A14A-525047F074C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E7DCB830-5ACF-4B60-A14A-525047F074C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7DCB830-5ACF-4B60-A14A-525047F074C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7DCB830-5ACF-4B60-A14A-525047F074C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7DCB830-5ACF-4B60-A14A-525047F074C0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

77
tea.cs Normal file
View File

@@ -0,0 +1,77 @@
/*
* Created by SharpDevelop.
* User: JDM17
* Date: 23.09.2020
* Time: 17:50
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace Tea
{
public interface ITeaCoding
{
void code(uint[] v, uint[] k);
//void decode(uint[] v, uint[] k);
}
public partial class TeaCoding: ITeaCoding
{
private readonly uint c_delta = 0x9E3779B9;
/*
public void encode(uint[] v, uint[] k) {
uint v0 = v[0], v1 = v[1];
uint sum = 0;
//for(int i = 0; i < 32; i++) {
for(uint i = 32; i > 0; i--) {
//v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
//sum += c_delta;
//v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
//sum += c_delta;
//v0 += (v1 << 4) + k[0] ^ v1 + sum ^ (v1 >> 5) + k[1];
//v1 += (v0 << 4) + k[2] ^ v0 + sum ^ (v0 >> 5) + k[3];
v0 += (v1 << 4 ^ v1 >> 5) + v1 ^ sum + k[sum & 3];
sum += c_delta;
v1 += (v0 << 4 ^ v0 >> 5) + v0 ^ sum + k[sum >> 11 & 3];
}
v[0] = v0;
v[1] = v1;
}
*/
public void code(uint[] v, uint[] k)
{
uint y = v[0];
uint z = v[1];
uint sum = 0;
uint n = 32;
while (n-- > 0)
{
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
sum += c_delta;
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
}
v[0] = y;
v[1] = z;
}
/*
private readonly uint c_sum = 0xC6EF3720;
public void decode(uint[] v, uint[] k) {
uint v0 = v[0], v1 = v[1];
uint sum = c_sum;
for (int i = 0; i < 32; i++) {
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
sum -= c_delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
}
v[0] = v0;
v[1] = v1;
}
*/
}
}

View File

@@ -0,0 +1,32 @@
local key = "1234" -- Model key
local fileName = "partial_file_encode" -- File name
local encodeSize = 36000 -- Encoding size (3000*12)
-- Reading crypted file
local file = fileOpen(fileName .. ".enc")
local encodedFileData = fileRead(file, encodeSize)
fileSetPos(file, encodeSize)
local originalFileData = fileRead(file, fileGetSize(file) - encodeSize)
fileClose(file)
-- Decoding file data
local block = ""
local decoded = ""
local encodedKey = string.sub(utf8.lower(md5(key)), 1, 16)
for i = 1, encodeSize, 12 do
block = teaDecode(string.sub(encodedFileData, i, i + 12), encodedKey)
decoded = decoded .. block
end
-- Writing uncrypted data to clean file
file = fileCreate(fileName .. ".dec")
fileWrite(file, decoded .. originalFileData)
fileClose(file)
originalFileData = nil
encodedFileData = nil
encodedKey = nil
decoded = nil
block = nil
file = nil

BIN
test/partial_file_encode Normal file

Binary file not shown.

2474
test/partial_file_encode.enc Normal file

File diff suppressed because one or more lines are too long

60
utils.cs Normal file
View File

@@ -0,0 +1,60 @@
/*
* Created by SharpDevelop.
* User: JDM17
* Date: 23.09.2020
* Time: 20:59
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Text;
using System.Security.Cryptography;
namespace Utils
{
public interface IUtil
{
uint[] ConvertKey(string input);
uint ConvertStringToUInt(string input);
string ConvertUIntToString(uint input);
}
public partial class Util: IUtil
{
public uint[] ConvertKey(string input)
{
// MD5 Hashing
var md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
var sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
sb.Append(hashBytes[i].ToString("X2"));
// Converting to uint[]
string key = sb.ToString().ToLower().Substring(0, 16);
var formattedKey = new uint[4];
int j = 0;
for (int i = 0; i < key.Length; i += 4)
formattedKey[j++] = ConvertStringToUInt(key.Substring(i, 4));
return formattedKey;
}
public uint ConvertStringToUInt(string input)
{
uint output;
output = ((uint)input[0]);
output += ((uint)input[1] << 8);
output += ((uint)input[2] << 16);
output += ((uint)input[3] << 24);
return output;
}
public string ConvertUIntToString(uint input)
{
var output = new StringBuilder();
output.Append((char)((input & 0xFF)));
output.Append((char)((input >> 8) & 0xFF));
output.Append((char)((input >> 16) & 0xFF));
output.Append((char)((input >> 24) & 0xFF));
return output.ToString();
}
}
}