Пример #1
0
        /// <summary>
        /// Return a new bundle containing the contents of the passed in bundle with
        /// the passed in public key ring removed.
        /// </summary>
        /// <param name="bundle">The <c>PgpPublicKeyRingBundle</c> the key ring is to be removed from.</param>
        /// <param name="publicKeyRing">The key ring to be removed.</param>
        /// <returns>A new <c>PgpPublicKeyRingBundle</c> not containing the passed in key ring.</returns>
        /// <exception cref="ArgumentException">If the keyId for the passed in key ring is not present.</exception>
        public static PgpPublicKeyRingBundle RemovePublicKeyRing(
            PgpPublicKeyRingBundle bundle,
            PgpPublicKeyRing publicKeyRing)
        {
            long key = publicKeyRing.GetPublicKey().KeyId;

            if (!bundle.pubRings.Contains(key))
            {
                throw new ArgumentException("Bundle does not contain a key with a keyId for the passed in ring.");
            }

            IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings);
            IList       newOrder    = Platform.CreateArrayList(bundle.order);

            newPubRings.Remove(key);
            newOrder.Remove(key);

            return(new PgpPublicKeyRingBundle(newPubRings, newOrder));
        }
Пример #2
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.");
        }