Skip to content

Null reference exception while decrypting stream to stream #38

@virouzrx

Description

@virouzrx

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");
    }
}
  1. 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;
        }
  1. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions