Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C#

SharpPrivacy - OpenPGP for C#

Rate me:
Please Sign up or sign in to vote.
4.92/5 (86 votes)
7 Jun 200314 min read 353.4K   8.4K   227  
SharpPrivacy is an OpenPGP implementation in C#. It can be used to encrypt and sign data, created OpenPGP compatible keys, and a lot more. This article explains how to use the library in your own .NET application or webpage to encrypt, sign, decrypt or verify OpenPGP messages.
//
// This file is part of the source code distribution of SharpPrivacy.
// SharpPrivacy is an Open Source OpenPGP implementation and can be 
// found at http://www.sharpprivacy.net
// It is released under Gnu General Public License and can be used 
// and modified as long as the result is released under GPL too. 
// For a copy of the GPL, please go to www.gnu.org/copyleft/gpl.html 
//
// AddUserID.cs: 
// 	This class is a GUI to add a userid to a key.
//
// Author:
//	Daniel Fabian (df@sharpprivacy.net)
//
//
// Version: 0.1.0 (initial release)
//
// Changelog:
//	- 25.05.2003: Created this file.
//	- 01.06.2003: Added this header for the first beta release.
//
// (C) 2003, Daniel Fabian
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SharpPrivacy.OpenPGP;
using SharpPrivacy.OpenPGP.Messages;
using SharpPrivacy.Cipher;

namespace SharpPrivacy {
	/// <summary>
	/// Summary description for AddUserID.
	/// </summary>
	public class AddUserID : System.Windows.Forms.Form {
		private System.Windows.Forms.TextBox txtEmail;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.Button cmdAdd;
		
		private bool bIsCanceled = true;
		private TransportablePublicKey tpkKey;
		private TransportableSecretKey tskKey;
		
		public bool IsCanceled {
			get {
				return bIsCanceled;
			}
		}
		
		public TransportablePublicKey PublicKeyWithUserID {
			get {
				if (bIsCanceled)
					throw new Exception("The userID has not yet been added to the key!");
				
				return tpkKey;
			}
		}
		
		public TransportableSecretKey SecretKeyWithUserID {
			get {
				if (bIsCanceled)
					throw new Exception("The userID has not yet been added to the key!");
				
				return tskKey;
			}
		}

		public AddUserID(TransportablePublicKey tpkKey, TransportableSecretKey tskKey) {
			this.tpkKey = tpkKey;
			this.tskKey = tskKey;
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.txtEmail = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.cmdAdd = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtEmail
			// 
			this.txtEmail.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(0)));
			this.txtEmail.Location = new System.Drawing.Point(124, 36);
			this.txtEmail.MaxLength = 50;
			this.txtEmail.Name = "txtEmail";
			this.txtEmail.Size = new System.Drawing.Size(216, 21);
			this.txtEmail.TabIndex = 7;
			this.txtEmail.Text = "";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(8, 40);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(108, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Your Email Address";
			// 
			// txtName
			// 
			this.txtName.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(0)));
			this.txtName.Location = new System.Drawing.Point(124, 8);
			this.txtName.MaxLength = 50;
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(216, 21);
			this.txtName.TabIndex = 5;
			this.txtName.Text = "";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 12);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(76, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "Your Name";
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.cmdCancel.Location = new System.Drawing.Point(148, 64);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.Size = new System.Drawing.Size(92, 28);
			this.cmdCancel.TabIndex = 9;
			this.cmdCancel.Text = "Cancel";
			this.cmdCancel.Click += new EventHandler(this.cmdCancel_Click);
			// 
			// cmdAdd
			// 
			this.cmdAdd.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.cmdAdd.Location = new System.Drawing.Point(248, 64);
			this.cmdAdd.Name = "cmdAdd";
			this.cmdAdd.Size = new System.Drawing.Size(92, 28);
			this.cmdAdd.TabIndex = 8;
			this.cmdAdd.Text = "Add";
			this.cmdAdd.Click += new EventHandler(this.cmdAdd_Click);
			// 
			// AddUserID
			// 
			this.AcceptButton = this.cmdAdd;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(344, 97);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cmdCancel,
																		  this.cmdAdd,
																		  this.txtEmail,
																		  this.label3,
																		  this.txtName,
																		  this.label2});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.ShowInTaskbar = false;
			this.Name = "AddUserID";
			this.Text = "Add User ID...";
			this.ResumeLayout(false);

		}
		
		private void cmdCancel_Click(object sender, EventArgs e) {
			this.Close();
		}
		
		private void cmdAdd_Click(object sender, EventArgs e) {
			if (this.txtEmail.Text.Length < 3 || this.txtName.Text.Length < 1) {
				MessageBox.Show("You must enter your full name as well as your email address!", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
				return;
			}
			
			CertifiedUserID cuiUID = new CertifiedUserID();
			UserIDPacket uipUID = new UserIDPacket();
			uipUID.UserID = this.txtName.Text.Trim() + " <" + this.txtEmail.Text.Trim() + ">";
			cuiUID.UserID = uipUID;
			
			QueryPassphrase qpPassphrase = new QueryPassphrase();
			qpPassphrase.ShowMyDialog(tskKey);
			string strPassphrase = qpPassphrase.Passphrase;
			
			SecretKeyPacket skpSignatureKey = tskKey.FindKey(AsymActions.Sign);
			SignaturePacket spSelfSig = new SignaturePacket();
			spSelfSig.Version = SignaturePacketVersionNumbers.v4;
			spSelfSig.HashAlgorithm = HashAlgorithms.SHA1;
			spSelfSig.KeyID = skpSignatureKey.PublicKey.KeyID;
			spSelfSig.TimeCreated = DateTime.Now;
			cuiUID.Certificates = new System.Collections.ArrayList();
			cuiUID.Sign(spSelfSig, skpSignatureKey, strPassphrase, this.tpkKey.PrimaryKey);
			
			tpkKey.Certifications.Add(cuiUID);
			tskKey.UserIDs.Add(uipUID);
			
			bIsCanceled = false;
			this.Close();
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Austria Austria
My name is Daniel and I am from Austria.
When I was in High School, all I wanted to do was programming. After finishing High School, I joined a company for which I wrote a project management utility in Visual Basic 6. It was then that I realised that programming all day long was nothing I wanted to do for the rest of my life.

I quit the job and started to study computer and media security at polytechnical university in Hagenberg/Austria.

As of now, I'm still studying. I still like to program, as long as I can do something else too. Recently I switched my favorite programming language to c#. Together with a friend from university, we started a project where we implement OpenPGP (RFC2440) in c#.

Comments and Discussions