mattosaurus / PgpCore

.NET Core class library for using PGP
MIT License
245 stars 98 forks source link

OutOfMemory Exception on Encrypting a large stream #152

Closed avnet78 closed 2 years ago

avnet78 commented 2 years ago

I read data from a 2GB file into a memory stream and why I try to encrypt the stream a System.OutOfMemoryException exception is being thrown.

Here's my code:

public Stream EncryptSync(Stream inputStream)
        {
            var pgp = new PGP();
            StreamWriter outputStream = new StreamWriter(memoryStream, Encoding.UTF8);
            Stream publicKeyStream = GenerateStreamFromString(publicKey);
            using (inputStream)
            {
                pgp.EncryptStream(inputStream, outputStream.BaseStream, publicKeyStream, false, false);
                outputStream.BaseStream.Seek(0, SeekOrigin.Begin);
            }
            return outputStream.BaseStream;
        }

Please advise.

mattosaurus commented 2 years ago

Hi, this is probably because you're holding your output stream in memory and it's using more than you have available.

The EncryptStream method is reasonably efficent so the issue is probably with how you're handling the output stream. See the below test for an example of how stream handling can work.

https://github.com/mattosaurus/PgpCore/blob/1474004307f054c75b134c362f83869823f70ad0/PgpCore.Tests/UnitTests/UnitTestsAsync.cs#L1473-L1495

avnet78 commented 2 years ago

Thank you @mattosaurus