mattosaurus / PgpCore

.NET Core class library for using PGP
MIT License
238 stars 100 forks source link

DecryptStream is very slow #280

Closed paritoshnagar2016 closed 6 months ago

paritoshnagar2016 commented 9 months ago

I am using DecryptStream that runs very slowly like take around 1.3 mins to complete 26 MD data, but when I try pgp.Decrypt it completes in 2 sec. Is there way I can improve performance using DecryptStream

mattosaurus commented 8 months ago

Hi, DecryptStream is just a pointer to Decrypt.

https://github.com/mattosaurus/PgpCore/blob/4855a42c3e245ff510992ba2875c53a92112f738/PgpCore/PGP.DecryptSync.cs#L160

Running some test code on a similar size file I'm seeing decryption times of around 2 seconds for each method.

using PgpCore.Tests;
using System.Diagnostics;

namespace PgpCore.Debug;

internal class Program
{
    static void Main(string[] args)
    {
        TestFactory testFactory = new TestFactory();
        // Generate a 30MB file
        testFactory.Arrange(FileType.GeneratedSmall);

        PGP pgp = new PGP();
        pgp.GenerateKey(
            testFactory.PublicKeyFileInfo,
            testFactory.PrivateKeyFileInfo,
            testFactory.UserName,
            testFactory.Password
            );

        EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
        PGP pgpEncrypt = new PGP(encryptionKeys);

        using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create())
            pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream);

        // Record time to decrypt
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        using (Stream outputFileStream = testFactory.DecryptedContentFileInfo.Create())
            pgpEncrypt.Decrypt(testFactory.EncryptedContentStream, outputFileStream);
        stopwatch.Stop();
        Console.WriteLine($"Decryption using Decrypt took {stopwatch.ElapsedMilliseconds}ms");
        // 3128ms

        // Reset
        testFactory.DecryptedContentFileInfo.Delete();
        stopwatch.Reset();

        stopwatch.Start();
        using (Stream outputFileStream = testFactory.DecryptedContentFileInfo.Create())
            pgpEncrypt.DecryptStream(testFactory.EncryptedContentStream, outputFileStream);
        stopwatch.Stop();
        Console.WriteLine($"Decryption using DecryptStream took {stopwatch.ElapsedMilliseconds}ms");
        // 2406ms
    }
}

If you can provide a fully working example demonstrating the issue I'll take another look.