From 747625c2b8e91eed7d405e9a13a6ebebf7686c8a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 12 Oct 2024 01:42:02 -0400 Subject: [PATCH] Path is part of the args now --- NDecrypt.Core/ITool.cs | 6 ++++-- NDecrypt.N3DS/CIATool.cs | 16 ++++++---------- NDecrypt.N3DS/ThreeDSTool.cs | 16 ++++++---------- NDecrypt.Nitro/DSTool.cs | 24 ++++++++++++------------ NDecrypt/Program.cs | 14 +++++++------- 5 files changed, 35 insertions(+), 41 deletions(-) diff --git a/NDecrypt.Core/ITool.cs b/NDecrypt.Core/ITool.cs index edff870..ee526a8 100644 --- a/NDecrypt.Core/ITool.cs +++ b/NDecrypt.Core/ITool.cs @@ -5,15 +5,17 @@ /// /// Attempts to encrypt an input file /// + /// Name of the file to encrypt /// Indicates if the operation should be forced /// True if the file could be encrypted, false otherwise - bool EncryptFile(bool force); + bool EncryptFile(string filename, bool force); /// /// Attempts to decrypt an input file /// + /// Name of the file to decrypt /// Indicates if the operation should be forced /// True if the file could be decrypted, false otherwise - bool DecryptFile(bool force); + bool DecryptFile(string filename, bool force); } } diff --git a/NDecrypt.N3DS/CIATool.cs b/NDecrypt.N3DS/CIATool.cs index 14d4772..e427966 100644 --- a/NDecrypt.N3DS/CIATool.cs +++ b/NDecrypt.N3DS/CIATool.cs @@ -14,11 +14,6 @@ namespace NDecrypt.N3DS // https://www.3dbrew.org/wiki/CIA public class CIATool : ITool { - /// - /// Name of the input CIA file - /// - private readonly string filename; - /// /// Decryption args to use while processing /// @@ -49,26 +44,27 @@ namespace NDecrypt.N3DS /// private readonly BigInteger[] NormalKey2C = new BigInteger[8]; - public CIATool(string filename, DecryptArgs decryptArgs) + public CIATool(DecryptArgs decryptArgs) { - this.filename = filename; this.decryptArgs = decryptArgs; } #region Common Methods /// - public bool EncryptFile(bool force) => ProcessFile(encrypt: true, force); + public bool EncryptFile(string filename, bool force) + => ProcessFile(filename, encrypt: true, force); /// - public bool DecryptFile(bool force) => ProcessFile(encrypt: false, 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(bool encrypt, bool force) + private bool ProcessFile(string filename, bool encrypt, bool force) { // Ensure the constants are all set if (decryptArgs.IsReady != true) diff --git a/NDecrypt.N3DS/ThreeDSTool.cs b/NDecrypt.N3DS/ThreeDSTool.cs index 9a435ca..1973c1e 100644 --- a/NDecrypt.N3DS/ThreeDSTool.cs +++ b/NDecrypt.N3DS/ThreeDSTool.cs @@ -12,11 +12,6 @@ namespace NDecrypt.N3DS { public class ThreeDSTool : ITool { - /// - /// Name of the input 3DS file - /// - private readonly string filename; - /// /// Decryption args to use while processing /// @@ -47,26 +42,27 @@ namespace NDecrypt.N3DS /// private readonly BigInteger[] NormalKey2C = new BigInteger[8]; - public ThreeDSTool(string filename, DecryptArgs decryptArgs) + public ThreeDSTool(DecryptArgs decryptArgs) { - this.filename = filename; this.decryptArgs = decryptArgs; } #region Common Methods /// - public bool EncryptFile(bool force) => ProcessFile(encrypt: true, force); + public bool EncryptFile(string filename, bool force) + => ProcessFile(filename, encrypt: true, force); /// - public bool DecryptFile(bool force) => ProcessFile(encrypt: false, 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(bool encrypt, bool force) + private bool ProcessFile(string filename, bool encrypt, bool force) { // Ensure the constants are all set if (decryptArgs.IsReady != true) diff --git a/NDecrypt.Nitro/DSTool.cs b/NDecrypt.Nitro/DSTool.cs index f85432f..57cd905 100644 --- a/NDecrypt.Nitro/DSTool.cs +++ b/NDecrypt.Nitro/DSTool.cs @@ -10,11 +10,6 @@ namespace NDecrypt.Nitro { public class DSTool : ITool { - /// - /// Name of the input DS/DSi file - /// - private readonly string filename; - #region Encryption process variables private uint[] _cardHash = new uint[0x412]; @@ -22,15 +17,12 @@ namespace NDecrypt.Nitro #endregion - public DSTool(string filename, DecryptArgs decryptArgs) - { - this.filename = filename; - } + public DSTool() { } #region Encrypt /// - public bool EncryptFile(bool force) + public bool EncryptFile(string filename, bool force) { try { @@ -46,6 +38,10 @@ namespace NDecrypt.Nitro return false; } + // Reset state variables + _cardHash = new uint[0x412]; + _arg2 = new uint[3]; + // Encrypt the secure area EncryptSecureArea(cart, force, reader, writer); return true; @@ -180,7 +176,7 @@ namespace NDecrypt.Nitro #region Decrypt /// - public bool DecryptFile(bool force) + public bool DecryptFile(string filename, bool force) { try { @@ -189,13 +185,17 @@ namespace NDecrypt.Nitro using var writer = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); // Deserialize the cart information - Cart? cart = NitroDeserializer.DeserializeStream(reader); + var cart = NitroDeserializer.DeserializeStream(reader); if (cart == null) { Console.WriteLine("Error: Not a DS or DSi Rom!"); return false; } + // Reset state variables + _cardHash = new uint[0x412]; + _arg2 = new uint[3]; + // Decrypt the secure area DecryptSecureArea(cart, force, reader, writer); diff --git a/NDecrypt/Program.cs b/NDecrypt/Program.cs index a83bc5e..65733cf 100644 --- a/NDecrypt/Program.cs +++ b/NDecrypt/Program.cs @@ -150,12 +150,12 @@ namespace NDecrypt if (tool == null) return; - if (encrypt && !tool.EncryptFile(force)) + if (encrypt && !tool.EncryptFile(path, force)) { Console.WriteLine("Encryption failed!"); return; } - else if (!encrypt && !tool.DecryptFile(force)) + else if (!encrypt && !tool.DecryptFile(path, force)) { Console.WriteLine("Decryption failed!"); return; @@ -204,19 +204,19 @@ More than one path can be specified at a time."); { case FileType.NDS: Console.WriteLine("File recognized as Nintendo DS"); - return new DSTool(filename, decryptArgs); + return new DSTool(); case FileType.NDSi: Console.WriteLine("File recognized as Nintendo DS"); - return new DSTool(filename, decryptArgs); + return new DSTool(); case FileType.iQueDS: Console.WriteLine("File recognized as iQue DS"); - return new DSTool(filename, decryptArgs); + return new DSTool(); case FileType.N3DS: Console.WriteLine("File recognized as Nintendo 3DS"); - return new ThreeDSTool(filename, decryptArgs); + return new ThreeDSTool(decryptArgs); case FileType.N3DSCIA: Console.WriteLine("File recognized as Nintendo 3DS CIA [CAUTION: NOT WORKING CURRENTLY]"); - return new CIATool(filename, decryptArgs); + return new CIATool(decryptArgs); case FileType.NULL: default: Console.WriteLine($"Unrecognized file format for {filename}. Expected *.nds, *.nds.enc, *.srl, *.dsi, *.3ds");