Пример #1
0
        /// <summary>
        /// A simple routine that opens a key ring file and loads the first available key suitable for encryption.
        /// </summary>
        /// <param name="publicKey">The public key.</param>
        /// <returns>The public key data.</returns>
        private Key.Bcpg.OpenPgp.PgpPublicKey ReadPublicKey(System.IO.Stream publicKey)
        {
            Key.Bcpg.OpenPgp.PgpPublicKeyRingBundle pgpPub = new Key.Bcpg.OpenPgp.PgpPublicKeyRingBundle(
                Key.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(publicKey));

            // Loop through the collection till we find a key suitable for encryption
            foreach (Key.Bcpg.OpenPgp.PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (Key.Bcpg.OpenPgp.PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key.IsEncryptionKey)
                    {
                        // Return the key.
                        return(key);
                    }
                }
            }

            // If no key data has been found.
            throw new ArgumentException("Can't find encryption key in key ring.");
        }