(EAGLS): encryption variation.

This commit is contained in:
morkt 2016-10-12 11:59:48 +04:00
parent 5b3c4e6abe
commit 92c0ed03e6
8 changed files with 216 additions and 70 deletions

View File

@ -107,6 +107,9 @@
</Compile> </Compile>
<Compile Include="DDSystem\ArcDDP.cs" /> <Compile Include="DDSystem\ArcDDP.cs" />
<Compile Include="Dogenzaka\ArcBIN.cs" /> <Compile Include="Dogenzaka\ArcBIN.cs" />
<Compile Include="Eagls\WidgetEAGLS.xaml.cs">
<DependentUpon>WidgetEAGLS.xaml</DependentUpon>
</Compile>
<Compile Include="ExHibit\WidgetGYU.xaml.cs"> <Compile Include="ExHibit\WidgetGYU.xaml.cs">
<DependentUpon>WidgetGYU.xaml</DependentUpon> <DependentUpon>WidgetGYU.xaml</DependentUpon>
</Compile> </Compile>
@ -667,6 +670,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Eagls\WidgetEAGLS.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="ExHibit\WidgetGYU.xaml"> <Page Include="ExHibit\WidgetGYU.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>

View File

@ -27,7 +27,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Linq; using System.Text;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
using GameRes.Utility; using GameRes.Utility;
namespace GameRes.Formats.Eagls namespace GameRes.Formats.Eagls
@ -47,10 +49,8 @@ namespace GameRes.Formats.Eagls
} }
internal static readonly string IndexKey = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/-@:^[]"; internal static readonly string IndexKey = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/-@:^[]";
internal static readonly string Key = "EAGLS_SYSTEM"; internal static readonly byte[] EaglsKey = Encoding.ASCII.GetBytes ("EAGLS_SYSTEM");
internal static readonly byte[] AdvSysKey = Encoding.ASCII.GetBytes ("ADVSYS");
// FIXME not thread-safe
CRuntimeRandomGenerator m_rng = new CRuntimeRandomGenerator();
public override ArcFile TryOpen (ArcView file) public override ArcFile TryOpen (ArcView file)
{ {
@ -70,6 +70,7 @@ namespace GameRes.Formats.Eagls
bool long_offsets = 40 == entry_size; bool long_offsets = 40 == entry_size;
int name_size = long_offsets ? 0x18 : 0x14; int name_size = long_offsets ? 0x18 : 0x14;
long first_offset = LittleEndian.ToUInt32 (index, name_size); long first_offset = LittleEndian.ToUInt32 (index, name_size);
bool has_scripts = false;
var dir = new List<Entry>(); var dir = new List<Entry>();
while (index_offset < index.Length) while (index_offset < index.Length)
{ {
@ -79,7 +80,10 @@ namespace GameRes.Formats.Eagls
index_offset += name_size; index_offset += name_size;
var entry = FormatCatalog.Instance.Create<Entry> (name); var entry = FormatCatalog.Instance.Create<Entry> (name);
if (name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase)) if (name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase))
{
entry.Type = "script"; entry.Type = "script";
has_scripts = true;
}
if (long_offsets) if (long_offsets)
{ {
entry.Offset = LittleEndian.ToInt64 (index, index_offset) - first_offset; entry.Offset = LittleEndian.ToInt64 (index, index_offset) - first_offset;
@ -99,32 +103,28 @@ namespace GameRes.Formats.Eagls
if (0 == dir.Count) if (0 == dir.Count)
return null; return null;
if (dir[0].Name.EndsWith (".gr", StringComparison.InvariantCultureIgnoreCase)) // CG archive if (dir[0].Name.EndsWith (".gr", StringComparison.InvariantCultureIgnoreCase)) // CG archive
return new CgArchive (file, this, dir); {
else var rng = DetectEncryptionScheme (file, dir[0]);
return new EaglsArchive (file, this, dir, new CgEncryption (rng));
}
else if (has_scripts)
{
var enc = QueryEncryption();
if (enc != null)
return new EaglsArchive (file, this, dir, enc);
}
return new ArcFile (file, this, dir); return new ArcFile (file, this, dir);
} }
public override Stream OpenEntry (ArcFile arc, Entry entry) public override Stream OpenEntry (ArcFile arc, Entry entry)
{ {
var cg_arc = arc as CgArchive; var earc = arc as EaglsArchive;
if (null != cg_arc) if (null == earc
return cg_arc.DecryptEntry (entry); || !(entry.Name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase) ||
if (entry.Name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase)) entry.Name.EndsWith (".gr", StringComparison.InvariantCultureIgnoreCase)))
return DecryptDat (arc, entry);
return arc.File.CreateStream (entry.Offset, entry.Size); return arc.File.CreateStream (entry.Offset, entry.Size);
}
Stream DecryptDat (ArcFile arc, Entry entry) return earc.DecryptEntry (entry);
{
byte[] input = arc.File.View.ReadBytes (entry.Offset, entry.Size);
int text_offset = 3600;
int text_length = (int)(entry.Size - text_offset - 2);
m_rng.SRand ((sbyte)input[input.Length-1]);
for (int i = 0; i < text_length; i += 2)
{
input[text_offset + i] ^= (byte)Key[m_rng.Rand() % Key.Length];
}
return new MemoryStream (input);
} }
byte[] DecryptIndex (ArcView idx) byte[] DecryptIndex (ArcView idx)
@ -134,13 +134,14 @@ namespace GameRes.Formats.Eagls
using (var view = idx.CreateViewAccessor (0, (uint)idx.MaxOffset)) using (var view = idx.CreateViewAccessor (0, (uint)idx.MaxOffset))
unsafe unsafe
{ {
m_rng.SRand (view.ReadInt32 (idx_size)); var rng = new CRuntimeRandomGenerator();
rng.SRand (view.ReadInt32 (idx_size));
byte* ptr = view.GetPointer (0); byte* ptr = view.GetPointer (0);
try try
{ {
for (int i = 0; i < idx_size; ++i) for (int i = 0; i < idx_size; ++i)
{ {
output[i] = (byte)(ptr[i] ^ IndexKey[m_rng.Rand() % IndexKey.Length]); output[i] = (byte)(ptr[i] ^ IndexKey[rng.Rand() % IndexKey.Length]);
} }
return output; return output;
} }
@ -150,15 +151,69 @@ namespace GameRes.Formats.Eagls
} }
} }
} }
IRandomGenerator DetectEncryptionScheme (ArcView file, Entry first_entry)
{
int signature = (file.View.ReadInt32 (first_entry.Offset) >> 8) & 0xFFFF;
byte seed = file.View.ReadByte (first_entry.Offset+first_entry.Size-1);
IRandomGenerator[] rng_list = {
new LehmerRandomGenerator(),
new CRuntimeRandomGenerator(),
};
foreach (var rng in rng_list)
{
rng.SRand (seed);
rng.Rand(); // skip LZSS control byte
int test = signature;
test ^= EaglsKey[rng.Rand() % EaglsKey.Length];
test ^= EaglsKey[rng.Rand() % EaglsKey.Length] << 8;
// FIXME
// as key is rather short, this detection could produce false results sometimes
if (0x4D42 == test) // 'BM'
return rng;
}
throw new UnknownEncryptionScheme();
} }
internal interface IRandomGenerator public override ResourceOptions GetDefaultOptions ()
{
IEntryEncryption enc = null;
if (!string.IsNullOrEmpty (Settings.Default.EAGLSEncryption))
KnownSchemes.TryGetValue (Settings.Default.EAGLSEncryption, out enc);
return new EaglsOptions { Encryption = enc };
}
public override object GetAccessWidget ()
{
return new GUI.WidgetEAGLS();
}
IEntryEncryption QueryEncryption ()
{
var options = Query<EaglsOptions> (arcStrings.ArcEncryptedNotice);
return options.Encryption;
}
internal static Dictionary<string, IEntryEncryption> KnownSchemes = new Dictionary<string, IEntryEncryption>
{
{ "EAGLS", new EaglsEncryption() },
{ "AdvSys", new AdvSysEncryption() },
};
}
public class EaglsOptions : ResourceOptions
{
public IEntryEncryption Encryption;
}
public interface IRandomGenerator
{ {
void SRand (int seed); void SRand (int seed);
int Rand (); int Rand ();
} }
internal class CRuntimeRandomGenerator : IRandomGenerator [Serializable]
public class CRuntimeRandomGenerator : IRandomGenerator
{ {
uint m_seed; uint m_seed;
@ -174,7 +229,8 @@ namespace GameRes.Formats.Eagls
} }
} }
internal class LehmerRandomGenerator : IRandomGenerator [Serializable]
public class LehmerRandomGenerator : IRandomGenerator
{ {
int m_seed; int m_seed;
@ -197,57 +253,86 @@ namespace GameRes.Formats.Eagls
} }
} }
internal class CgArchive : ArcFile public interface IEntryEncryption
{ {
IRandomGenerator m_rng; void Decrypt (byte[] data);
}
public CgArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir) [Serializable]
public class CgEncryption : IEntryEncryption
{
readonly byte[] Key = PakOpener.EaglsKey;
readonly IRandomGenerator m_rng;
public CgEncryption (IRandomGenerator rng)
{
m_rng = rng;
}
public void Decrypt (byte[] data)
{
m_rng.SRand (data[data.Length-1]);
int limit = Math.Min (data.Length-1, 0x174b);
for (int i = 0; i < limit; ++i)
{
data[i] ^= (byte)Key[m_rng.Rand() % Key.Length];
}
}
}
[Serializable]
public class EaglsEncryption : IEntryEncryption
{
readonly byte[] Key = PakOpener.EaglsKey;
readonly IRandomGenerator m_rng;
public EaglsEncryption ()
{
m_rng = new CRuntimeRandomGenerator();
}
public void Decrypt (byte[] data)
{
int text_offset = 3600;
int text_length = data.Length - text_offset - 2;
m_rng.SRand ((sbyte)data[data.Length-1]);
for (int i = 0; i < text_length; i += 2)
{
data[text_offset + i] ^= Key[m_rng.Rand() % Key.Length];
}
}
}
[Serializable]
public class AdvSysEncryption : IEntryEncryption
{
readonly byte[] Key = PakOpener.AdvSysKey;
public void Decrypt (byte[] data)
{
int text_offset = 136000;
int text_length = data.Length - text_offset;
for (int i = 0; i < text_length; ++i)
{
data[text_offset + i] ^= Key[i % Key.Length];
}
}
}
internal class EaglsArchive : ArcFile
{
readonly IEntryEncryption Encryption;
public EaglsArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, IEntryEncryption enc)
: base (arc, impl, dir) : base (arc, impl, dir)
{ {
try Encryption = enc;
{
m_rng = DetectEncryptionScheme();
}
catch
{
this.Dispose();
throw;
}
}
IRandomGenerator DetectEncryptionScheme ()
{
var first_entry = Dir.First();
int signature = (File.View.ReadInt32 (first_entry.Offset) >> 8) & 0xFFFF;
byte seed = File.View.ReadByte (first_entry.Offset+first_entry.Size-1);
IRandomGenerator[] rng_list = {
new LehmerRandomGenerator(),
new CRuntimeRandomGenerator(),
};
foreach (var rng in rng_list)
{
rng.SRand (seed);
rng.Rand(); // skip LZSS control byte
int test = signature;
test ^= PakOpener.Key[rng.Rand() % PakOpener.Key.Length];
test ^= PakOpener.Key[rng.Rand() % PakOpener.Key.Length] << 8;
// FIXME
// as key is rather short, this detection could produce false results sometimes
if (0x4D42 == test) // 'BM'
return rng;
}
throw new UnknownEncryptionScheme();
} }
public Stream DecryptEntry (Entry entry) public Stream DecryptEntry (Entry entry)
{ {
var input = File.View.ReadBytes (entry.Offset, entry.Size); byte[] input = File.View.ReadBytes (entry.Offset, entry.Size);
m_rng.SRand (input[input.Length-1]); Encryption.Decrypt (input);
int limit = Math.Min (input.Length-1, 0x174b);
for (int i = 0; i < limit; ++i)
{
input[i] ^= (byte)PakOpener.Key[m_rng.Rand() % PakOpener.Key.Length];
}
return new MemoryStream (input); return new MemoryStream (input);
} }
} }

