PGP (Pretty Good Privacy) Using C#

Introduction

PGP stands for “Pretty Good Privacy,” and it’s most often used for sending encrypted messages between two people. Pretty Good Privacy (PGP) is a data encryption and decryption computer program that provides cryptographic privacy and authentication for data communication. PGP is often used for signing, encrypting, and decrypting texts, emails, files, directories, and whole disk partitions and to increase the security of e-mail communications.

I would recommend that you please find about cryptography before starting PGP works.

What is cryptography

Cryptography is the science of using mathematics to encrypt and decrypt data. Cryptography enables you to store sensitive information or transmit it across insecure networks (like the Internet) so that it cannot be read by anyone except the intended recipient.

How does Cryptography work?

  • Plain text --> Message Encrypted Security Algorithms [Cryptography] --> Cipher text.
  • Cipher text --> [Cryptography] Message Encrypted Security Algorithms --> Plain text.

PGP Conceptual view
PGP Conceptual view

Flow of Control


Flow of Control

The following series of mechanical steps explains how to achieve PGP process in C#.NET.

Step 1: Please enter the Signature to get the valid Keys which is private and public keys to encrypt plain text to PGP Encrypted text.

 PGP Encrypted text

Code Snippet

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.    string signature = textBox3.Text;  
  4.    textBox1.Text = PGPKEYS.PGPKeyrings.PrivateKey(signature);  
  5.    textBox2.Text = PGPKEYS.PGPKeyrings.PublicKey(signature);  
  6.   
  7. }  
Keys will be now:

Keys

Step 2: PGP Encryption has happened with a combination of Signature + Private KEY + Public Key:

PGP Encryption
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     PGPEncryptionText.Text = "";  
  4.     try  
  5.     {  
  6.         PGPEncryptionText.Text = PGPKEYS.PGPKeyrings.PGPEncryption(new PGPKEYS.PGPProperties  
  7.         {  
  8.             private key = textBox1.Text,  
  9.                 public key = textBox2.Text,  
  10.                 signature = textBox3.Text  
  11.         }, PlainText.Text.ToString());  
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.         MessageBox.Show("Wrong Keys Found");  
  16.     }  
  17. }  
Find Cipher Text now.

Step 3:
In this we can get plain text from cipher text.

 Plain text
  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.     PGPDecryptionText.Text = "";  
  4.     try  
  5.     {  
  6.         PGPDecryptionText.Text = PGPKEYS.PGPKeyrings.PGPDecryption(new PGPKEYS.PGPProperties  
  7.         {  
  8.             private key = textBox1.Text,  
  9.                 public key = textBox2.Text,  
  10.                 signature = textBox3.Text  
  11.         }, PGPEncryptionText.Text.ToString());  
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.         MessageBox.Show("Wrong Keys Found");  
  16.     }  
  17. }  
Step 4: Assertion.

Imagine I changed Sign p.m@ruthi to p.m@ruthipall, but plain text will notbe produced if the keys are same. Your Signature Keys must be same, then only will Decryption happen.

Keys must be same

Text with Actual Sign.

Decryption checks Signature with keys.

Decryption

References: