ExtractPrivateKey() public method

Extract a PgpPrivateKey from this secret key's encrypted contents.
public ExtractPrivateKey ( char passPhrase ) : PgpPrivateKey
passPhrase char
return PgpPrivateKey
示例#1
0
        /// <summary>
        /// Gets the private key from the specified secret key.
        /// </summary>
        /// <returns>The private key.</returns>
        /// <param name="key">The secret key.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="key"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The user chose to cancel the password prompt.
        /// </exception>
        /// <exception cref="System.UnauthorizedAccessException">
        /// 3 bad attempts were made to unlock the secret key.
        /// </exception>
        protected PgpPrivateKey GetPrivateKey(PgpSecretKey key)
        {
            int attempts = 0;
            string password;

            if (key == null)
                throw new ArgumentNullException ("key");

            do {
                if ((password = GetPasswordForKey (key)) == null)
                    throw new OperationCanceledException ();

                try {
                    return key.ExtractPrivateKey (password.ToCharArray ());
                } catch (Exception ex) {
                    Debug.WriteLine ("Failed to extract secret key: {0}", ex);
                }

                attempts++;
            } while (attempts < 3);

            throw new UnauthorizedAccessException ();
        }
		/// <summary>
		/// Gets the private key from the specified secret key.
		/// </summary>
		/// <remarks>
		/// Gets the private key from the specified secret key.
		/// </remarks>
		/// <returns>The private key.</returns>
		/// <param name="key">The secret key.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="key"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The user chose to cancel the password prompt.
		/// </exception>
		/// <exception cref="System.UnauthorizedAccessException">
		/// 3 bad attempts were made to unlock the secret key.
		/// </exception>
		protected PgpPrivateKey GetPrivateKey (PgpSecretKey key)
		{
			int attempts = 0;
			string password;

			if (key == null)
				throw new ArgumentNullException ("key");

			do {
				if ((password = GetPasswordForKey (key)) == null)
					throw new OperationCanceledException ();

				try {
					var privateKey = key.ExtractPrivateKey (password.ToCharArray ());

					// Note: the private key will be null if the private key is empty.
					if (privateKey == null)
						break;

					return privateKey;
				} catch (Exception ex) {
#if DEBUG
					Debug.WriteLine (string.Format ("Failed to extract secret key: {0}", ex));
#endif
				}

				attempts++;
			} while (attempts < 3);

			throw new UnauthorizedAccessException ();
		}
		char[] PasswordCallback(PgpSecretKey masterKey, PgpSecretKey key)
		{
			if (PassphraseCache.ContainsKey(key.PublicKey.KeyId))
				return PassphraseCache[key.KeyId];

			// Loop until correct password or user selects cancel
			do
			{
				var passphraseDialog = new FormPassphrase(masterKey, key);
				var result = passphraseDialog.ShowDialog();
				if (result == DialogResult.Cancel)
					return null;

				var pass = passphraseDialog.textBoxPassphrase.Text.ToCharArray();

				try
				{
					key.ExtractPrivateKey(pass);
					PassphraseCache[key.PublicKey.KeyId] = pass;

					_lastPasswordLookupKey = key.PublicKey.KeyId;
					return pass;
				}
				catch (Exception)
				{
				}
			}
			while (true);
		}