/// <summary> /// Derive the encryption tool to be used for the given file /// </summary> /// <param name="filename">Filename to derive the tool from</param> /// <param name="decryptArgs">Arguments to pass to the tools on creation</param> /// <returns></returns> private static ITool DeriveTool(string filename, DecryptArgs decryptArgs) { FileType type = DetermineFileType(filename); switch (type) { case FileType.NDS: Console.WriteLine("File recognized as Nintendo DS"); return(new DSTool(filename, decryptArgs)); case FileType.NDSi: Console.WriteLine("File recognized as Nintendo DS"); return(new DSTool(filename, decryptArgs)); case FileType.iQueDS: Console.WriteLine("File recognized as iQue DS"); return(new DSTool(filename, decryptArgs)); case FileType.N3DS: Console.WriteLine("File recognized as Nintendo 3DS"); return(new ThreeDSTool(filename, decryptArgs)); case FileType.N3DSCIA: Console.WriteLine("File recognized as Nintendo 3DS CIA [CAUTION: NOT WORKING CURRENTLY]"); return(new CIATool(filename, decryptArgs)); case FileType.NULL: default: Console.WriteLine($"Unrecognized file format for {filename}. Expected *.nds, *.srl, *.dsi, *.3ds"); return(null); } }
public void Decrypt(DecryptArgs args) { IFileHandler privateKeyFileHandler = new FileHandler(args.PrivateKeyFileName); IFileHandler unEncryptedFileHandler = new FileHandler(args.UnencryptedFileName); IFileHandler encryptedFileHandler = new FileHandler(args.EncryptedFileName); var cryptography = new RsaCrypoProvider(); string privateKey = privateKeyFileHandler.ReadFile(); Byte[] encryptedBytes = encryptedFileHandler.ReadEncryptedFile(); string decryptedText = cryptography.Decrypt(privateKey, encryptedBytes); if (args.ShowKeys) { Console.WriteLine("Decrypted: "); Console.WriteLine(decryptedText); return; } if (args.ShowKeys) { string encryptedText = Encoding.Default.GetString(encryptedBytes); Console.WriteLine("Encrypted: "); Console.WriteLine(encryptedText); Console.WriteLine(); Console.WriteLine("Decrypted: "); Console.WriteLine(decryptedText); return; } unEncryptedFileHandler.WriteToFile(decryptedText); }
/// <summary> /// Display a basic help text /// </summary> /// <param name="path">Path to the file to process</param> /// <param name="decryptArgs">DecryptArgs to use during processing</param> /// <param name="outputHashes">True to write out a hashfile, false otherwise</param> private static void ProcessPath(string path, DecryptArgs decryptArgs, bool outputHashes) { Console.WriteLine(path); ITool tool = DeriveTool(path, decryptArgs); if (tool?.ProcessFile() != true) { Console.WriteLine("Processing failed!"); } else if (outputHashes) { WriteHashes(path); } }
public DSTool(string filename, DecryptArgs decryptArgs) { this.filename = filename; this.decryptArgs = decryptArgs; }
public static void Main(string[] args) { if (args.Length < 2) { DisplayHelp("Not enough arguments"); return; } var decryptArgs = new DecryptArgs(); if (args[0] == "decrypt" || args[0] == "d") { decryptArgs.Encrypt = false; } else if (args[0] == "encrypt" || args[0] == "e") { decryptArgs.Encrypt = true; } else { DisplayHelp($"Invalid operation: {args[0]}"); return; } bool outputHashes = false; int start = 1; for ( ; start < args.Length; start++) { if (args[start] == "-c" || args[start] == "--citra") { decryptArgs.UseCitraKeyFile = true; } else if (args[start] == "-dev" || args[start] == "--development") { decryptArgs.Development = true; } else if (args[start] == "-f" || args[start] == "--force") { decryptArgs.Force = true; } else if (args[start] == "-h" || args[start] == "--hash") { outputHashes = true; } else if (args[start] == "-k" || args[start] == "--keyfile") { if (start == args.Length - 1) { Console.WriteLine("Invalid keyfile path: no additional arguments found!"); } start++; string tempPath = args[start]; if (string.IsNullOrWhiteSpace(tempPath)) { Console.WriteLine($"Invalid keyfile path: null or empty path found!"); } tempPath = Path.GetFullPath(tempPath); if (!File.Exists(tempPath)) { Console.WriteLine($"Invalid keyfile path: file {tempPath} not found!"); } else { decryptArgs.KeyFile = tempPath; } } else { break; } } // Derive the keyfile path based on the runtime folder if not already set if (string.IsNullOrWhiteSpace(decryptArgs.KeyFile)) { if (decryptArgs.UseCitraKeyFile) { decryptArgs.KeyFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "aes_keys.txt"); } else { decryptArgs.KeyFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "keys.bin"); } } // If we are using a Citra keyfile, there are no development keys if (decryptArgs.Development && decryptArgs.UseCitraKeyFile) { Console.WriteLine("Citra keyfiles don't contain development keys; disabling the option..."); decryptArgs.Development = false; } // Initialize the constants, if possible decryptArgs.Initialize(); for (int i = start; i < args.Length; i++) { if (File.Exists(args[i])) { ProcessPath(args[i], decryptArgs, outputHashes); } else if (Directory.Exists(args[i])) { foreach (string file in Directory.EnumerateFiles(args[i], "*", SearchOption.AllDirectories)) { ProcessPath(file, decryptArgs, outputHashes); } } else { Console.WriteLine($"{args[i]} is not a file or folder. Please check your spelling and formatting and try again."); } } }