diff options
author | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-08-22 04:16:57 +0300 |
---|---|---|
committer | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-08-22 04:16:57 +0300 |
commit | 7d1884282f667930f4ef74a82a4e912844a63089 (patch) | |
tree | 84cd828e82d51dda763b23cac49eb86b24e54938 /UVtools.Core/FileFormats/FDGFile.cs | |
parent | 70a4cbb1de9cb15e60f2a4b5c9c25735a44e557a (diff) |
Cleanup
Diffstat (limited to 'UVtools.Core/FileFormats/FDGFile.cs')
-rw-r--r-- | UVtools.Core/FileFormats/FDGFile.cs | 88 |
1 files changed, 34 insertions, 54 deletions
diff --git a/UVtools.Core/FileFormats/FDGFile.cs b/UVtools.Core/FileFormats/FDGFile.cs index 879063a..751ac55 100644 --- a/UVtools.Core/FileFormats/FDGFile.cs +++ b/UVtools.Core/FileFormats/FDGFile.cs @@ -471,8 +471,7 @@ namespace UVtools.Core.FileFormats if (Parent.HeaderSettings.EncryptionKey > 0) { - KeyRing kr = new(Parent.HeaderSettings.EncryptionKey, layerIndex); - EncodedRle = kr.Read(EncodedRle); + LayerRleReCrypt(Parent.HeaderSettings.EncryptionKey, layerIndex, EncodedRle); } int limit = image.Width * image.Height; @@ -596,8 +595,7 @@ namespace UVtools.Core.FileFormats if (Parent.HeaderSettings.EncryptionKey > 0) { - var kr = new KeyRing(Parent.HeaderSettings.EncryptionKey, layerIndex); - EncodedRle = kr.Read(rawData).ToArray(); + EncodedRle = LayerRleCrypt(Parent.HeaderSettings.EncryptionKey, layerIndex, rawData); } else { @@ -614,55 +612,6 @@ namespace UVtools.Core.FileFormats } #endregion - #region KeyRing - - public class KeyRing - { - public uint Init { get; } - public uint Key { get; private set; } - public uint Index { get; private set; } - - public KeyRing(uint seed, uint layerIndex) - { - Init = (seed - 0x1dcb76c3) ^ 0x257e2431; - Key = Init * 0x82391efd * (layerIndex ^ 0x110bdacd); - } - - public byte Next() - { - byte k = (byte)(Key >> (int)(8 * Index)); - Index++; - - if ((Index & 3) == 0) - { - Key += Init; - Index = 0; - } - - return k; - } - - public List<byte> Read(List<byte> input) - { - List<byte> data = new(input.Count); - data.AddRange(input.Select(t => (byte)(t ^ Next()))); - - return data; - } - - public byte[] Read(byte[] input) - { - byte[] data = new byte[input.Length]; - for (int i = 0; i < input.Length; i++) - { - data[i] = (byte) (input[i] ^ Next()); - } - return data; - } - } - - #endregion - #endregion #region Properties @@ -1197,6 +1146,37 @@ namespace UVtools.Core.FileFormats } } - #endregion + #endregion + + #region Static Methods + public static byte[] LayerRleCrypt(uint seed, uint layerIndex, IEnumerable<byte> input) + { + var result = input.ToArray(); + LayerRleReCrypt(seed, layerIndex, result); + return result; + } + + public static void LayerRleReCrypt(uint seed, uint layerIndex, byte[] input) + { + var init = (seed - 0x1dcb76c3) ^ 0x257e2431; + var key = init * 0x82391efd * (layerIndex ^ 0x110bdacd); + + int index = 0; + for (int i = 0; i < input.Length; i++) + { + var k = (byte)(key >> 8 * index); + + index++; + + if ((index & 3) == 0) + { + key += init; + index = 0; + } + + input[i] = (byte)(input[i] ^ k); + } + } + #endregion } } |