GetSecretKey() public method

Return the PGP secret key associated with the given key id.
public GetSecretKey ( long keyId ) : PgpSecretKey
keyId long The ID of the secret key to return.
return PgpSecretKey
        //private static string Password = ConfigurationManager.AppSettings["Password"];
        //public static string PublicKey = ConfigurationManager.AppSettings["PublicKey"];
        //public static string PublicKeyPath = IoHelper.BasePath + @"\" + PublicKey;
        //// note: this should be changed if the private key is not located in the current executables path
        //private static string PrivateKeyOnly = ConfigurationManager.AppSettings["PrivateKeyOnly"];
        //private static string PrivateKeyOnlyPath = IoHelper.BasePath + @"\" + PrivateKeyOnly;

        // The majority of the functionality encrypting/decrypting came from this answer: http://stackoverflow.com/a/10210465

        private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
        {
            PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);
            if (pgpSecKey == null)
            {
                return null;
            }

            return pgpSecKey.ExtractPrivateKey(pass);
        }
Example #2
0
        private static Tuple <PgpPublicKeyEncryptedData, PgpObjectFactory> Decrypt(PgpEncryptedDataList pgpEncryptedDatalist, Stream privateKeyStream, string praKeyPwd)
        {
            var pkstream = Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(privateKeyStream);
            var bundle   = new Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle(pkstream);

            foreach (PgpPublicKeyEncryptedData encryptedData in pgpEncryptedDatalist.GetEncryptedDataObjects())
            {
                var privateKey = bundle.GetSecretKey(encryptedData.KeyId)?.ExtractPrivateKey(praKeyPwd.ToCharArray());
                if (privateKey == null)
                {
                    continue;
                }
                using (var tmp = encryptedData.GetDataStream(privateKey))
                {
                    return(Tuple.Create(encryptedData, new PgpObjectFactory(tmp)));
                }
            }
            throw new ArgumentException("未能正常读取到加密文件内容,请检查文件及密钥");
        }
Example #3
0
        private static PgpPrivateKey FindSecretKey(
            Stream keyIn,
            long keyId,
            char[] pass)
        {
            PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
                PgpUtilities.GetDecoderStream(keyIn));

            PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);

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

            return pgpSecKey.ExtractPrivateKey(pass);
        }