public static void CreateOutput(BaseCryptographyCliInput input, object toOutput) { // Get required format string formattedOutput = default(string); switch (input.OutputFormat) { case OutputFormat.json: formattedOutput = JsonConvert.SerializeObject(toOutput); Log.Information("Output serialized to JSON object."); break; default: formattedOutput = CLIHelpers.PropertiesToString(toOutput); Log.Information("Output serialized to console human readable object."); break; } // Process to output if (string.IsNullOrEmpty(input.OutputFilePath)) { Log.Information("Printing output to the console."); Console.WriteLine(formattedOutput); } else { Log.Information($"Data will be saved into the file {input.OutputFilePath}."); File.WriteAllText(input.OutputFilePath, formattedOutput); } }
/// <summary> /// AES Decryption service. /// </summary> /// <param name="toDecryption">CLI input model.</param> /// <returns>CLI output model with information about decryption.</returns> public static SymmetricCryptographyCliOutput DecryptionRequest(ISymmetricCryptographyCliInput toDecryption, CryptoProvider cryptoProvider) { Log.Information($"New aes decryption request => {toDecryption.CipherType}"); if (string.IsNullOrEmpty(toDecryption.Content)) { Log.Information("Data which should be decrypted missing - asking user for input."); toDecryption.Content = CLIHelpers.InformationProvider("Enter entrycpted phrase"); } if (string.IsNullOrEmpty(toDecryption.Key)) { Log.Information("The encryption key is missing - asking user for input."); toDecryption.Key = CLIHelpers.InformationProvider("Enter encryption key"); } if (string.IsNullOrEmpty(toDecryption.InitializationVector) && toDecryption.CipherType.CipherMode != CipherMode.ECB) { Log.Information("The initialization vector is missing - asking user for input"); toDecryption.InitializationVector = CLIHelpers.InformationProvider("Enter initialization vector"); } SymmetricCryptographyProperties cryptoProperties; if (toDecryption.CipherType.CipherMode == CipherMode.ECB) { // ignore IV when ECB cipher mode is used cryptoProperties = new SymmetricCryptographyProperties(toDecryption.Key, toDecryption.CipherType.CipherMode); } else { cryptoProperties = new SymmetricCryptographyProperties(toDecryption.Key, toDecryption.InitializationVector, toDecryption.CipherType.CipherMode); } cryptoProvider.CryptographyProperties = cryptoProperties; var decrypted = cryptoProvider.Decrypt(toDecryption.Content); Log.Information("Successfully decrypted."); return(new SymmetricCryptographyCliOutput(decrypted)); }