<PackageReference Include="BouncyCastle.Cryptography" Version="2.7.0" />

Org.BouncyCastle.Cms.CmsEnvelopedDataStreamGenerator

Streaming generator for CMS EnvelopedData (PKCS#7 enveloped-data) messages. Add recipients via the base CmsEnvelopedGenerator methods, then call Open to obtain a Stream to which the content to be encrypted is written; closing that stream finalizes the CMS structure.
namespace Org.BouncyCastle.Cms { public class CmsEnvelopedDataStreamGenerator : CmsEnvelopedGenerator { public CmsEnvelopedDataStreamGenerator(); public CmsEnvelopedDataStreamGenerator(SecureRandom random); public void SetBufferSize(int bufferSize); public void SetBerEncodeRecipients(bool berEncodeRecipientSet); public Stream Open(Stream outStream, string encryptionOid); public Stream Open(Stream outStream, string encryptionOid, int keySize); } }