From 651aad92a26d58353509c98d02a3e50776176a3f Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 15 Aug 2017 16:04:53 +0400 Subject: [PATCH] (LibCfiScheme): added 'rotate key' scheme parameter. --- ArcFormats/Malie/LibScheme.cs | 6 ++++-- ArcFormats/Malie/MalieEncryption.cs | 12 +++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ArcFormats/Malie/LibScheme.cs b/ArcFormats/Malie/LibScheme.cs index 70fb5769..d2dc1e51 100644 --- a/ArcFormats/Malie/LibScheme.cs +++ b/ArcFormats/Malie/LibScheme.cs @@ -80,15 +80,17 @@ namespace GameRes.Formats.Malie public class LibCfiScheme : LibScheme { public byte[] Key { get; set; } + public uint[] RotateKey { get; set; } - public LibCfiScheme (uint align, byte[] key) : base (align) + public LibCfiScheme (uint align, byte[] key, uint[] rot_key) : base (align) { Key = key; + RotateKey = rot_key; } public override IMalieDecryptor CreateDecryptor () { - return new CfiDecryptor (Key); + return new CfiDecryptor (Key, RotateKey); } } diff --git a/ArcFormats/Malie/MalieEncryption.cs b/ArcFormats/Malie/MalieEncryption.cs index 8b003896..9e8edd55 100644 --- a/ArcFormats/Malie/MalieEncryption.cs +++ b/ArcFormats/Malie/MalieEncryption.cs @@ -53,10 +53,12 @@ namespace GameRes.Formats.Malie public class CfiDecryptor : IMalieDecryptor { byte[] m_key; + uint[] m_rotate_key; - public CfiDecryptor (byte[] key) + public CfiDecryptor (byte[] key, uint[] rotate_key) { m_key = key; + m_rotate_key = rotate_key; } public void DecryptBlock (long block_offset, byte[] data, int index) @@ -79,13 +81,13 @@ namespace GameRes.Formats.Malie fixed (byte* data8 = &data[index]) { uint* data32 = (uint*)data8; - uint k = Binary.RotR (0x39653542, m_key[offset & 0x1F] ^ 0xA5); + uint k = Binary.RotR (m_rotate_key[0], m_key[offset & 0x1F] ^ 0xA5); data32[0] = Binary.RotR (data32[0] ^ k, m_key[(offset + 12) & 0x1F] ^ 0xA5); - k = Binary.RotL (0x76706367, m_key[(offset + 3) & 0x1F] ^ 0xA5); + k = Binary.RotL (m_rotate_key[1], m_key[(offset + 3) & 0x1F] ^ 0xA5); data32[1] = Binary.RotL (data32[1] ^ k, m_key[(offset + 15) & 0x1F] ^ 0xA5); - k = Binary.RotR (0x69454462, m_key[(offset + 6) & 0x1F] ^ 0xA5); + k = Binary.RotR (m_rotate_key[2], m_key[(offset + 6) & 0x1F] ^ 0xA5); data32[2] = Binary.RotR (data32[2] ^ k, m_key[(offset - 14) & 0x1F] ^ 0xA5); - k = Binary.RotL (0x71334334, m_key[(offset + 9) & 0x1F] ^ 0xA5); + k = Binary.RotL (m_rotate_key[3], m_key[(offset + 9) & 0x1F] ^ 0xA5); data32[3] = Binary.RotL (data32[3] ^ k, m_key[(offset - 11) & 0x1F] ^ 0xA5); } }