RSA PGP decryption in C#

The following is an example of PGP RSA decryption in C#, as there are no good examples I have found thus far. This code has been tested, is complete, and I used ReSharper to clean it up as much as possible while keeping it fully working. Enjoy!

Crypto.cs

using System;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Org.BouncyCastle.Bcpg.OpenPgp;

public class Crypto
{
    private readonly string                 _passphrase; // Private key passphrase
    private readonly string                 _privateKey; // Private key in ASCII armor format (not base64)

    public string Decrypt(string inputData)
    {
        using var inputStream = inputData.GetStream();
        using var keyIn       = _privateKey.GetStream(Encoding.ASCII);
        var       output      = Decrypt(inputStream, keyIn, _passphrase);

        return output;
    }

    private string Decrypt(Stream inputStream, Stream privateKeyStream, string passPhrase)
    {
        string output;

        var pgpFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
        // find secret key
        var pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
        var pgp        = pgpFactory.NextPgpObject();

        // the first object might be a PGP marker packet.
        PgpEncryptedDataList encryptedData;
        if (pgp is PgpEncryptedDataList list)
            encryptedData = list;
        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) continue;
            pubKeyData = pubKeyDataItem;
            break;
        }

        if (privateKey == null) throw new ArgumentException("Secret key for message not found.");

        PgpObjectFactory plainFact;
        using (var clear = pubKeyData.GetDataStream(privateKey))
        {
            plainFact = new PgpObjectFactory(clear);
        }

        var message = plainFact.NextPgpObject();

        switch (message)
        {
            case PgpCompressedData data:
                {
                    var              compressedData       = data;
                    PgpObjectFactory pgpCompressedFactory = null;

                    using (var compDataIn = compressedData.GetDataStream())
                    {
                        pgpCompressedFactory = new PgpObjectFactory(compDataIn);
                    }

                    message = pgpCompressedFactory.NextPgpObject();
                    PgpLiteralData literalData                      = null;
                    if (message is PgpOnePassSignatureList) message = pgpCompressedFactory.NextPgpObject();

                    literalData = (PgpLiteralData) message;
                    using (var unc = literalData.GetInputStream())
                    {
                        output = IoHelper.GetString(unc);
                    }

                    break;
                }
            case PgpLiteralData data:
                {
                    var       literalData = data;
                    using var unc         = literalData.GetInputStream();
                    output = IoHelper.GetString(unc);

                    break;
                }
            case PgpOnePassSignatureList _:
                throw new PgpException("Encrypted message contains a signed message - not literal data.");
            default:
                throw new PgpException("Message is not a simple encrypted file - type unknown.");
        }

        return output;
    }

    private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
        => pgpSec.GetSecretKey(keyId)?.ExtractPrivateKey(pass);
}

IOHelper.cs

using System.IO;
using System.Text;

public static class IoHelper
	{
		public static Stream GetStream(this string s, Encoding encoding = null)
		{
			var stream = new MemoryStream();
			var writer = encoding != null ? new StreamWriter(stream, encoding) : new StreamWriter(stream);
			writer.Write(s);
			writer.Flush();
			stream.Position = 0;
			return stream;
		}

		public static string GetString(Stream inputStream)
		{
			using var reader = new StreamReader(inputStream);
			var       output = reader.ReadToEnd();
			return output;
		}

		public static void WriteStream(Stream inputStream, ref byte[] dataBytes)
		{
			using var outputStream = inputStream;
			outputStream.Write(dataBytes, 0, dataBytes.Length);
		}
	}

That's it! Just make sure _passphrase and _privateKey are set and you're done.