Esempio n. 1
0
        public static Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey ReadPublicKey(System.IO.Stream publickKeyStream)
        {
            var pkstream = Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(publickKeyStream);
            var bundle   = new Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRingBundle(pkstream);

            foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRing keyRing in bundle.GetKeyRings())
            {
                foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key?.IsEncryptionKey ?? false)
                    {
                        return(key);
                    }
                }
            }
            throw new ArgumentException("公钥数据有问题,没有找到公钥");
        }
Esempio n. 2
0
        /**
         * A simple routine that opens a key ring file and loads the first available key
         * suitable for encryption.
         *
         * @param input
         * @return
         * @throws IOException
         * @throws PGPException
         */
        internal static Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey ReadPublicKey(System.IO.Stream input)
        {
            Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRingBundle pgpPub = new Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRingBundle(
                Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(input));

            //
            // we just loop through the collection till we find a key suitable for encryption, in the real
            // world you would probably want to be a bit smarter about this.
            //

            foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }

            throw new System.ArgumentException("Can't find encryption key in key ring.");
        }