View File

@ -0,0 +1,9 @@
<StackPanel x:Class="GameRes.Formats.GUI.WidgetEAGLS"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fmt="clr-namespace:GameRes.Formats.Eagls"
xmlns:p="clr-namespace:GameRes.Formats.Properties"
MaxWidth="200">
<ComboBox Name="Scheme" ItemsSource="{Binding}"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=EAGLSEncryption, Mode=TwoWay}" Width="150"/>
</StackPanel>

View File

@ -0,0 +1,22 @@
using System.Windows.Controls;
using System.Linq;
using GameRes.Formats.Eagls;
using GameRes.Formats.Strings;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for WidgetEAGLS.xaml
/// </summary>
public partial class WidgetEAGLS : StackPanel
{
public WidgetEAGLS ()
{
InitializeComponent ();
var schemes = new string[] { arcStrings.ArcIgnoreEncryption };
Scheme.ItemsSource = schemes.Concat (PakOpener.KnownSchemes.Keys);
if (-1 == Scheme.SelectedIndex)
Scheme.SelectedValue = PakOpener.KnownSchemes.First().Key;
}
}
}

View File

@ -645,5 +645,17 @@ namespace GameRes.Formats.Properties {
this["PAZTitle"] = value; this["PAZTitle"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string EAGLSEncryption {
get {
return ((string)(this["EAGLSEncryption"]));
}
set {
this["EAGLSEncryption"] = value;
}
}
} }
} }

