Cinchoo / ChoPGP

PGP library for .NET / c#
MIT License
60 stars 29 forks source link

Null reference exception while decrypting stream to stream #38

Open virouzrx opened 2 years ago

virouzrx commented 2 years ago

I've been trying to encrypt and decrypt files in memory using streams, but there is a null reference exception on

DecryptAsync(Stream inputStream, Stream outputStream, Stream privateKeyStream, string passPhrase)

method.

My code:

  1. Key generating:
    public static void CinchooPGP_GenerateKeys()
    {
    using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
    {
        pgp.GenerateKey(@"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", null, "password");
    }
    }
  2. Encryption:
        public static async Task<MemoryStream> CinchooPGPEncryption()
        {
            var ContentToEncryptStream = await FileToStreamMethod(@"C:\TEMP\Content\content.txt");
            var PublicKeyStream = await FileToStreamMethod(@"C:\TEMP\Keys\public.asc");
            MemoryStream encryptedContent = new MemoryStream();
            using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
            {
                //this accepts 3 streams: stream to encrypt, stream to decrypt, public keys
                await pgp.EncryptAsync(ContentToEncryptStream, encryptedContent, PublicKeyStream);
            }
            return encryptedContent;
        }
  3. Decryption:

        public static async Task<MemoryStream> CinchooPGPDecryption(MemoryStream encryptedContent, string passPhrase)
        {
            var PrivateKeyStream = await FileToStreamMethod(@"C:\TEMP\Keys\private.asc");
            MemoryStream decryptedContent = new MemoryStream();
    
            using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
            {
                await pgp.DecryptAsync(encryptedContent, decryptedContent, PrivateKeyStream, passPhrase); //exception is thrown here
            }
            return decryptedContent;
        }

    Stacktrace:

    at Cinchoo.PGP.ChoPGPEncryptDecrypt.Decrypt(Stream inputStream, Stream outputStream, Stream privateKeyStream, String passPhrase)
    at Cinchoo.PGP.ChoPGPEncryptDecrypt.<>c__DisplayClass47_0.<DecryptAsync>b__0()
    at System.Threading.Tasks.Task.InnerInvoke()
    at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
    at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
    --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
    at Cinchoo.PGP.ChoPGPEncryptDecrypt.<DecryptAsync>d__47.MoveNext()
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

No stream is null while I was debugging the app, so I think it's thrown somewhere inside the lib. Or maybe I am doing something wrong?

0n5000 commented 1 month ago

I had the same issue when decrypting. Make sure that you set the input stream's position to zero. ChoPgp doesn't do it.