BouncyCastle C # PublicKey is different from GnuPG

I am trying to get the public PGP key from a GnuPG generated keychain using the BouncyCastle C # library. I got it half time using the following code. The problem is that the public key it issues is about half the length of the real one, and the last few bytes are also different. I'm just trying to get the real key.

UPDATE . Something interesting to note that there was only one public key in the keyring I created, but I am getting two from bouncycastle. I also found that if you insert the second key in the first few characters from the end, it produces an almost original key. Only a few characters at the end are different. So why are there two keys and why is this happening? What am I missing?

Is the dongle GnuPG compatible?

Also note that the code given here only gets the last key. Now I add them to the list.

Here is my code:

public static string ReadKey(string pubkeyFile)
{
    string theKey;
    Stream fs = File.OpenRead(pubkeyFile);

    //
    // Read the public key rings
    //
    PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));
    fs.Close();

    foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
    {
        pgpPub.GetPublicKey();

        foreach (PgpPublicKey pgpKey in pgpPub.GetPublicKeys())
        {
            //AsymmetricKeyParameter pubKey = pgpKey.GetKey();
            //SubjectPublicKeyInfo k = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey);
            //byte[] keyData = k.ToAsn1Object().GetDerEncoded();
            //byte[] keyData = k.GetEncoded();
            byte[] keyData = pgpKey.GetEncoded();
            theKey = Convert.ToBase64String(keyData);
        }
    }
    return theKey;
}

      

Here is the actual public key:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.20 (MingW32)

mQENBFP2z94BCADKfQT9DGHm4y/VEAYGL7XiUavbv+aE7D2OZ2jCbwnx7BYzQBu8
63v5qYe7oH0oBOiw67VaQSjS58fSBAE8vlTkKjvRAscHJNUX9qZrQoRtpMSnrK7N
Ca9N2ptvof7ykF1TAgbxDSSnhwysVznYc7mx76BO6Qx8KChqEd0Yp3w2U89YkUqN
qdzjB7ZIhj5hDM9f4eyHwsz0uZgyqLKK5VgNj6dHVmOHZt6+RIydRC2lGfocWKM8
loPkk6GiSX9sdEm6GXxi7gV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1Nr4VHOxKbKmmLu8YhdYRk2KF9fPI2Qj376C69W90R/LHByCrcCg7
xmqAvO9a8Eac7Rk+Fc+5NKVw9D1rP7MqZGgIQQoh8jLiI2MblvEEahwNxA9AYs8U
PpMD0pdo93wxXIYuKc40MF4yFL9LfpPxDnf373dbYQjk3pNThQ5RagIgLNEhRow4
5x/1wcO6FMx5a/irQXnJ2o1XYRvznBeCsoyOAYbikA==
=r3Qj
-----END PGP PUBLIC KEY BLOCK-----

      

