/// <summary>Performs a Cryptography operation on a given ERA file</summary> /// <param name="path">Path to the input ERA file</param> /// <param name="eraName">The naked ERA file, void of directory or extension data</param> /// <param name="outputPath">Path to </param> /// <param name="transformType">Type of Cryptography operation to perform</param> /// <param name="verboseOutput">(Optional) the object to write verbose operation output to</param> /// <returns>The output file's full path</returns> /// <exception cref="FileNotFoundException">Input ERA file does not exist</exception> public static string Crypt(string path, string eraName, string outputPath, CryptographyTransformType transformType, TextWriter verboseOutput = null) { if (string.IsNullOrWhiteSpace(outputPath)) { outputPath = path; } string input_file = Path.Combine(path, eraName) + EraFile.kExtensionEncrypted; string output_file = Path.Combine(outputPath, eraName) + EraFile.kExtensionEncrypted; // If we're encrypting, the input file will be a .bin, else the output file will be a .bin switch (transformType) { case CryptographyTransformType.Decrypt: output_file += EraFile.kExtensionDecrypted; break; case CryptographyTransformType.Encrypt: input_file += EraFile.kExtensionDecrypted; break; default: throw new KSoft.Debug.UnreachableException(transformType.ToString()); } if (!File.Exists(input_file)) { throw new FileNotFoundException("ERA file for cryptography operation does not exist", input_file); } if (verboseOutput != null) { verboseOutput.WriteLine("Input: {0}", input_file); verboseOutput.WriteLine("Output: {0}", output_file); } using (var er_fs = File.OpenRead(input_file)) using (var er = new IO.EndianReader(er_fs, Shell.EndianFormat.Big)) using (var ew_fs = new FileStream(output_file, FileMode.Create, FileAccess.Write)) using (var ew = new IO.EndianWriter(ew_fs, Shell.EndianFormat.Big)) { CryptStream(er, ew, transformType); } return(output_file); }
public static void CryptStream(IO.EndianReader input, IO.EndianWriter output, CryptographyTransformType transformType) { Contract.Requires(input != null); Contract.Requires(output != null); // This should be OK because PhxTEA is buffered //Contract.Requires(input.BaseStream != output.BaseStream); var tea = new Security.Cryptography.PhxTEA(input, output); tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyEra); switch (transformType) { case CryptographyTransformType.Decrypt: tea.Decrypt(); break; case CryptographyTransformType.Encrypt: tea.Encrypt(); break; } }