Search code examples
c#.netencryptioncryptographypgp

How to implement PGP Encryption/Decrypttion given the public / private keys


I think I know the basics:

  • Based on public-key cryptography, everyone has a public & private key
  • Sender encrypts with recipient's public key.
  • Recipient decrypts with their own private key.

But this isn't enough to implement this. For instance, what encryption algorithm is used and how is that implemented?

How difficult is it to write a passable pgp encryption/decryption library in .NET given a message and the associated keys?

Where can I find the standard algorithm for this?


Solution

  • OpenPGP is defined in RFC 4880.

    It is a high-level protocol, that uses a broad variety of cryptographic algorithms, some are mandatory (3DES, DSA, Elgamal, SHA-1), others should also be implemented (RSA, AES, SHA-2, compression, ...). It is especially suggested not to implement those basic algorithms on your own to prevent a broad number of side channel and timing attacks, rely on readily existing cryptography libraries instead (GnuTLS, OpenSSL).

    If you want to implement the OpenPGP message format based on those existing algorithms, make sure to read the Security Considerations and Implementation Nits chapters, which have some important information on possible attacks to consider and compatibility issues.

    If your main objective is learning about OpenPGP and cryptography in general, I'd recommend to look into BouncyCastle (which already has a C# and Java implementation), GnuPG (which probably is the OpenPGP implementation with largest user base, and ships with pretty much every Linux distribution, but is also available for Windows) or the OpenPGP.go library (written in well, go). All of them are open source, and probably have interesting problems to solve in their bug trackers (BouncyCastle, GnuPG).