Wednesday, 22 May 2013

Create windows Utility for GNUPG



Download and Install GNUPG 

Before we write a line of code, we need to create a key for encryption/decryption.

PGP use Public Key Cryptography

Create private key using GNU command 

 gpg --gen-key


































Receipientid : Test@company.com

Passphase : Test@123

Remember Email Id and Passphase as these are required during encryption and decryption





















Code for Encryption : 

   
                string appPath = @"C:\Program Files\GNU\GnuPG\pub\gpg2.exe";
                string dataToEncrypt = "DataToEncrypt";
                GnuPG gpg = new GnuPG();
                gpg.BinaryPath = Path.GetDirectoryName(appPath);
                gpg.Recipient = "ReceipientId";// EmailId
                //  Perform encryption

                // convert string to stream
                byte[] byteArray = Encoding.UTF8.GetBytes(dataToEncrypt);
                MemoryStream sourceStream = new MemoryStream(byteArray);
                MemoryStream encryptedStream = new MemoryStream();
                gpg.Encrypt(sourceStream, encryptedStream);
                encryptedStream.Position = 0;
                StreamReader reader = new StreamReader(encryptedStream);
                string text = reader.ReadToEnd();
                textBox2.Text = text; 

Code For Decryption :  

                GnuPG gpg = new GnuPG();

                string dataToDecrypt = "dataToDecrypt";

                string appPath = @"C:\Program Files\GNU\GnuPG\pub\gpg2.exe";
                gpg.BinaryPath = Path.GetDirectoryName(appPath);

                byte[] byteArray = Encoding.UTF8.GetBytes(dataToDecrypt);
                MemoryStream sourceStream = new MemoryStream(byteArray);
                MemoryStream decryptedStream = new MemoryStream();
                //  Decrypt
                gpg.Passphrase = "passphase";// Passphase
                gpg.Decrypt(sourceStream, decryptedStream);

                decryptedStream.Position = 0;
                StreamReader reader = new StreamReader(decryptedStream);
                string text = reader.ReadToEnd();
                textBox2.Text = text; 

DownLoad From MSDN

No comments:

Post a Comment