diff --git a/NDecrypt.N3DS/CIATool.cs b/NDecrypt.N3DS/CIATool.cs index 574d6a0..c6fb10d 100644 --- a/NDecrypt.N3DS/CIATool.cs +++ b/NDecrypt.N3DS/CIATool.cs @@ -37,18 +37,6 @@ namespace NDecrypt.N3DS /// public bool EncryptFile(string filename, bool force) - => ProcessFile(filename, encrypt: true, force); - - /// - public bool DecryptFile(string filename, bool force) - => ProcessFile(filename, encrypt: false, force); - - /// - /// Process an input file given the input values - /// - /// Indicates if the file should be encrypted or decrypted - /// Indicates if the operation should be forced - private bool ProcessFile(string filename, bool encrypt, bool force) { // Ensure the constants are all set if (_decryptArgs.IsReady != true) @@ -71,10 +59,44 @@ namespace NDecrypt.N3DS return false; } - // Process all 8 NCCH partitions - if (encrypt) EncryptAllPartitions(cia, force, input, output); - else DecryptAllPartitions(cia, force, input, output); + // Encrypt all 8 NCCH partitions + EncryptAllPartitions(cia, force, input, output); + return false; + } + catch + { + Console.WriteLine($"An error has occurred. {filename} may be corrupted if it was partially processed."); + Console.WriteLine("Please check that the file was a valid 3DS CIA file and try again."); + return false; + } + } + /// + public bool DecryptFile(string filename, bool force) + { + // Ensure the constants are all set + if (_decryptArgs.IsReady != true) + { + Console.WriteLine("Could not read keys. Please make sure the file exists and try again."); + return false; + } + + try + { + // Open the read and write on the same file for inplace processing + using var input = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var output = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + + // Deserialize the CIA information + var cia = ReadCIA(input); + if (cia == null) + { + Console.WriteLine("Error: Not a 3DS CIA!"); + return false; + } + + // Decrypt all 8 NCCH partitions + DecryptAllPartitions(cia, force, input, output); return false; } catch diff --git a/NDecrypt.N3DS/ThreeDSTool.cs b/NDecrypt.N3DS/ThreeDSTool.cs index 19aad9e..36d0a7d 100644 --- a/NDecrypt.N3DS/ThreeDSTool.cs +++ b/NDecrypt.N3DS/ThreeDSTool.cs @@ -31,22 +31,8 @@ namespace NDecrypt.N3DS _decryptArgs = decryptArgs; } - #region Common Methods - /// public bool EncryptFile(string filename, bool force) - => ProcessFile(filename, encrypt: true, force); - - /// - public bool DecryptFile(string filename, bool force) - => ProcessFile(filename, encrypt: false, force); - - /// - /// Process an input file given the input values - /// - /// Indicates if the file should be encrypted or decrypted - /// Indicates if the operation should be forced - private bool ProcessFile(string filename, bool encrypt, bool force) { // Ensure the constants are all set if (_decryptArgs.IsReady != true) @@ -69,10 +55,8 @@ namespace NDecrypt.N3DS return false; } - // Process all 8 NCCH partitions - if (encrypt) EncryptAllPartitions(cart, force, input, output); - else DecryptAllPartitions(cart, force, input, output); - + // Encrypt all 8 NCCH partitions + EncryptAllPartitions(cart, force, input, output); return true; } catch @@ -83,7 +67,41 @@ namespace NDecrypt.N3DS } } - #endregion + /// + public bool DecryptFile(string filename, bool force) + { + // Ensure the constants are all set + if (_decryptArgs.IsReady != true) + { + Console.WriteLine("Could not read keys. Please make sure the file exists and try again."); + return false; + } + + try + { + // Open the read and write on the same file for inplace processing + using var input = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var output = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + + // Deserialize the cart information + var cart = N3DSDeserializer.DeserializeStream(input); + if (cart?.Header == null || cart?.CardInfoHeader?.InitialData?.BackupHeader == null) + { + Console.WriteLine("Error: Not a 3DS cart image!"); + return false; + } + + // Decrypt all 8 NCCH partitions + DecryptAllPartitions(cart, force, input, output); + return true; + } + catch + { + Console.WriteLine($"An error has occurred. {filename} may be corrupted if it was partially processed."); + Console.WriteLine("Please check that the file was a valid 3DS or New 3DS cart image and try again."); + return false; + } + } #region Decrypt