Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null reference exception while decrypting stream to stream #38

Open
virouzrx opened this issue Mar 21, 2022 · 1 comment
Open

Null reference exception while decrypting stream to stream #38

virouzrx opened this issue Mar 21, 2022 · 1 comment

Comments

@virouzrx
Copy link

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?

@0n5000
Copy link

0n5000 commented May 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants