예제 #1
0
        /**
         * Search a secret key ring collection for a secret key corresponding to keyID if it
         * exists.
         *
         * @param pgpSec a secret key ring collection.
         * @param keyID keyID we want.
         * @param pass passphrase to decrypt secret key with.
         * @return
         * @throws PGPException
         * @throws NoSuchProviderException
         */
        internal static Org.BouncyCastle.Bcpg.OpenPgp.PgpPrivateKey FindSecretKey(Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
        {
            Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
예제 #2
0
        /**
         * A simple routine that opens a key ring file and loads the first available key
         * suitable for signature generation.
         *
         * @param input stream to read the secret key ring collection from.
         * @return a secret key.
         * @throws IOException on a problem with using the input stream.
         * @throws PGPException if there is an issue parsing the input stream.
         */
        internal static Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey ReadSecretKey(System.IO.Stream input)
        {
            Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle pgpSec = new Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle(
                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.PgpSecretKeyRing keyRing in pgpSec.GetKeyRings())
            {
                foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey key in keyRing.GetSecretKeys())
                {
                    if (key.IsSigningKey)
                    {
                        return(key);
                    }
                }
            }

            throw new System.ArgumentException("Can't find signing key in key ring.");
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="publickKeyStream">对应gpg4win导出公钥的*.asc公钥文件(导出工具分为导出/)</param>
        /// <param name="privateKeyStream">对应gpg4win导出私钥的*.gpg私钥文件(被密码加密)</param>
        /// <param name="privateKeyPwd">*.gpg私钥文件的密码</param>
        public GpgKeyPair(System.IO.Stream publickKeyStream, System.IO.Stream privateKeyStream, string privateKeyPwd)
        {
            this.PublickKey = ReadPublicKey(publickKeyStream);

            var pkstream = Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(privateKeyStream);
            var bundle   = new Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle(pkstream);

            foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRing keyRing in bundle.GetKeyRings())
            {
                foreach (Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey key in keyRing.GetSecretKeys())
                {
                    if (key?.IsSigningKey ?? false)
                    {
                        this.PrivateKeySecreted = key;
                    }
                }
            }
            if (this.PrivateKeySecreted == null)
            {
                throw new ArgumentException("私钥数据有问题,没有找到公钥");
            }

            this.PrivateKey = this.PrivateKeySecreted.ExtractPrivateKey(privateKeyPwd.ToCharArray());
        }