title: Identify the recipient(s) KeyId from PGP encrypted data using the BouncyCastle C# library
body: <p>I have developed a library to perform PGP signing/encryption and decryption/validation of files against one or more recipients. This part works great and works with large files using streams nicely and efficiently.</p>
  <p>Part of the PGP Message Exchange Formats specification (<a href="https://www.rfc-editor.org/rfc/rfc1991#section-6.7" rel="nofollow noreferrer">RFC 1991</a>) states the following:</p>
  <blockquote>
  <p>...</p>
  <p><strong>6.7 User ID Packet</strong></p>
  <p>Purpose.  A user ID packet identifies a user and is associated with a
  public or private key.</p>
  <p>Definition.  A user ID packet is the concatenation of the following
  fields:</p>
  <p>(a) packet structure field (2 bytes);</p>
  <p>(b) User ID string.</p>
  <p>The User ID string may be any string of printable ASCII characters.
  However, since the purpose of this packet is to uniquely identify an
  individual, the usual practice is for the User ID string to consist of
  the user's name followed by an e-mail address for that user, the
  latter enclosed in angle brackets.</p>
  <p>...</p>
  </blockquote>
  <p>The application I am creating will need to attempt to identify the appropriate key for decrypting the files automatically so that I have as little user intervention as possible. If the key can not be identified (for example, if the recipient(s) are hidden) the application will prompt for the selection of the correct key. I am trying to make it as streamlined as possible.</p>
  <p>The RFC suggests the packet is not part of the encrypted data which makes sense. PGP makes it easy to try and identify who encrypted the data. This is evident when you try and decrypt a file using Kleopatra when it has the relevant keys added to its key database. In this instance, it will prompt for the password protecting the secret key.</p>
  <p>My specific question is:</p>
  <p><em>How do I use the C# BouncyCastle library to read which recipients the encrypted data was intended to? In otherwords, which private key to use for decryption?</em></p>
  <p>I have tried to find examples using the <a href="https://github.com/bcgit/bc-csharp/tree/master/crypto/test/src/openpgp" rel="nofollow noreferrer">Bouncy Castle GitHub repository</a> and couldn't see any that demonstrated this particular problem. I also looked at as many google search results for this question to no avail.</p>