#副標=以.NET整合自然人憑證(2) #大標=自然人憑證身份驗證範例 #作者=文/李明儒 -----box----- #程式1 查詢個人憑證區的憑證資料 using System; //執行程式前,請記得先安裝WSE using Microsoft.Web.Services2.Security.X509; namespace ExploreMyStore { class Class1 { [STAThread] static void Main(string[] args) { //開啟使用者的個人憑證儲存區 X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore); store.Open(); Console.WriteLine("共找到{0}張憑證!", store.Certificates.Count); //顯示所有憑證的Hash foreach (X509Certificate cert in store.Certificates) Console.WriteLine( cert.GetCertHashString() ); //按任意鍵結束 Console.Read(); } } } -----end----- -----box----- 程式2 使用自然人憑證進行簽章 using System; using Microsoft.Web.Services2.Security.X509; using System.Security.Cryptography; using System.Text; namespace Sign { class Class1 { [STAThread] static void Main(string[] args) { //開啟使用者的個人憑證儲存區 X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore); store.Open(); X509Certificate signCert=null; //找出第一張可用來簽名的自然人憑證 //以簽發單位名稱為比對依據 foreach (X509Certificate cert in store.Certificates) if (cert.GetIssuerName()== "C=TW, O=行政院, OU=內政部憑證管理中心" && cert.SupportsDigitalSignature) //註1 { signCert=cert; break; } string content="重要資料內容"; //進行電子簽章作業 if (signCert!=null) { //使用SafeSign CSP時, //KeyContainerName等於憑證的 //SHA1Hash(Thumbprint)欄位 CspParameters cspParam= new CspParameters(1, "SafeSign CSP Version 1.0", signCert.GetCertHashString().ToUpper()); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParam); //將內容轉為Byte Array byte[] signature; try { //指定SHA1 Hash後製作數位簽章 signature=rsa.SignData( Encoding.Unicode.GetBytes("簽章內容"), new SHA1CryptoServiceProvider()); Console.WriteLine("文件本文->"+content); Console.WriteLine("電子簽章->"+ Convert.ToBase64String(signature)); } catch //密碼錯或簽章失敗時傳回錯誤 { throw new ApplicationException( "簽章失敗!"); } } else Console.WriteLine("未找到可用的自然人憑證!"); //按任意鍵結束 Console.Read(); } } } -----end----- -----box----- 程式3 電子簽章驗證範例 //記得在專案中參考及using X509PublicKeyParser static bool verifyData(X509Certificate cert, string content, byte[] signature) { try { RSACryptoServiceProvider rsa= new RSACryptoServiceProvider(); //以X509PublicKeyParser由憑證中取出公鑰 RSAParameters p = X509PublicKeyParser. GetRSAPublicKeyParameters(cert); rsa.ImportParameters(p); return rsa.VerifyData( Encoding.Unicode.GetBytes(content), new SHA1CryptoServiceProvider(), signature); } catch { return false; } } -----end----- -----box----- 程式4 修改程式2加上簽章驗證流程 try { //指定SHA1 Hash後製作數位簽章 signature=rsa.SignData( Encoding.Unicode.GetBytes(content), new SHA1CryptoServiceProvider()); Console.WriteLine("文件本文->"+content); Console.WriteLine("電子簽章->"+ Convert.ToBase64String(signature)); //可以試著變更content及signature //會得到false的驗證結果,例如: //content+="AA"; //signature[0]=255; if (verifyData(signCert,content,signature)) Console.WriteLine("簽章驗證成功!"); else Console.WriteLine("簽章驗證失敗!"); } catch //密碼錯或簽章失敗時傳回錯誤 { throw new ApplicationException("簽章失敗!"); } -----end-----