Skip to content

Commit

Permalink
Try to decrypt files inside FPK archives if they begin with 0x1B or 0x1C
Browse files Browse the repository at this point in the history
  • Loading branch information
emoose committed Sep 30, 2015
1 parent f884ec6 commit a136f02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions GzsTool/Fpk/FpkEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,40 @@ private Func<Stream> ReadDataLazy(Stream input)
};
}

private bool DecryptCryptedFile(byte[] fileData, string filePath, out byte[] decryptedData)
{
var filename = Path.GetFileName(filePath);
var hash = Hashing.HashFileNameLegacy(filename, false);
var key = BitConverter.GetBytes(~hash);

var decData = new byte[fileData.Length - 1];

for (int i = 0; i < fileData.Length - 1; i++)
{
key[i & 7] ^= fileData[i + 1];
decData[i] = key[i & 7];
}

var success = decData[decData.Length - 1] == 0;
if (!success)
{
decryptedData = fileData;
return false;
}

Array.Resize(ref decData, decData.Length - 1);
decryptedData = decData;
return true;
}

private Stream ReadData(Stream input)
{
input.Position = DataOffset;
byte[] result = new byte[DataSize];
input.Read(result, 0, (int)DataSize);
if (result[0] == 0x1B || result[0] == 0x1C)
DecryptCryptedFile(result, GetFpkEntryFileName(), out result);

return new MemoryStream(result);
}

Expand Down
4 changes: 2 additions & 2 deletions GzsTool/Utility/Hashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private static ulong HashFileExtension(string fileExtension)
return HashFileName(fileExtension, false) & 0x1FFF;
}

private static ulong HashFileName(string text, bool removeExtension = true)
public static ulong HashFileName(string text, bool removeExtension = true)
{
if (removeExtension)
{
Expand All @@ -305,7 +305,7 @@ private static ulong HashFileName(string text, bool removeExtension = true)
return setFlag ? maskedHash | 0x4000000000000 : maskedHash;
}

private static ulong HashFileNameLegacy(string text, bool removeExtension = true)
public static ulong HashFileNameLegacy(string text, bool removeExtension = true)
{
if (removeExtension)
{
Expand Down

0 comments on commit a136f02

Please sign in to comment.