public void Decrypt() { var encryptedKeyXml = GetEncryptedKey(); var encryptedKey = new EncryptedKey(); encryptedKey.LoadXml(encryptedKeyXml); byte[] symmetricKey = RsaOaepSha256.Decrypt(encryptedKey.CipherData.CipherValue, Certificate); var symmetricAlgorithm = new AesGcm { Key = symmetricKey }; foreach (var attachment in Attachments) { attachment.Stream.Position = 0; var nonce = new byte[AesGcm.NonceSize / 8]; attachment.Stream.Read(nonce, 0, nonce.Length); symmetricAlgorithm.Nonce = nonce; var decryptedStream = new MemoryStream(); var cryptoStream = new CryptoStream(attachment.Stream, symmetricAlgorithm.CreateDecryptor(), CryptoStreamMode.Read); cryptoStream.CopyTo(decryptedStream); if (!cryptoStream.HasFlushedFinalBlock) { cryptoStream.FlushFinalBlock(); } attachment.Stream = decryptedStream; } }