Use GnuPG Tools or C# Code for PGP Encryption and Signature

For PGP encryption and signature, usually we use PGP command line tools for test purpose and finally use C# application for production.

Mine is a Net Core 2.0 console application using NuGet package PgpCore,  https://github.com/mattosaurus/PgpCore. PgpCore is based on BouncyCastle.NetCore which supports the standard OpenPGP keys. Here w use the popular GnuPG (GPG) tools to generate OpenPGP key pair.

Download and Install GPG for Windows

Download from https://gpg4win.org/download.html. By default the installation will create two folders (here we use command line tools only):

  • Gpg4Win UI (Kleopatra): C:\Program Files (x86)\Gpg4win
  • Command line tools: C:\Program Files (x86)\GnuPG\bin

Generate a GPG Key Pair

For GnuPG 2.2.1 and above:
    C:\Program Files (x86)\GnuPG\bin> gpg --full-generate-key
For old versions of GnuPG:
    C:\Program Files (x86)\GnuPG\bin> gpg --gen-key

Common GPG Commands

List public keys: gpg --list-key or gpg -k
List private keys: gpg --list-secretkey or gpg -K
List and check signatures:  gpg --check-sigs
To delete a key pair, please delete private first, then delete public:
    Delete a private key: gpg --delete-secret-key keyname
    Delete a public key: gpg --delete-key keyname

Export GPG Public Key File

C:\Program Files (x86)\GnuPG\bin>gpg --export -a -o PGPPublicKey.asc keyname
Please send this public key file to the remote server so that the server can validate our signature.

Export GPG Private Key File (if using C# code)

C:\Program Files (x86)\GnuPG\bin>gpg --export-secret-key -a -o PGPPrivateKey.asc keyname

This private key file will be used by the PgpCore package for file signature.

Please note that we don't need to export the private key if we use GPG command line tools only.

Import, Sign and Trust Remote Server's GPG Public Key (if using GPG command tools)

C:\Program Files (x86)\GnuPG\bin>gpg --import RemoteServersPGPPublicKeyFilePath
C:\Program Files (x86)\GnuPG\bin>gpg --sign-key RemoteServersPGPKeyName
C:\Program Files (x86)\GnuPG\bin>gpg --edit-key RemoteServersPGPKeyName (then type trust and quit)

Please note that we don't need to import remote server's public key if we use C# code for encryption and signature.

Encryption and Signature using GPG command tools

Encrypt and Sign:
    C:\Program Files (x86)\GnuPG\bin>gpg -r RecipientKeyName -a -u SenderKeyName -s -e -o encryptedAndSignedFile sourceFile

Descrypt:
    Decrypt: C:\Program Files (x86)\GnuPG\bin>gpg -d -o destinationFile encryptedFile


Encryption and Signature using C# Code

using PgpCore;

using (PGP pgp = new PGP()) {
    pgp.EncryptFileAndSign(Source, EncryptedAndSignedFile, RemoteServersPGPPublicKeyFilePath, ClientsPGPPrivateKeyFilePath, ClientsPGPPrivateKeyPassPhrase, Armor?, IntegrityCheck?);
}


Comments

  1. I am very grateful you did share your knowledge here. It is an excellent post.Thank you so much for providing such useful content.
    Digital Signature Certificate

    ReplyDelete
  2. Interesting topic for a blog. I have been searching the Internet for fun and came upon your blog Fabulous post. We are the best provider of digital signature certificate

    ReplyDelete
  3. Reading your blog is really Amazing. Thanks for this one and expect one more from you.
    Get class 2 digital signature certificate at a very reasonable price.

    ReplyDelete
  4. I'm happy to see your instructive post here and yes I'm going to attempt your tips a debt of gratitude is in order for sharing… Keep Sharing


    Class 3 digital signature certificate

    ReplyDelete
  5. This was really what i was looking for and i'm glad to come back right here! Thanks for sharing such treasured statistics with us.

    Digital signature Provider in Delhi

    ReplyDelete
  6. I've been searching for information on this subject for some time. I'm glad this one is so extraordinary. Keep up the brilliant work.

    Digital signature provider in Delhi

    ReplyDelete
  7. thankyou for sharing these details we will coordinate with you,doing good job guys
    https://www.99digitalsignature.com

    ReplyDelete


  8. I want to thank you. Because for this great post you really deserve a big thumbs up.


    digital signature provider in Gurgaon

    ReplyDelete
  9. Thank you for all the knowledge that you share, great article. I was extremely interested in the post, and it's impressive.

    printing company sydney

    ReplyDelete
  10. Pretty! This was an extremely wonderful post. I'm thankful that you're sharing useful and valuable information for all us.
    Shadi dresses design

    ReplyDelete
  11. Awesome article on blog commenting, keep the good work Thank you.Digital Signature In Noida

    ReplyDelete
  12. First of all thanks to the blogger for sharing and giving useful information. Buy Digital Signature in Chennai

    ReplyDelete
  13. Awesome article on blog commenting, keep the good work Thank you. Get Digital Signature in Ghaziabad

    ReplyDelete
  14. This post is really insightful; it has helped me understand so many things.Best Digital Signature Services In Faridabad

    ReplyDelete
  15. Thanks for the wonderful post. I've been looking around the web for this type of information and finally found it.Buy Digital Signature in Chennai

    ReplyDelete
  16. Happy to see your blog as it is just what I’ve looking for. I am looking forward to another great article from you.Best Digital Signature Services in Faridabad

    ReplyDelete
  17. Thank you for describing in detail about blog commenting. It is a valuable tool.Apply Digital Signature in Jalandhar

    ReplyDelete
  18. Thank you for this helpful article! I have a question though. How many comments should I post per day.Buy Digital Signature In Siwan

    ReplyDelete
  19. Thank you very much for your valuable information. Digital Signature in Rajkot

    ReplyDelete
  20. Very well constructed about blog commenting. Blog commenting plays a good role in SEO in fact approving the comments is the biggest achievement. Renew Detective Signature Online

    ReplyDelete
  21. Thank you for describing in detail about blog commenting. It is a valuable tool. Digital Signature in Kolkata

    ReplyDelete

Post a Comment

Popular posts from this blog

Errors in Net Core Add-Migration

Confusing Concepts about SFTP: SSH2 vs OpenSSH