Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SupersetZip.cs #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions AsusSupersetDecryptor/SupersetZip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using System.IO.Compression;
using System.Runtime.InteropServices;
using Vanara.PInvoke;
Expand Down Expand Up @@ -72,7 +72,7 @@ public SupersetZip(string zipFileName)
throw new FormatException("Invalid ASUS ZIP file.");
}
}

public void ReconstructZipFile(Stream outStream)
{
using (var encStream = _encEntry.Open())
Expand All @@ -88,44 +88,52 @@ public void ReconstructZipFile(Stream outStream)
zipDataStream.CopyTo(outStream);
}
}

// I changed this because of
private MemoryStream DecryptZipCentralDirectory(Stream encStream)
{
if (!InitializeDecryptionContext(out var hprov, out var hcryptkey))
throw new InvalidOperationException("Unable to initialize decryption context for whatever reason.");

var ms = new MemoryStream();
var bytes = new byte[256];

while (encStream.Position < encStream.Length)
using (var memoryStream = new MemoryStream())
{
var actualCount = encStream.Read(bytes);
var final = encStream.Position >= encStream.Length;

if (!AdvApi32.CryptDecrypt(
hcryptkey,
Crypt32.HCRYPTHASH.NULL,
final,
0,
bytes,
ref actualCount
))
encStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

var ms = new MemoryStream();
var bytes = new byte[256];

while (memoryStream.Position < memoryStream.Length)
{
throw new InvalidOperationException("Decryption failed for whatever reason.");
var actualCount = memoryStream.Read(bytes);
var final = memoryStream.Position >= memoryStream.Length;

if (!AdvApi32.CryptDecrypt(
hcryptkey,
Crypt32.HCRYPTHASH.NULL,
final,
0,
bytes,
ref actualCount
))
{
throw new InvalidOperationException("Decryption failed for whatever reason.");
}

ms.Write(bytes, 0, actualCount);
}

ms.Write(bytes[0..actualCount]);
}
ms.Seek(0, SeekOrigin.Begin);
ms.SetLength(ms.Length - 4); // We don't care about the 4-byte checksum at the end.

AdvApi32.CryptReleaseContext(hprov);
AdvApi32.CryptDestroyKey(hcryptkey);

ms.Seek(0, SeekOrigin.Begin);
ms.SetLength(ms.Length - 4); // We don't give a single flying fuck about the 4-byte checksum at the end.

AdvApi32.CryptReleaseContext(hprov);
AdvApi32.CryptDestroyKey(hcryptkey);

return ms;
return ms;
}
}



private bool InitializeDecryptionContext(out AdvApi32.SafeHCRYPTPROV hprov, out Crypt32.HCRYPTKEY key)
{
var ret = false;
Expand Down Expand Up @@ -215,4 +223,4 @@ public void Dispose()
_archive.Dispose();
}
}
}
}