Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / Visual Basic

Cinchoo PGP

Rate me:
Please Sign up or sign in to vote.
4.80/5 (9 votes)
20 Jan 2017CPOL3 min read 30.7K   12   10
Simple PGP wrapper library for .NET

Contents

  1. Introduction
  2. Assumptions
  3. Features
  4. Using the Code
    1. GenerateKey
    2. PGPEncryptFile
    3. PGPEncryptFileAndSign
    4. PGPDecryptFile

1. Introduction

ChoPGP library is an open source PGP library for .NET. It is a simple PGP wrapper library over Bouncy Castle library. It includes methods for PGP encryption, decryption, decrypt and verify, key generation, supports both key store and keys located in files. 100% managed code.

This article explains how to use this library to encrypt, sign, decrypt or verify OpenPGP messages. ChoPGP has been developed to be an Open Source implementation of the OpenPGP standard. OpenPGP is a standard for encrypting, decrypting, signing and verifying data cryptographically. The OpenPGP protocol is commonly used and is said to be very secure.

Fortunately, there is a free OpenPGP implementation available, Bouncy Castle. That is a really full-featured and stable implementation of OpenPGP protocol.

This wrapper library simplifies the use of it to PGP encrypt and decrypt files in no time.

2. Assumptions

This article assumes you have some basic understanding of public key cryptography.

3. Features

Some of the features provided by ChoPGP are:

  • Encrypt/Decrypt files and streams with PGP using a single line of code
  • Synchronous or asynchronous operation
  • Public/Private key ring file generation
  • ASCII Armor, data integrity packets, etc.

Nuget Command: Install-Package ChoPGP

4. Using the Code

This library exposes ChoPGPEncryptDecrypt class to do PGP encryption and decryption process.

C#
public class ChoPGPEncryptDecrypt : IDisposable
{
    //PGP DecryptFile overloads
    public void DecryptFile(string inputFilePath, string outputFilePath, 
           string privateKeyFilePath, string passPhrase);
    public Task DecryptFileAsync(string inputFilePath, string outputFilePath, 
           string privateKeyFilePath, string passPhrase);

    //PGP EncryptFile overloads     
    public void EncryptFile(string inputFilePath, string outputFilePath, 
           string publicKeyFilePath, bool armor, bool withIntegrityCheck);
    public Task EncryptFileAsync(string inputFilePath, string outputFilePath, 
           string publicKeyFilePath, bool armor, bool withIntegrityCheck); 

    //PGP EncryptFileAndSign overloads 
    public void EncryptFileAndSign(string inputFilePath, string outputFilePath, 
           string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor);
    public Task EncryptFileAndSignAsync(string inputFilePath, string outputFilePath, 
           string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor);

    //PGP GenerateKey overloads 
    public void GenerateKey(Stream publicKeyStream, Stream privateKeyStream, 
           string username = null, string password = null, int strength = 1024, int certainty = 8);
    public void GenerateKey(string publicKeyFilePath, string privateKeyFilePath, 
           string username = null, string password = null, int strength = 1024, int certainty = 8);
    public Task GenerateKeyAsync(string publicKeyFilePath, string privateKeyFilePath, 
           string username = null, string password = null, int strength = 1024, int certainty = 8);
}

4.1 GenerateKey

In this section, you will learn how to use the ChoPGP assembly to generate PGP key ring.

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
    pgp.GenerateKey("pub.asc", "pri.asc", "mark@gmail.com", "Test123");

