sign1

using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.IO;

namespace Com.Organization.Ca.Esign
{
 class Certificate
 {
  public static string CertState = null;

  /// <summary>
  ///
  /// </summary>
  /// <returns></returns>
  private static X509Certificate2 GetCertificate()
  {
   X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
   st.Open(OpenFlags.ReadOnly);
   X509Certificate2Collection col = st.Certificates;
   X509Certificate2 cert = null;
   X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificates",
    "Select one to sign", X509SelectionFlag.SingleSelection);
   if (sel.Count > 0)
   {
    X509Certificate2Enumerator en = sel.GetEnumerator();
    en.MoveNext();
    cert = en.Current;
   }

   st.Close();
   return cert;
  }

  /// <summary>
  ///
  /// </summary>
  /// <returns></returns>
  private static X509Certificate2 GetCertificate(string sCertName)
  {
   //開啟憑證Store
   System.Security.Cryptography.X509Certificates.X509Store myStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
   myStore.Open(OpenFlags.ReadOnly);

   //未安裝證書
   if (myStore.Certificates.Count < 1)
   {
    myStore.Close();
    return null;
   }

   //有安裝一張以上證書
   if (myStore.Certificates.Count > 1)
   {
    int iNameCount = 0;
    int iNameindex = 0;
    if (sCertName != "")
    {
     for (int i = 0; i < myStore.Certificates.Count; i++)
     {
      if (myStore.Certificates[i].GetNameInfo(X509NameType.SimpleName, false).ToUpper() == sCertName.ToUpper())
      {
       iNameCount = iNameCount + 1;
       iNameindex = i;
      }
     }
    }

    if ((iNameCount > 1) || (iNameCount == 0))
    {
     //彈出選擇證書窗框
     System.Security.Cryptography.X509Certificates.X509Certificate2Collection myCerts =
      System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(myStore.Certificates,
      "Selecting Certificate", "請選擇您的證書,再點擊確認:\nPlease select your certificate, and then click OK:", X509SelectionFlag.SingleSelection);
     myStore.Close();
     if (myCerts.Count > 0)
     {
      return myCerts[0];
     }
     else
     {
      return null;
     }
    }
    else
    {
     System.Security.Cryptography.X509Certificates.X509Certificate2 myCert = myStore.Certificates[iNameindex];
     myStore.Close();
     return myCert;
    }
   }
   else
   {
    //只有一張憑證時直接返回此憑證,不用選擇
    System.Security.Cryptography.X509Certificates.X509Certificate2 myCert = myStore.Certificates[0];
    myStore.Close();
    if (sCertName != "")
    {
     if (myCert.GetNameInfo(X509NameType.SimpleName, false).ToUpper() == sCertName.ToUpper())
      return myCert;
     else
      return null;
    }
    else
    {
     return myCert;
    }
   }
  }

  /// <summary>
  /// this method is for login action, display the personal certificates replace of capicom
  /// </summary>
  /// <returns></returns>
  internal static string SignIn()
  {
   try
   {
    X509Certificate2 myCert = GetCertificate();
    //generate the serialnumber
    string sCertSerNum = "";
    byte[] iCertSerNum = myCert.GetSerialNumber();
    string s = myCert.GetSerialNumberString();
    for (int i = iCertSerNum.Length - 1; i >= 0; i--)
    {
     string str1 = iCertSerNum[i].ToString("X").PadLeft(2, '0');
     sCertSerNum = sCertSerNum + str1;
    }
    string sPublicKey = myCert.GetPublicKeyString();
    string sCertNames = myCert.Subject;
    string sEffectiveDate = myCert.GetEffectiveDateString();
    string sExpirationDate = myCert.GetExpirationDateString();
    string sRawCertData = Convert.ToBase64String(myCert.GetRawCertData());
    string sIssuer = myCert.GetNameInfo(X509NameType.SimpleName, true);

    //fixed the signdata string
    string sSignDataStr = sCertNames + ",Publisher=" + sIssuer + ",SN=" + sCertSerNum + ",EffectiveDate=" + sEffectiveDate + ",ExpirationDate=" + sExpirationDate
     + ",RawCertData=" + sRawCertData;
    //return the value
    return sSignDataStr;
   }
   catch (Exception ex)
   {
    throw ex;
   }
  }

posted on 2009-01-05 14:52  ChinaLeo  阅读(185)  评论(0)    收藏  举报

导航