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

PGP in C#

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

private void button1_Click(object sender, EventArgs e)  
{  
   string signature = textBox3.Text;  
   textBox1.Text = PGPKEYS.PGPKeyrings.PrivateKey(signature);  
   textBox2.Text = PGPKEYS.PGPKeyrings.PublicKey(signature);  
}

Keys will be now:

Keys

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

PGP Encryption

private void button2_Click(object sender, EventArgs e)  
{  
    PGPEncryptionText.Text = "";  
    try  
    {  
        PGPEncryptionText.Text = PGPKEYS.PGPKeyrings.PGPEncryption(new PGPKEYS.PGPProperties  
        {  
            private key = textBox1.Text,  
                public key = textBox2.Text,  
                signature = textBox3.Text  
        }, PlainText.Text.ToString());  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show("Wrong Keys Found");  
    }  
}

Find Cipher Text now.

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

 Plain text

private void button3_Click(object sender, EventArgs e)  
{  
    PGPDecryptionText.Text = "";  
    try  
    {  
        PGPDecryptionText.Text = PGPKEYS.PGPKeyrings.PGPDecryption(new PGPKEYS.PGPProperties  
        {  
            private key = textBox1.Text,  
                public key = textBox2.Text,  
                signature = textBox3.Text  
        }, PGPEncryptionText.Text.ToString());  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show("Wrong Keys Found");  
    }  
}

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


Similar Articles