diff --git a/PKHeX.Core/Saves/MemeCrypto/SwishCrypto.cs b/PKHeX.Core/Saves/MemeCrypto/SwishCrypto.cs index c0ddffc5d..e3f08f393 100644 --- a/PKHeX.Core/Saves/MemeCrypto/SwishCrypto.cs +++ b/PKHeX.Core/Saves/MemeCrypto/SwishCrypto.cs @@ -96,11 +96,20 @@ public static bool GetIsHashValid(byte[] data) /// Hash is assumed to be valid before calling this method. /// public static IReadOnlyList Decrypt(byte[] data) + { + var temp = GetDecryptedRawData(data); + return ReadBlocks(temp); + } + + /// + /// Decrypts the save data, with raw block data concatenated together. + /// + public static byte[] GetDecryptedRawData(byte[] data) { // de-ref from input data, since we're going to modify the contents in-place var temp = (byte[])data.Clone(); CryptStaticXorpadBytes(temp); - return ReadBlocks(temp); + return temp; } private static IReadOnlyList ReadBlocks(byte[] data) @@ -122,6 +131,21 @@ private static IReadOnlyList ReadBlocks(byte[] data) /// Decrypted save data /// Encrypted save data. public static byte[] Encrypt(IReadOnlyList blocks) + { + var result = GetDecryptedRawData(blocks); + CryptStaticXorpadBytes(result); + + var hash = ComputeHash(result); + hash.CopyTo(result, result.Length - SIZE_HASH); + + return result; + } + + /// + /// Tries to encrypt the save data. + /// + /// Raw save data without the final xorpad layer. + public static byte[] GetDecryptedRawData(IEnumerable blocks) { using var ms = new MemoryStream(); foreach (var block in blocks) @@ -133,12 +157,6 @@ public static byte[] Encrypt(IReadOnlyList blocks) // Allocate hash bytes at the end var result = new byte[ms.Length + SIZE_HASH]; ms.ToArray().CopyTo(result, 0); - - CryptStaticXorpadBytes(result); - - var hash = ComputeHash(result); - hash.CopyTo(result, result.Length - SIZE_HASH); - return result; } }