where:

  • publicKeyFilePath - File path to store the PGP public key info. Put this key on your website or at the bottom signature of your email messages. Anyone wishing to contact you in private will have your public PGP to send you encrypted messages
  • privateKeyFilePath - File path to store the PGP private key info. Save your PGP private key in a file on your computer and keep it as confidential as possible.
  • userName - Your email address is recommended for generating your PGP keys.
    Your email address will be included as public information in your public PGP key, so your public key can be easily imported by any third-party PGP software. If you do not supply your email address, your PGP decryption software may be unable to link your email address to your public PGP key, and therefore unable to automatically encrypt/decrypt email messages. As a result, you will have to manually decrypt messages each time you receive a PGP-encrypted message.
  • password - Pick a password to protect your private PGP key.
    This password offers an extra layer of protection in case someone manages to steal your public PGP key.
  • strength - The key strength. Default value is 1024.
  • certainty - Certainty for prime evaluation. Bouncy Castle uses this number to generate random number and checks whether or not they are prime numbers using prime test algorithm. Default value is 8.

4.2 PGPEncryptFile

In this section, you will learn how to use the ChoPGP assembly to encrypt data.

To PGP encrypt a file synchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false);
}

where:

  • inputFilePath - Plain data file path to be encrypted
  • outputFilePath - Output PGP encrypted file path
  • publicKeyFilePath - PGP public key file path
  • armor - True, means a binary data representation as an ASCII-only text. Otherwise, false
  • withIntegrityCheck - True, to perform integrity packet check on input file. Otherwise, false

To PGP encrypt a file asynchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.EncryptFileAsync("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false);
}

4.3 PGPEncryptFileAndSign

To PGP encrypt a file and sign synchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.EncryptFileAndSign("SampleData.txt", "SampleData.PGP", "Pub.asc", "Pri.asc",
        "Test123", true);
}

where:

  • inputFilePath - Plain data file path to be encrypted
  • outputFilePath - Output PGP encrypted file path
  • publicKeyFilePath - PGP public key file path
  • privateKeyFilePath - PGP secret key file path
  • password - PGP secret key password
  • armor - True, means a binary data representation as an ASCII-only text. Otherwise, false

To PGP encrypt a file and sign asynchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.EncryptFileAndSignAsync("SampleData.txt", "SampleData.PGP", "Pub.asc", "Pri.asc",
        "Test123", true);
}

4.4 PGPDecryptFile

In this section, you will learn how to use the ChoPGP assembly to decrypt data.

To PGP decrypt a file synchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123");
}

where:

  • inputFilePath - PGP encrypted data file
  • outputFilePath - Output of decrypted file path
  • privateKeyFilePath - PGP secret key file path
  • password - PGP secret key password

To PGP decrypt a file asynchronously:

C#
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
    pgp.DecryptFileAsync("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123");
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSystem.InvalidCastException Pin
Sen Dog28-Jun-18 18:53
Sen Dog28-Jun-18 18:53 
AnswerRe: System.InvalidCastException Pin
Francisco Amores Torres3-Oct-22 11:45
Francisco Amores Torres3-Oct-22 11:45 
QuestionIs it possible to Decrypt and verify an encrypted and signed file? Pin
Member 1354054824-Nov-17 23:25
Member 1354054824-Nov-17 23:25 
AnswerRe: Is it possible to Decrypt and verify an encrypted and signed file? Pin
Cinchoo26-Nov-17 11:18
Cinchoo26-Nov-17 11:18 
QuestionGenerateKey Pin
FloatingPoint4445-Dec-16 10:20
FloatingPoint4445-Dec-16 10:20 
AnswerRe: GenerateKey Pin
Garth J Lancaster5-Dec-16 19:44
professionalGarth J Lancaster5-Dec-16 19:44 
Questioncrc check not found Pin
glsperoni14-Nov-16 4:24
glsperoni14-Nov-16 4:24 
AnswerRe: crc check not found Pin
Cinchoo14-Nov-16 7:12
Cinchoo14-Nov-16 7:12 
Thanks for reporting. Found the issue and applied fix. Please take the latest from Git or Nuget. Hope this helps.
GeneralRe: crc check not found Pin
glsperoni14-Nov-16 8:58
glsperoni14-Nov-16 8:58 
GeneralRe: crc check not found Pin
Cinchoo14-Nov-16 9:02
Cinchoo14-Nov-16 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.