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.