diff --git a/NDecrypt.N3DS/CIATool.cs b/NDecrypt.N3DS/CIATool.cs index 997301e..574d6a0 100644 --- a/NDecrypt.N3DS/CIATool.cs +++ b/NDecrypt.N3DS/CIATool.cs @@ -71,8 +71,9 @@ namespace NDecrypt.N3DS return false; } - // Process all NCCH partitions - ProcessAllPartitions(cia, encrypt, force, input, output); + // Process all 8 NCCH partitions + if (encrypt) EncryptAllPartitions(cia, force, input, output); + else DecryptAllPartitions(cia, force, input, output); return false; } @@ -84,16 +85,18 @@ namespace NDecrypt.N3DS } } + #endregion + + #region Decrypt + /// - /// Process all partitions in the content file data of a CIA header + /// Decrypt all partitions in the content file data of a CIA header /// /// CIA representing the 3DS CIA file - /// Indicates if the file should be encrypted or decrypted /// Indicates if the operation should be forced /// Stream representing the input /// Stream representing the output - private void ProcessAllPartitions(CIA cia, - bool encrypt, + private void DecryptAllPartitions(CIA cia, bool force, Stream input, Stream output) @@ -116,18 +119,12 @@ namespace NDecrypt.N3DS continue; } - // Process the partition, if possible - if (encrypt && ShouldEncryptPartition(cia, p, force)) - EncryptPartition(header, p, input, output); - else if (!encrypt && ShouldDecryptPartition(cia, p, force)) + // Decrypt the partition, if possible + if (ShouldDecryptPartition(cia, p, force)) DecryptPartition(header, p, input, output); } } - #endregion - - #region Decrypt - /// /// Determine if the current partition should be decrypted /// @@ -482,6 +479,39 @@ namespace NDecrypt.N3DS #region Encrypt + /// + /// Encrypt all partitions in the content file data of a CIA header + /// + /// CIA representing the 3DS CIA file + /// Indicates if the operation should be forced + /// Stream representing the input + /// Stream representing the output + private void EncryptAllPartitions(CIA cia, bool force, Stream input, Stream output) + { + // Check the partitions table + if (cia.Partitions == null) + { + Console.WriteLine("Invalid partitions table!"); + return; + } + + // Iterate over all 8 NCCH partitions + for (int p = 0; p < cia.Partitions.Length; p++) + { + // Check the partition exists + var header = cia.Partitions[0]; + if (header == null) + { + Console.WriteLine($"Partition {p} Not found... Skipping..."); + continue; + } + + // Encrypt the partition, if possible + if (ShouldEncryptPartition(cia, p, force)) + EncryptPartition(header, p, input, output); + } + } + /// /// Determine if the current partition should be encrypted /// diff --git a/NDecrypt.N3DS/ThreeDSTool.cs b/NDecrypt.N3DS/ThreeDSTool.cs index 61a64fa..19aad9e 100644 --- a/NDecrypt.N3DS/ThreeDSTool.cs +++ b/NDecrypt.N3DS/ThreeDSTool.cs @@ -70,7 +70,8 @@ namespace NDecrypt.N3DS } // Process all 8 NCCH partitions - ProcessAllPartitions(cart, encrypt, force, input, output); + if (encrypt) EncryptAllPartitions(cart, force, input, output); + else DecryptAllPartitions(cart, force, input, output); return true; } @@ -82,15 +83,18 @@ namespace NDecrypt.N3DS } } + #endregion + + #region Decrypt + /// - /// Process all partitions in the partition table of an NCSD header + /// Decrypt all partitions in the partition table of an NCSD header /// /// Cart representing the 3DS file - /// Indicates if the file should be encrypted or decrypted /// Indicates if the operation should be forced /// Stream representing the input /// Stream representing the output - private void ProcessAllPartitions(Cart cart, bool encrypt, bool force, Stream input, Stream output) + private void DecryptAllPartitions(Cart cart, bool force, Stream input, Stream output) { // Check the partitions table if (cart.Header?.PartitionsTable == null || cart.Partitions == null) @@ -109,18 +113,12 @@ namespace NDecrypt.N3DS continue; } - // Process the partition, if possible - if (encrypt && ShouldEncryptPartition(cart, p, force)) - EncryptPartition(cart, p, input, output); - else if (!encrypt && ShouldDecryptPartition(cart, p, force)) + // Decrypt the partition, if possible + if (ShouldDecryptPartition(cart, p, force)) DecryptPartition(cart, p, input, output); } } - #endregion - - #region Decrypt - /// /// Determine if the current partition should be decrypted /// s @@ -448,6 +446,38 @@ namespace NDecrypt.N3DS #region Encrypt + /// + /// Encrypt all partitions in the partition table of an NCSD header + /// + /// Cart representing the 3DS file + /// Indicates if the operation should be forced + /// Stream representing the input + /// Stream representing the output + private void EncryptAllPartitions(Cart cart, bool force, Stream input, Stream output) + { + // Check the partitions table + if (cart.Header?.PartitionsTable == null || cart.Partitions == null) + { + Console.WriteLine("Invalid partitions table!"); + return; + } + + // Iterate over all 8 NCCH partitions + for (int p = 0; p < 8; p++) + { + // Check the partition exists + if (cart.Partitions[p] == null) + { + Console.WriteLine($"Partition {p} Not found... Skipping..."); + continue; + } + + // Encrypt the partition, if possible + if (ShouldEncryptPartition(cart, p, force)) + EncryptPartition(cart, p, input, output); + } + } + /// /// Determine if the current partition should be encrypted ///