• 从自签名证书导出pfx和cer证书


    完整代码:

      1 public sealed class DataCertificate
      2     {
      3         #region 生成证书
      4         /// <summary>   
      5         /// 根据指定的证书名和makecert全路径生成证书(包含公钥和私钥,并保存在MY存储区)   
      6         /// </summary>   
      7         /// <param name="subjectName"></param>   
      8         /// <param name="makecertPath"></param>   
      9         /// <returns></returns>   
     10         public static bool CreateCertWithPrivateKey(string subjectName, string makecertPath)
     11         {
     12             subjectName = "CN=" + subjectName;
     13             string param = " -pe -ss my -n "" + subjectName + "" ";
     14             try
     15             {
     16                 Process p = Process.Start(makecertPath, param);
     17                 p.WaitForExit();
     18                 p.Close();
     19             }
     20             catch (Exception e)
     21             {
     22                 return false;
     23             }
     24             return true;
     25         }
     26         #endregion
     27 
     28         #region 文件导入导出
     29         /// <summary>   
     30         /// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,   
     31         /// 并导出为pfx文件,同时为其指定一个密码   
     32         /// 并将证书从个人区删除(如果isDelFromstor为true)   
     33         /// </summary>   
     34         /// <param name="subjectName">证书主题,不包含CN=</param>   
     35         /// <param name="pfxFileName">pfx文件名</param>   
     36         /// <param name="password">pfx文件密码</param>   
     37         /// <param name="isDelFromStore">是否从存储区删除</param>   
     38         /// <returns></returns>   
     39         public static bool ExportToPfxFile(string subjectName, string pfxFileName,
     40             string password, bool isDelFromStore)
     41         {
     42             subjectName = "CN=" + subjectName;
     43             X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
     44             store.Open(OpenFlags.ReadWrite);
     45             X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
     46             foreach (X509Certificate2 x509 in storecollection)
     47             {
     48                 if (x509.Subject == subjectName)
     49                 {
     50                     Debug.Print(string.Format("certificate name: {0}", x509.Subject));
     51 
     52                     byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);
     53                     using (FileStream fileStream = new FileStream(pfxFileName, FileMode.Create))
     54                     {
     55                         // Write the data to the file, byte by byte.   
     56                         for (int i = 0; i < pfxByte.Length; i++)
     57                             fileStream.WriteByte(pfxByte[i]);
     58                         // Set the stream position to the beginning of the file.   
     59                         fileStream.Seek(0, SeekOrigin.Begin);
     60                         // Read and verify the data.   
     61                         for (int i = 0; i < fileStream.Length; i++)
     62                         {
     63                             if (pfxByte[i] != fileStream.ReadByte())
     64                             {
     65                                 fileStream.Close();
     66                                 return false;
     67                             }
     68                         }
     69                         fileStream.Close();
     70                     }
     71                     if (isDelFromStore == true)
     72                         store.Remove(x509);
     73                 }
     74             }
     75             store.Close();
     76             return true;
     77         }
     78         /// <summary>   
     79         /// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,   
     80         /// 并导出为CER文件(即,只含公钥的)   
     81         /// </summary>   
     82         /// <param name="subjectName"></param>   
     83         /// <param name="cerFileName"></param>   
     84         /// <returns></returns>   
     85         public static bool ExportToCerFile(string subjectName, string cerFileName)
     86         {
     87             subjectName = "CN=" + subjectName;
     88             X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
     89             store.Open(OpenFlags.ReadWrite);
     90             X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
     91             foreach (X509Certificate2 x509 in storecollection)
     92             {
     93                 if (x509.Subject == subjectName)
     94                 {
     95                     Debug.Print(string.Format("certificate name: {0}", x509.Subject));
     96                     //byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);   
     97                     byte[] cerByte = x509.Export(X509ContentType.Cert);
     98                     using (FileStream fileStream = new FileStream(cerFileName, FileMode.Create))
     99                     {
    100                         // Write the data to the file, byte by byte.   
    101                         for (int i = 0; i < cerByte.Length; i++)
    102                             fileStream.WriteByte(cerByte[i]);
    103                         // Set the stream position to the beginning of the file.   
    104                         fileStream.Seek(0, SeekOrigin.Begin);
    105                         // Read and verify the data.   
    106                         for (int i = 0; i < fileStream.Length; i++)
    107                         {
    108                             if (cerByte[i] != fileStream.ReadByte())
    109                             {
    110                                 fileStream.Close();
    111                                 return false;
    112                             }
    113                         }
    114                         fileStream.Close();
    115                     }
    116                 }
    117             }
    118             store.Close();
    119             store = null;
    120             storecollection = null;
    121             return true;
    122         }
    123         #endregion
    124 
    125         #region 从证书中获取信息
    126         /// <summary>   
    127         /// 根据私钥证书得到证书实体,得到实体后可以根据其公钥和私钥进行加解密   
    128         /// 加解密函数使用DEncrypt的RSACryption类   
    129         /// </summary>   
    130         /// <param name="pfxFileName"></param>   
    131         /// <param name="password"></param>   
    132         /// <returns></returns>   
    133         public static X509Certificate2 GetCertificateFromPfxFile(string pfxFileName,
    134             string password)
    135         {
    136             try
    137             {
    138                 return new X509Certificate2(pfxFileName, password, X509KeyStorageFlags.Exportable);
    139             }
    140             catch (Exception e)
    141             {
    142                 return null;
    143             }
    144         }
    145         /// <summary>   
    146         /// 到存储区获取证书   
    147         /// </summary>   
    148         /// <param name="subjectName"></param>   
    149         /// <returns></returns>   
    150         public static X509Certificate2 GetCertificateFromStore(string subjectName)
    151         {
    152             subjectName = "CN=" + subjectName;
    153             X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    154             store.Open(OpenFlags.ReadWrite);
    155             X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
    156             foreach (X509Certificate2 x509 in storecollection)
    157             {
    158                 if (x509.Subject == subjectName)
    159                 {
    160                     return x509;
    161                 }
    162             }
    163             store.Close();
    164             store = null;
    165             storecollection = null;
    166             return null;
    167         }
    168         /// <summary>   
    169         /// 根据公钥证书,返回证书实体   
    170         /// </summary>   
    171         /// <param name="cerPath"></param>   
    172         public static X509Certificate2 GetCertFromCerFile(string cerPath)
    173         {
    174             try
    175             {
    176                 return new X509Certificate2(cerPath);
    177             }
    178             catch (Exception e)
    179             {
    180                 return null;
    181             }
    182         }
    183         #endregion
    184     }
  • 相关阅读:
    个人学习随笔(psi-blast随笔)
    psp进度(11月25号-31号)
    本周psp(11月17-23)
    规格说明书练习-吉林市1日游
    补PSP进度(10.28-11.3)
    第九周PSP&进度条
    (第九周)读构建之法有感1
    (第九周)读构建之法有感2
    词频统计的效能测试。
    (第二周)读《构建之法》有感
  • 原文地址:https://www.cnblogs.com/frankyou/p/4813866.html
Copyright © 2020-2023  润新知