public void Test_DecryptFile_with_append_encryption_data() { var testFilePath = Path.GetTempFileName(); var appendEncryptionData = true; var aesDecryptionResult = new AesDecryptionResult(); var testFileStringContentRead = ""; var errorMessage = ""; File.WriteAllText(testFilePath, _testString); var aesEncryptionResult = _aes128cbcHmacSha256.EncryptFile(testFilePath, testFilePath, _password, false, appendEncryptionDataToOutputFile: appendEncryptionData); if (aesEncryptionResult.Success) { aesDecryptionResult = _aes128cbcHmacSha256.DecryptFile(testFilePath, testFilePath, _password, false, hasEncryptionDataAppendedInInputFile: appendEncryptionData); if (aesDecryptionResult.Success) { testFileStringContentRead = File.ReadAllText(testFilePath); } else { errorMessage = aesDecryptionResult.Message; } } else { errorMessage = aesEncryptionResult.Message; } Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && testFileStringContentRead.Equals(_testString)), errorMessage); }
private static ExitCode RunDecryptOptionsAndReturnExitCode(DecryptOptions decryptOptions) { AesDecryptionResult aesDecryptionResult = null; switch (decryptOptions.InputType.ToLower()) { case "string": { aesDecryptionResult = (decryptOptions.Algorithm.ToLower()) switch { "aes128cbc" => new AE_AES_128_CBC_HMAC_SHA_256().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password), "aes192cbc" => new AE_AES_192_CBC_HMAC_SHA_384().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password), "aes256cbc" => new AE_AES_256_CBC_HMAC_SHA_512().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password), "aes128gcm" => new AEAD_AES_128_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData), "aes192gcm" => new AEAD_AES_192_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData), "aes256gcm" => new AEAD_AES_256_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData), _ => new AesDecryptionResult() { Success = false, Message = $"Unknown algorithm \"{decryptOptions.Algorithm}\"." }, }; } break; case "file": { switch (decryptOptions.Algorithm.ToLower()) { case "aes128cbc": { using (var progressBar = new ProgressBar()) { var aes128 = new AE_AES_128_CBC_HMAC_SHA_256(); aes128.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; aes128.OnDecryptionMessage += (msg) => { progressBar.WriteLine(msg); }; aesDecryptionResult = aes128.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile); } } break; case "aes192cbc": { using (var progressBar = new ProgressBar()) { var aes192 = new AE_AES_192_CBC_HMAC_SHA_384(); aes192.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; aes192.OnDecryptionMessage += (msg) => { progressBar.WriteLine(msg); }; aesDecryptionResult = aes192.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile); } } break; case "aes256cbc": { using (var progressBar = new ProgressBar()) { var aes256 = new AE_AES_256_CBC_HMAC_SHA_512(); aes256.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; aes256.OnDecryptionMessage += (msg) => { progressBar.WriteLine(msg); }; aesDecryptionResult = aes256.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile); } } break; case "aes128gcm": case "aes192gcm": case "aes256gcm": aesDecryptionResult = new AesDecryptionResult() { Success = false, Message = $"Algorithm \"{decryptOptions.Algorithm}\" currently not available for file decryption." }; break; default: aesDecryptionResult = new AesDecryptionResult() { Success = false, Message = $"Unknown algorithm \"{decryptOptions.Algorithm}\"." }; break; } } break; default: aesDecryptionResult = new AesDecryptionResult() { Success = false, Message = $"Unknown input type \"{decryptOptions.InputType}\"." }; break; } if (aesDecryptionResult.Success) { Console.WriteLine((decryptOptions.InputType.ToLower().Equals("string") ? aesDecryptionResult.DecryptedDataString : aesDecryptionResult.Message)); return(ExitCode.Sucess); } else { Console.WriteLine(aesDecryptionResult.Message); return(ExitCode.Error); } }