Biztalk Server 2016 : PGP decryption using Bouncy Castle

pooja jagtap 1 Reputation point
2021-02-11T05:44:28.08+00:00

Hi All,

I am using bouncy castle for decryption in custom pipeline cxomponent.

At this point in code i am getting null reference error. what could be possible reason and workaround for this.

foreach (PgpPublicKeyEncryptedData pubKeyDataItem in encryptedData.GetEncryptedDataObjects())

Please advise. Thanks in advance.

Regards,
Pooja

Microsoft BizTalk Server
Microsoft BizTalk Server
A family of Microsoft server products that support large-scale implementation management of enterprise application integration processes.
365 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Leo Erlandsson 1,656 Reputation points
    2021-02-11T07:18:38.49+00:00

    Hi,

    Unfortunately it's impossible to say without more code or a stacktrace.

    But the only thing that could be null in the code you provided is the variable encryptedData.

    Br,
    Leo

    0 comments No comments

  2. pooja jagtap 1 Reputation point
    2021-02-11T08:03:28.88+00:00

    This is method :

    public static string DecryptPgpData(Stream inputStream, Stream privateKeyStream, string passPhrase)
    {
    string output;

            PgpObjectFactory pgpFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
            // find secret key
            PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
    
            PgpObject pgp = null;
            if (pgpFactory != null)
            {
                pgp = pgpFactory.NextPgpObject();
            }
    
            // the first object might be a PGP marker packet.
            PgpEncryptedDataList encryptedData = null;
            if (pgp is PgpEncryptedDataList)
            {
                encryptedData = (PgpEncryptedDataList)pgp;
            }
            else
            {
                encryptedData = (PgpEncryptedDataList)pgpFactory.NextPgpObject();
            }
    
            // decrypt
            PgpPrivateKey privateKey = null;
            PgpPublicKeyEncryptedData pubKeyData = null;
            ***foreach (PgpPublicKeyEncryptedData pubKeyDataItem in encryptedData.GetEncryptedDataObjects())***
            {
                privateKey = FindSecretKey(pgpKeyRing, pubKeyDataItem.KeyId, passPhrase.ToCharArray());
    
                if (privateKey != null)
                {
                    pubKeyData = pubKeyDataItem;
                    break;
                }
            }
    
            if (privateKey == null)
            {
                throw new ArgumentException("Secret key for message not found.");
            }
    
            PgpObjectFactory plainFact = null;
            using (Stream clear = pubKeyData.GetDataStream(privateKey))
            {
                plainFact = new PgpObjectFactory(clear);
            }
    
            PgpObject message = plainFact.NextPgpObject();
    
            if (message is PgpCompressedData)
            {
                PgpCompressedData compressedData = (PgpCompressedData)message;
                PgpObjectFactory pgpCompressedFactory = null;
    
                using (Stream compDataIn = compressedData.GetDataStream())
                {
                    pgpCompressedFactory = new PgpObjectFactory(compDataIn);
                }
    
                message = pgpCompressedFactory.NextPgpObject();
                PgpLiteralData literalData = null;
                if (message is PgpOnePassSignatureList)
                {
                    message = pgpCompressedFactory.NextPgpObject();
                }
    
                literalData = (PgpLiteralData)message;
                using (Stream unc = literalData.GetInputStream())
                {
                    output = IoHelper.GetString(unc);
                }
    
            }
            else if (message is PgpLiteralData)
            {
                PgpLiteralData literalData = (PgpLiteralData)message;
                using (Stream unc = literalData.GetInputStream())
                {
                    output = IoHelper.GetString(unc);
                }
            }
            else if (message is PgpOnePassSignatureList)
            {
                throw new PgpException("Encrypted message contains a signed message - not literal data.");
            }
            else
            {
                throw new PgpException("Message is not a simple encrypted file - type unknown.");
            }
    
            return output;
        }
    
    0 comments No comments

  3. pooja jagtap 1 Reputation point
    2021-02-11T10:41:59.6+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.