Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

November 30, 2005

ASP.NET Example to Locate a Digital Certificate w/ Private Key and use it for Signing

This ASP.NET example searches the Current User certificate store and the Local Machine certificate store (from the ASPNET account) for a certificate matching an email address. If found, it checks to see if the private key is available, and if so, it tries to create a digital signature using the certificate and private key:

<font size=2 face=courier>
		    // Find a certificate matching an email address.
		    // Check to see if it has a private key available, and
		    // if it can be used to create a digital signature.
		    Chilkat.CreateCS ccs = new Chilkat.CreateCS();
	
		    // First check the Current User certificate store, then the
		    // Local Machine cert store.
		    Chilkat.CertStore cs = ccs.OpenCurrentUserStore();
	
		    Chilkat.Cert cert = cs.FindCertBySubjectE("support@chilkatsoft.com");
		    if (cert == null)
		    {
			cs = ccs.OpenLocalSystemStore();
			cert = cs.FindCertBySubjectE("support@chilkatsoft.com");
			if (cert == null)
			{
			    TextBox1.Text = "No matching certificate found!";
			    return;
			}
		    }		
	
		    // Do we have a private key?
		    if (cert.HasPrivateKey())
		    {
			// Let’s use Chilkat Crypt to create the signature.
			Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
			crypt.UnlockComponent("30-day trial");
	
			// Use this certificate for signing.
			crypt.SetSigningCert(cert);
	
			// The signature will be returned in base64 format.
			crypt.EncodingMode = "base64″;
			string sig = crypt.SignStringENC("This string will be signed");
			if (sig.Length == 0)
			{
			    TextBox4.Text = crypt.LastErrorText;
			}
			else
			{
			    TextBox1.Text = "Create Digital Signature!";
			}
		    }
		    else
		    {
			TextBox1.Text = "Cannot create signature without a private key!";
		    }
</font>


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.