0

I am using the PGP bouncy Castle decryption using C# based on code in link here https://gist.github.com/tekmaven/03fddd1b156f0149f28e66a7c675d94e.

I have encrypted a sample file and have successfully decrypted the file with the same code and keys.

But I got a encrypted file from Unix server, encryption done using the same public key but was not able to do decryption.

There was no "secret key error" messages if the secret key is wrong. So the key is correct.

The issue is that I get is 'Object reference not set to an instance of object' when doing the foreach loop in the code below

 encryptedData.GetEncryptedDataObjects()

Trying to go back to the previous code before reaching this I see that pgpF is null. See below.

// InputStream.length = 211

PgpObjectFactory pgpF = new PgpObjectFactory(PgpUtilities.GetDecoderStream(InputStream));  // Issue here.

Tried to debug it I see the InputStream.length = 211. so InputStream is not an empty value or null value.

I could see the file being UTF8. I opened the file and do see characters there.

I have looked at other sources to see if someone has similar issue related to BouncyCastle. But found none.

Edit 2:

Adding more code - Decrypt part of the code:

public static string DecryptPgpData(Stream inputStream, Stream privateKeyStream, string passPhrase, string OutputFile)
        {
            try 
       {

            PgpObjectFactory pgpF = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
            // find secret key
            PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));

            PgpObject pgp = null;
            if (pgpF != null)
            {
                pgp = pgpF.NextPgpObject();
            }

            // the first object might be a PGP marker packet.
            PgpEncryptedDataList encryptedData = null;
            if (pgp is PgpEncryptedDataList)
            {
                encryptedData = (PgpEncryptedDataList)pgp;
            }
            else
            {
                encryptedData = (PgpEncryptedDataList)pgpF.NextPgpObject();
            }

            // decrypt
            PgpPrivateKey privateKey = null;
            PgpPublicKeyEncryptedData pubKeyData = null;
            foreach (PgpPublicKeyEncryptedData pubKeyDataItem in encryptedData.GetEncryptedDataObjects())
            {

............

}

code in main program part

using(FileStream wsfs = new FileStream(fileFullPath, FileMode.Open))
using(BinaryReader WinScpBinaryReader = new BinaryReader(wsfs))
{  

     TextToDecrypt = Convert.ToBase64STring(WinScpBinaryReader.ReadBytes(Convert.ToInt32(WinScpFileStream.Length)));

}

        pgpTest.DecryptPgpData(
        new MemoryStream(Encoding.UTF8.GetBytes(TextToDecrypt ?? "")),
        new MemoryStream(Encoding.UTF8.GetBytes(PrivateKeyText ?? ""),
        Passphrase,
        OutputFile);

Playing with lots of files.Using notepad++

The successful encryption and decryption I did earlier, shows Window (CR LF UTF-8) at the bottom.

The troublesome file from server, shows (Unix (LF) ANSI or UTF -8)

Will these difference causes the error?

Edit 3:

While I was testing lots of different files by copying the decryptable contents to the file that was having issue. I noticed I get another error. This time it is "System.IO.EndOfStreamException: Premature end of stream in PartialInputSTream"

Without any changes to the code, I am using "using" to open the file. (Looking at the other users who have posted similar issues on the sites)

The file contents are same but the fileStream length is different.

  • Please don't repost the same question that has already been closed. – President James K. Polk Dec 21 '19 at 13:54
  • 1
    `Trying to go back to the previous code before reaching this I see that pgpF is null. See below.` That's nonsense, either that assignment is never reached, or it is not `null`. There is simply no way that the `new` operator generates `null`. I think you may have a valid question, but please try and create good sample code, and describe what is happening to the best of your possibilities. Currently you leave us guessing to a large degree. – Maarten Bodewes Dec 21 '19 at 16:47
  • Track down the `null` properly. Use the debugger. As Maarten says, `new` will never return null. It will either throw an exception or return an object. – Blorgbeard Dec 24 '19 at 18:56
  • And please do not repost this question again. If you can edit it such that it's no longer a duplicate (i.e. boils down to more than "how do I fix NullReferenceException") then it can be reopened. – Blorgbeard Dec 24 '19 at 18:58
  • Sorry. I am editing this again. Thanks. – simpleorchid Dec 27 '19 at 17:53

0 Answers0