View File

@ -158,5 +158,8 @@
<Setting Name="PAZTitle" Type="System.String" Scope="User"> <Setting Name="PAZTitle" Type="System.String" Scope="User">
<Value Profile="(Default)" /> <Value Profile="(Default)" />
</Setting> </Setting>
<Setting Name="EAGLSEncryption" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

View File

@ -160,6 +160,9 @@
<setting name="PAZTitle" serializeAs="String"> <setting name="PAZTitle" serializeAs="String">
<value /> <value />
</setting> </setting>
<setting name="EAGLSEncryption" serializeAs="String">
<value />
</setting>
</GameRes.Formats.Properties.Settings> </GameRes.Formats.Properties.Settings>
</userSettings> </userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

View File

@ -420,6 +420,7 @@ Draculius <span class="footnote">ShiinaRio v2.38</span><br/>
Enkaku Sousa <span class="footnote">2.36 or 2.37</span><br/> Enkaku Sousa <span class="footnote">2.36 or 2.37</span><br/>
Gensou no Idea ~Oratorio Phantasm Historia~<span class="footnote">ShiinaRio v2.49</span><br/> Gensou no Idea ~Oratorio Phantasm Historia~<span class="footnote">ShiinaRio v2.49</span><br/>
Gohoushi Nurse ~Mayonaka no Kyousei Call~ <span class="footnote">ShiinaRio v2.50</span><br/> Gohoushi Nurse ~Mayonaka no Kyousei Call~ <span class="footnote">ShiinaRio v2.50</span><br/>
Hana to Otome ni Shukufuku o <span class="footnote">ShiinaRio v2.46</span><br/>
Helter Skelter <span class="footnote">ShiinaRio v2.40</span><br/> Helter Skelter <span class="footnote">ShiinaRio v2.40</span><br/>
Hin wa Bokura no Fuku no Kami <span class="footnote">ShiinaRio v2.49</span><br/> Hin wa Bokura no Fuku no Kami <span class="footnote">ShiinaRio v2.49</span><br/>
Hitozuma Onna Kyoushi Reika <span class="footnote">ShiinaRio v2.39</span><br/> Hitozuma Onna Kyoushi Reika <span class="footnote">ShiinaRio v2.39</span><br/>
@ -524,6 +525,8 @@ Futago Hinyuu x 3<br/>
Oppai Baka<br/> Oppai Baka<br/>
Oshiete! Yuiko Sensei<br/> Oshiete! Yuiko Sensei<br/>
Mainichi Shabutte Ii Desu ka?<br/> Mainichi Shabutte Ii Desu ka?<br/>
Ryoujoku Chikan Jigoku<br/>
Saimin Class WONDERFUL<br/>
Senpai - Oppai - Kako ni Modori Pai<br/> Senpai - Oppai - Kako ni Modori Pai<br/>
Tsuribaka ~Gakuen Taikou! Joshikousei Tsuriage Adventure~<br/> Tsuribaka ~Gakuen Taikou! Joshikousei Tsuriage Adventure~<br/>
</td></tr> </td></tr>
@ -567,6 +570,7 @@ Medorei ~Okasareta Houkago~<br/>
Onna Kyoushi<br/> Onna Kyoushi<br/>
Ore to Kanojo wa Shuju na Kankei<br/> Ore to Kanojo wa Shuju na Kankei<br/>
Saishuu Chikan Densha<br/> Saishuu Chikan Densha<br/>
Saishuu Chikan Densha 3<br/>
Serina<br/> Serina<br/>
Ura Nyuugaku ~Ineki ni Nureta Kyoukasho~<br/> Ura Nyuugaku ~Ineki ni Nureta Kyoukasho~<br/>
</td></tr> </td></tr>
@ -878,6 +882,7 @@ Gakuen Saimin Reido<br/>
<tr class="odd last"><td>*.akb</td><td><tt>AKB</tt></td><td>No</td></tr> <tr class="odd last"><td>*.akb</td><td><tt>AKB</tt></td><td>No</td></tr>
<tr><td>*.cpz</td><td><tt>CPZ5</tt><br/><tt>CPZ6</tt></td><td>No</td><td rowspan="2">CMVS</td><td rowspan="2"> <tr><td>*.cpz</td><td><tt>CPZ5</tt><br/><tt>CPZ6</tt></td><td>No</td><td rowspan="2">CMVS</td><td rowspan="2">
Amatsutsumi<br/> Amatsutsumi<br/>
Ashita no Kimi to Au Tame ni<br/>
Chrono Clock<br/> Chrono Clock<br/>
Hapymaher<br/> Hapymaher<br/>
Haruiro Ouse<br/> Haruiro Ouse<br/>