Add support for DXArchive8 encryption.

This commit is contained in:
Sławomir Śpiewak 2024-07-14 20:59:18 +02:00
parent e2d4ddb59a
commit 2a6f3ab70c
2 changed files with 56 additions and 1 deletions

View File

@ -26,6 +26,7 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;
using GameRes.Utility;
namespace GameRes.Formats.DxLib
@ -153,4 +154,57 @@ namespace GameRes.Formats.DxLib
throw new NotSupportedException ("SHA-256 key cannot be restored.");
}
}
[Serializable]
public class DxKey8 : DxKey
{
public DxKey8(string password) : base(password ?? "DXARC")
{
}
public override byte[] GetEntryKey(string name)
{
var password = this.Password;
var path = name.Split('\\', '/');
password += string.Join("", path.Reverse().Select(n => n.ToUpperInvariant()));
return CreateKey(password);
}
protected override byte[] CreateKey(string keyword)
{
//from DxArchive.cpp
//first check if the keyword is too short
if (keyword.Length < 4)
{
keyword += "DXARC";
}
string oddString, evenString;
byte[] key = new byte[7];
oddString = string.Concat(keyword.Where((c, i) => i % 2 == 0));
evenString = string.Concat(keyword.Where((c, i) => (i+1) % 2 == 0));
UInt32 crc_0, crc_1;
crc_0 = Crc32.Compute(Encodings.ASCII.GetBytes(oddString), 0, oddString.Length);
crc_1 = Crc32.Compute(Encodings.ASCII.GetBytes(evenString), 0, evenString.Length);
byte[] crc_0_Bytes = BitConverter.GetBytes(crc_0),crc_1_Bytes=BitConverter.GetBytes(crc_1);
key[0] = crc_0_Bytes[0];
key[1] = crc_0_Bytes[1];
key[2] = crc_0_Bytes[2];
key[3] = crc_0_Bytes[3];
key[4] = crc_1_Bytes[0];
key[5] = crc_1_Bytes[1];
key[6] = crc_1_Bytes[2];
return key;
/*
using (var sha = SHA256.Create())
{
var bytes = Encodings.cp932.GetBytes(keyword);
return sha.ComputeHash(bytes);
} */
}
protected override string RestoreKey(byte[] key)
{
throw new NotSupportedException("CRC key cannot be restored.");
}
}
}

View File

@ -34,6 +34,7 @@ namespace GameRes
public static class Encodings
{
public static readonly Encoding cp932 = Encoding.GetEncoding(932);
public static readonly Encoding ASCII = Encoding.GetEncoding(32765);
public static Encoding WithFatalFallback (this Encoding enc)
{