Click here to Skip to main content
15,897,187 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 354.5K   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 
//
// SecretKeyRing.cs: 
// 	Class for handling secret key rings.
//
// Author:
//	Daniel Fabian (df@sharpprivacy.net)
//
//
// Version: 0.1.0 (initial release)
//
// Changelog:
//	- 23.02.2003: Created this file.
//	- 01.06.2003: Added this header for the first beta release.
//
// (C) 2003, Daniel Fabian
//
using System;
using System.Windows.Forms;
using SharpPrivacy.OpenPGP;
using SharpPrivacy.OpenPGP.Messages;
using System.Collections;
using System.IO;

namespace SharpPrivacy {
	
	public class SecretKeyRing {
		
		private ArrayList alSecretKeys;
		private bool bIsUpdated = false;
		private string strLoadingPath;
		
		public bool IsUpdated {
			get {
				return bIsUpdated;
			}
		}
		
		public ArrayList SecretKeys {
			get {
				return alSecretKeys;
			}
			set {
				alSecretKeys = value;
			}
		}
		
		public SecretKeyRing() {
			alSecretKeys = new ArrayList();
		}
		
		public void Reload() {
			if (this.strLoadingPath.Length == 0)
				return;
			
			Load(strLoadingPath);
		}
		
		public void Load(string strPath) {
			strLoadingPath = strPath;
			System.IO.StreamReader srInput = new StreamReader(strPath);
			string strKeys = srInput.ReadToEnd();
			srInput.Close();
			
			this.SecretKeys = new ArrayList();
			
			ArmorTypes atType = new ArmorTypes();
			string strKey = Armor.RemoveArmor(strKeys, ref atType, ref strKeys);
			while (strKey.Length > 0) {
				TransportableSecretKey tskKey = new TransportableSecretKey(strKey);
				this.SecretKeys.Add(tskKey);
				
				strKey = Armor.RemoveArmor(strKeys, ref atType, ref strKeys);
			}
			bIsUpdated = false;
		}
		
		public void Save() {
			Save(this.strLoadingPath);
		}
		
		public void Save(string strPath) {
			System.IO.StreamWriter swOutput = new StreamWriter(strPath);
			IEnumerator ieKeys = this.SecretKeys.GetEnumerator();
			while (ieKeys.MoveNext()) {
				if (ieKeys.Current is TransportableSecretKey) {
					try {
						TransportableSecretKey tskKey = (TransportableSecretKey)ieKeys.Current;
						byte[] bKey = tskKey.Generate();
						string strKey = Armor.WrapPrivateKey(bKey);
						swOutput.Write(strKey);
					} catch (Exception e) {
						MessageBox.Show("Error while trying to save a private key: " + e.Message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
				}
			}
			swOutput.Close();
			bIsUpdated = false;
		}
		
		public void Add(TransportableSecretKey tskKey) {
			bIsUpdated = true;
			SecretKeys.Add(tskKey);
		}
		
		public void Delete(TransportableSecretKey tskKey) {
			bIsUpdated = true;
			SecretKeys.Remove(tskKey);
		}
		
		public void Delete(ulong lKeyID) {
			bIsUpdated = true;
			SecretKeys.Remove(Find(lKeyID));
		}
		
		public TransportableSecretKey Find(ulong lKeyID) {
			IEnumerator ieKeys = SecretKeys.GetEnumerator();
			while (ieKeys.MoveNext()) {
				TransportableSecretKey tskKey = (TransportableSecretKey)ieKeys.Current;
				if (tskKey.PrimaryKey.PublicKey.KeyID == lKeyID) {
					return tskKey;
				}
				
				IEnumerator ieSubkeys = tskKey.SubKeys.GetEnumerator();
				while (ieSubkeys.MoveNext()) {
					if (!(ieSubkeys.Current is SecretKeyPacket))
						throw new Exception("Expected a secret key packet, but did not find one.");
					
					SecretKeyPacket skpKey = (SecretKeyPacket)ieSubkeys.Current;
					if (skpKey.PublicKey.KeyID == lKeyID) {
						return tskKey;
					}
				}
			}
			return null;
		}
		
	}
	
}

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