Skip to content

Commit

Permalink
Made it automatically generate a decrypted file
Browse files Browse the repository at this point in the history
  • Loading branch information
ash47 committed Mar 22, 2018
1 parent bc3b3b2 commit 8e1472d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ MapEditorHtml/tools/Data
MapEditorHtml/tools/allents.js
ZXCheckGenerator/.vs/*
ZXCheckGenerator/bin/*
ZXCheckGenerator/obj/*
ZXCheckGenerator/obj/*
ZXCheckGenerator/packages/*
4 changes: 4 additions & 0 deletions ZXCheckGenerator/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<Costura />
</Weavers>
84 changes: 80 additions & 4 deletions ZXCheckGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -156,13 +157,13 @@ public static void extractPassword(string pathToSaveFile)
foreach (MethodInfo possibleHelper in possibleType.GetMethods(BindingFlags.NonPublic | BindingFlags.Static))
{
ParameterInfo[] helperParams = possibleHelper.GetParameters();

if (helperParams.Length != 3) continue;
if (helperParams[0].ParameterType.FullName != "System.String") continue;
if (helperParams[1].ParameterType.FullName != "System.Int32") continue;
if (helperParams[2].ParameterType.FullName != "System.Boolean") continue;

if(possibleHelper.ReturnType.FullName != "System.Void") continue;
if (possibleHelper.ReturnType.FullName != "System.Void") continue;

possibleHelper.Invoke(null, new object[] { pathToSaveFile, flag, true });

Expand All @@ -174,6 +175,53 @@ public static void extractPassword(string pathToSaveFile)

Console.WriteLine("Found password = " + thePassword);
System.IO.File.AppendAllText("_passwords.txt", "Password = " + thePassword + Environment.NewLine);

// Get a working directory
string myDir = GetTemporaryDirectory();

using (ZipFile zip = ZipFile.Read(pathToSaveFile))
{
// Add the password
zip.Password = thePassword;

// Extract all
try
{
zip.ExtractAll(myDir);

using (ZipFile newZip = new ZipFile())
{
// Add the contents of the directory to my file
newZip.AddDirectory(myDir);

// Write it to disk
string saveFolder = Path.GetDirectoryName(pathToSaveFile);
string saveBaseName = Path.GetFileNameWithoutExtension(pathToSaveFile);

// Set the filename
string newRawFileName = saveBaseName + "_decrpyted.zxsav";
string newFileName = Path.Combine(saveFolder, newRawFileName); ;
newZip.Name = saveFolder + @"\" + saveBaseName + "_decrpyted.zxsav";

// Save it
newZip.Save();

// Sign it
generateZXCheck(newFileName);

// Cleanup the temp directory
DeleteDirectory(myDir);

// Tell the user that we decrypted it
Console.WriteLine("Successfully decrypted save file! New file called '" + newRawFileName + "'");
}
}
catch(Exception e)
{
Console.WriteLine("Failed to extract zxsav :/");
Console.WriteLine(e.Message);
}
}
}

}
Expand All @@ -192,7 +240,35 @@ public static void extractPassword(string pathToSaveFile)
}
}
}


// Deletes a directory
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);

foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}

foreach (string dir in dirs)
{
DeleteDirectory(dir);
}

Directory.Delete(target_dir, false);
}

// Returns a random temp directory to work with
public static string GetTemporaryDirectory()
{
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}

// Called when the program is launched
static void Main(string[] args)
{
Expand Down
23 changes: 23 additions & 0 deletions ZXCheckGenerator/ZXCheckGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -59,6 +61,14 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=1.6.2.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>packages\Costura.Fody.1.6.2\lib\dotnet\Costura.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="DotNetZip, Version=1.10.1.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>packages\DotNetZip.1.10.1\lib\net20\DotNetZip.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -74,8 +84,21 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="FodyWeavers.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\Fody.2.0.0\build\dotnet\Fody.targets" Condition="Exists('packages\Fody.2.0.0\build\dotnet\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Fody.2.0.0\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.2.0.0\build\dotnet\Fody.targets'))" />
<Error Condition="!Exists('packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets'))" />
</Target>
<Import Project="packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
6 changes: 6 additions & 0 deletions ZXCheckGenerator/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="1.6.2" targetFramework="net46" developmentDependency="true" />
<package id="DotNetZip" version="1.10.1" targetFramework="net46" />
<package id="Fody" version="2.0.0" targetFramework="net46" developmentDependency="true" />
</packages>

0 comments on commit 8e1472d

Please sign in to comment.