Here is the NEW KEY generated by BouncyCastle (sorry, can't format):

mQENBFP2z94BCADKfQT9DGHm4y / VEAYGL7XiUavbv + aE7D2OZ2jCbwnx7BYzQBu863v5qYe7oH0oBOiw67VaQSjS58fSBAE8vlTkKjvRAscHJNUX9qZrQoRtpMSnrK7NCa9N2ptvof7ykF1TAgbxDSSnhwysVznYc7mx76BO6Qx8KChqEd0Yp3w2U89YkUqNqdzjB7ZIhj5hDM9f4eyHwsz0uZgyqLKK5VgNj6dHVmOHZt6 + RIydRC2lGfocWKM8loPkk6GiSX9sdEm6GXxi7gV / Q3Jr0G099AFg57cWyj1eO6NC8YHLgBHwrB1IkFwiJ0x5IHZssy / XleQ1i1izc3ntWiiH24powuAhABEBAAG0H3N5bmFwczMgPHN5bmFwczNAc2FmZS1tYWlsLm5ldD6JATkEEwECACMFAlP2z94CGwMHCwkIBwMCAQYVCAIJCgsEFgIDAQIeAQIXgAAKCRD944Hz1MHUYP + AB / 4roauazFR5lDrJBFB0YoH4VFKM28IJtuy6OThg3cxhqI / N74sZoxtB90QQk4lcshdpwD7CIe9TCKrnhWokIdm4N91mTGDmW7iIeM3kcPp3mj9 / 7hGOetESuz9JxhBQ0aHAXYk5LdHeDKyRg1KL3JvWrJ27fioDoLLpxxdudSd2nJLhi0hAaHKnkLVl98r37AwxTigGj + J2rN47D +UepJraf8jeeZrY / RfwKJVleF1KYPIgduwX3jdiABrI4EsZP / CdbEWTvmmkFFtD4clSMsmqaXPTa3VeaL / saScBPL93tDsjqCddcgW28hsnhzoJ7TM78j2zNcTXZjK8 / tNCDnShsAIAA7kBDQRT9s / eAQgA5ggDCKTqedfMsFJRuT83uR3aYBokBdul88MVLd5jwDFwVEO76kTA / 8zWoD1yrYaf6eLdI0aBcBcBfuwqka8ZL9ka4 + Tqgw + yahE2OZ30WdB734uHD5H9oEte0lSOC0hUP7 / 7CEjTYiNwaP8b6Tniv gH9PO4 + ARHvL4xbss39PBwoJSqC + lgKfOflnNr4 / uxGx + tWJaCijboKt + AMrjYdUGJpeiyNOG9ikdG / 6l01vFKi764eGBmjqZPNJew6QZioBrV5 / CRX1PVd7BgrHEp38w7yo6qZrSl4iRcTtqcjwjeY0SnxjDksgH2dtTSKwSREf1r7iAJbPE4pJPsCkSl66QARAQABiQEfBBgBAgAJBQJT9s / eAhsMAAoJEP3jgfPUwdRgwxAH / jgPYYG9xS2xLXDb5FJfcE7S2sa / ZwLAaMKHzRCEx / eoVcOIunpvUY4Nvf / uCa5w4Ym7obBa / xfLiqI / omRhcNjaVx2COskvgS27lY / IzpGAMhqt + BzDVl7ktTa + FRzsSmyppi7vGIXWEZNihfXzyNkI9 ++ guvVvdEfyxwcgq3AoO8ZqgLzvWvBGnO0ZPhXPuTSlcPQ9az + zKmRoCEEKTAGIfGyf4d6d9az + zKmRoCEEKIFLGyFxD6d9az + zKmRoCEEKIfLGaFxD6d9az93W2EI5N6TU4UOUWoCICzRIUaMOOcf9cHDuhTMeWv4q0F5ydqNV2Eb85wXgrKMjgGG4pCwAgAD

Still not the same. Place both keys in notepad and find "tNCDnS", then they change.

Thank you for joining me. I've gotten enough with this that I don't want to give up all the code for some crappy encryption.

+3


source to share


1 answer


You may have been a victim of BouncyCastle, a somewhat unusual use of the term "keyring". The BouncyCastle uses three related terms here:

PgpPublicKey . This is the publicly available portion of a single mathematical key in PGP format. It does not contain any connections.

PgpPublicKeyRing . This is a cryptographic key with its subsections. Other programs commonly refer to this as a PGP key.

PgpPublicKeyRingBundle . This is any number of PgpPublicKeyRings (as defined in BouncyCastle). Other programs usually refer to this as a public key ring (no package).

You have iterated through the keychain to find all the PgpPublicKey objects. You have decoded them and returned the last and only the last one as a string. If you insert input and output lines into pgpdump.net (you need to add BEGIN and END headers to the output), you will see that you will lose a subsection in this procedure. Use the PgpPublicKeyRing GetEncoded () method instead. This should save all the information, and also easier :-).



Also, PGP Radix encoding is a little more than just Base64 encoding. Many programs and libraries, including BC, ignore the presence of PGP headers and footers, as well as the CRC checksum, which includes Radix encoding as opposed to plain Base64. However, the GPG is stricter and doesn't seem to accept ASCII armor without a CRC checksum. Therefore, you need to use a binary key (which is the [] keyData byte in your problem code) or create proper ASCII PGP armor. I edited the following code based on your code to implement the latter using BC ArmoredOutputStream:

public static string ReadKey(string pubkeyFile)
{
    Stream fs = File.OpenRead(pubkeyFile);

    //
    // Read the public key rings
    //
    PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));
    fs.Close();

    foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
    {
            using (MemoryStream ms = new MemoryStream())
            {
                using (ArmoredOutputStream aos = new ArmoredOutputStream(ms))
                    pgpPub.Encode(aos);
                return System.Text.Encoding.ASCII.GetString(ms.ToArray());
            }
    }
    return null;
}

      

EDIT . Below is the complete program without external dependencies. It works for me i.e. Gives out "The same!" Please run this exact program and check if it outputs "Same!" or "Difference!" If it says "Same!", You can use it to fix your own code:

using System;
using System.IO;
using Org.BouncyCastle.Bcpg.OpenPgp;

