C# Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript



C# Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML to XML
HTTP
IMAP
Encryption
MHT / HTML Email
MIME
RSA Encryption
S/MIME
Socket
Spider
Tar Archive
Upload
XML
XMP
Zip Compression


More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

Validate S/MIME Signature in C#

Download C# Project Files and Source Code

This C# S/MIME example program shows how to verify (validate) a signed MIME message. The MIME message can be a signed-data message (x-pkcs7-mime) or one with a detached (x-pkcs7-signature) signature. The process of verifying the signature unwraps the MIME message to its original unsigned state, where the data can easily be accessed.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace VerifySignature
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button VerifySig;
		private System.Windows.Forms.ListBox listBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.VerifySig = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// VerifySig
			// 
			this.VerifySig.Location = new System.Drawing.Point(24, 24);
			this.VerifySig.Name = "VerifySig";
			this.VerifySig.Size = new System.Drawing.Size(112, 32);
			this.VerifySig.TabIndex = 0;
			this.VerifySig.Text = "Verify Signature";
			this.VerifySig.Click += new System.EventHandler(this.VerifySig_Click);
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(24, 72);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(384, 173);
			this.listBox1.TabIndex = 1;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(432, 270);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listBox1,
																		  this.VerifySig});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		// Loads a signed message, unwraps the security envelopes, and determines
		// if the signature is valid.  The signed MIME message can be an opaque
		// signed-data message, or it can be one with a detached PKCS7 signature.
		private void VerifySig_Click(object sender, System.EventArgs e)
		{
			Chilkat.Mime mime = new Chilkat.Mime();

			// Get a 30-day trial code from http://www.chilkatsoft.com/register30.asp
			mime.UnlockComponent("UnlockCode");
			mime.LoadMimeFile("signed.txt");

			// Determine if this is a signed message or not.
			if (mime.IsSigned())
			{
				listBox1.Items.Add("Message is signed");
			}
			else
			{
				listBox1.Items.Add("Message is not signed");
			}

			// Unwrap the security.  This is the signature validation.
			// The results are stored within the Mime object and can be queried
			// afterwards.
			bool signatureValid = mime.UnwrapSecurity();
			if (signatureValid)
			{
				listBox1.Items.Add("Signed message has been validated");
			}
			else
			{
				listBox1.Items.Add("Signed message not validated");
			}

			// Get the first signing certificate.
			Chilkat.Cert cert;
			cert = mime.GetSignerCert(0);
			listBox1.Items.Add("Signed By: " + cert.SubjectDN);

			// How many signatures are there?
			listBox1.Items.Add("Num Signatures: " + mime.NumSignerCerts);
			
			// This example uses a MIME message that included a GIF attachment.
			// Save the GIF to a file.
			Chilkat.Mime gifAttachment;
			gifAttachment = mime.GetPart(1);
			gifAttachment.SaveBody("dude.gif");
		}
	}
}

 

 

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.

Email Component · XML Parser