public void Test_DecryptString_without_encryption_data_appended_without_associated_data() { string associatedData = null; var appendEncryptionData = false; var aesDecryptionResult = new AesDecryptionResult(); string errorMessage = ""; var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData); if (aesEncryptionResult.Success) { aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), null, hasEncryptionDataAppendedInInput: appendEncryptionData, aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce); if (!aesDecryptionResult.Success) { errorMessage = aesDecryptionResult.Message; } } else { errorMessage = aesEncryptionResult.Message; } Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage); }
/// <summary> /// Encrypts an input byte array using AES with a 256 bits key in GCM authenticated mode. /// </summary> /// <param name="cipher">The byte array of the cipher to decrypt.</param> /// <param name="key">The encryption key being used.</param> /// <returns>The decrypted output string.</returns> public static string Decrypt(byte[] cipher, byte[] key) { if (cipher == null) { throw new ArgumentNullException(nameof(cipher)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } var aes = new AEAD_AES_256_GCM(); return(Encoding.UTF8.GetString(aes.DecryptString(cipher, key))); }
/// <summary> /// Decrypts an input string using AES with a 256 bits key in GCM authenticated mode. /// </summary> /// <param name="cipher">The base64 encoded input cipher to decrypt.</param> /// <param name="key">The encryption key being used.</param> /// <returns>The decrypted output string.</returns> public static string Decrypt(string cipher, string key) { if (string.IsNullOrEmpty(cipher)) { throw new ArgumentNullException(nameof(cipher)); } if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } var aes = new AEAD_AES_256_GCM(); return(Encoding.UTF8.GetString(aes.DecryptString(cipher, key))); }
/// <summary> /// Loads JSON configuration key/values from a stream into a provider. /// </summary> /// <param name="stream">The stream to read.</param> public override void Load(Stream stream) { var source = (EncryptedJsonConfigurationSource)Source; try { var encryptedSettings = stream.ToBytes(); var aes = new AEAD_AES_256_GCM(); var settings = aes.DecryptString(encryptedSettings, source.Key); Data = EncryptedJsonConfigurationFileParser.Parse(new MemoryStream(settings)); } catch (JsonException e) { throw new FormatException("Could not parse the encrypted JSON file", e); } }
/// <summary> /// Loads JSON configuration key/values from a stream into a provider. /// </summary> public override void Load() { var source = (EncryptedJsonConfigurationSource)Source; try { var text = Convert.FromBase64String(File.ReadAllText(source.Path)); var aes = new AEAD_AES_256_GCM(); var settings = aes.DecryptString(text, source.Key); Data = EncryptedJsonConfigurationFileParser.Parse(new MemoryStream(settings)); } catch (JsonException e) { throw new FormatException("Could not parse the encrypted JSON file", e); } }