class Program
{
    private const string PGP_OVERFLOW_KEYBODY =
    "mQENBFP2z94BCADKfQT9DGHm4y/VEAYGL7XiUavbv+aE7D2OZ2jCbwnx7BYzQBu8\r\n" +
    "63v5qYe7oH0oBOiw67VaQSjS58fSBAE8vlTkKjvRAscHJNUX9qZrQoRtpMSnrK7N\r\n" +
    "Ca9N2ptvof7ykF1TAgbxDSSnhwysVznYc7mx76BO6Qx8KChqEd0Yp3w2U89YkUqN\r\n" +
    "qdzjB7ZIhj5hDM9f4eyHwsz0uZgyqLKK5VgNj6dHVmOHZt6+RIydRC2lGfocWKM8\r\n" +
    "loPkk6GiSX9sdEm6GXxi7gV/Q3Jr0G099AFg57cWyj1eO6NC8YHLgBHwrB1IkFwi\r\n" +
    "J0x5IHZssy/XleQ1i1izc3ntWiiH24powuAhABEBAAG0H3N5bmFwczMgPHN5bmFw\r\n" +
    "czNAc2FmZS1tYWlsLm5ldD6JATkEEwECACMFAlP2z94CGwMHCwkIBwMCAQYVCAIJ\r\n" +
    "CgsEFgIDAQIeAQIXgAAKCRD944Hz1MHUYP+AB/4roauazFR5lDrJBFB0YoH4VFKM\r\n" +
    "28IJtuy6OThg3cxhqI/N74sZoxtB90QQk4lcshdpwD7CIe9TCKrnhWokIdm4N91m\r\n" +
    "TGDmW7iIeM3kcPp3mj9/7hGOetESuz9JxhBQ0aHAXYk5LdHeDKyRg1KL3JvWrJ27\r\n" +
    "fioDoLLpxxdudSd2nJLhi0hAaHKnkLVl98r37AwxTigGj+J2rN47D+UepJraf8je\r\n" +
    "eZrY/RfwKJVleF1KYPIgduwX3jdiABrI4EsZP/CdbEWTvmmkFFtD4clSMsmqaXPT\r\n" +
    "a3VeaL/saScBPL93tDsjqCddcgW28hsnhzoJ7TM78j2zNcTXZjK8/tNCDnShuQEN\r\n" +
    "BFP2z94BCADmCAMIpOp518ywUlG5Pze5HdpgGiQF26XzwxUt3mPAMXBUQ7vqRMD/\r\n" +
    "zNagPXKthp/p4t0jRoFwFwF+7CqRrxkv2Rrj5OqDD7JqETY5nfRZ0Hvfi4cPkf2g\r\n" +
    "S17SVI4LSFQ/v/sISNNiI3Bo/xvpOeK+Af087j4BEe8vjFuyzf08HCglKoL6WAp8\r\n" +
    "5+Wc2vj+7EbH61YloKKNugq34AyuNh1QYml6LI04b2KR0b/qXTW8UqLvrh4YGaOp\r\n" +
    "k80l7DpBmKgGtXn8JFfU9V3sGCscSnfzDvKjqpmtKXiJFxO2pyPCN5jRKfGMOSyA\r\n" +
    "fZ21NIrBJER/WvuIAls8Tikk+wKRKXrpABEBAAGJAR8EGAECAAkFAlP2z94CGwwA\r\n" +
    "CgkQ/eOB89TB1GDDEAf+OA9hgb3FLbEtcNvkUl9wTtLaxr9nAsBowofNEITH96hV\r\n" +
    "w4i6em9Rjg29/+4JrnDhibuhsFr/F8uKoj+iZGFw2NpXHYI6yS+BLbuVj8jOkYAy\r\n" +
    "Gq34HMNWXuS1Nr4VHOxKbKmmLu8YhdYRk2KF9fPI2Qj376C69W90R/LHByCrcCg7\r\n" +
    "xmqAvO9a8Eac7Rk+Fc+5NKVw9D1rP7MqZGgIQQoh8jLiI2MblvEEahwNxA9AYs8U\r\n" +
    "PpMD0pdo93wxXIYuKc40MF4yFL9LfpPxDnf373dbYQjk3pNThQ5RagIgLNEhRow4\r\n" +
    "5x/1wcO6FMx5a/irQXnJ2o1XYRvznBeCsoyOAYbikA==";

    static void Main(string[] args)
    {
        string parsedKey = ReadKeyDirectly(PGP_OVERFLOW_KEYBODY);
        if (parsedKey != PGP_OVERFLOW_KEYBODY.Replace("\r\n",""))
            Console.WriteLine("Difference!");
        else
            Console.WriteLine("Same!");
    }

    public static string ReadKeyDirectly(string stringKeyData)
    {
        Stream fs = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(stringKeyData));
        fs.Seek(0, SeekOrigin.Begin);

        PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));

        foreach (PgpPublicKeyRing pubRing in pubRings.GetKeyRings())
            return Convert.ToBase64String(pubRing.GetEncoded());
        return null;
    }
}

      

+2


source







All Articles