zoukankan      html  css  js  c++  java
  • C#从证书存储区读取证书

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Security.Cryptography.X509Certificates;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Web;
     8 
     9 
    10 namespace makecertdemo
    11 {
    12     public class MakecertHelper
    13     {
    14         /// <summary>   
    15         /// 根据私钥证书得到证书实体,得到实体后可以根据其公钥和私钥进行加解密   
    16         /// 加解密函数使用DEncrypt的RSACryption类   
    17         /// </summary>   
    18         /// <param name="pfxFileName"></param>   
    19         /// <param name="password"></param>   
    20         /// <returns></returns>   
    21         public static X509Certificate2 GetCertificateFromPfxFile(string pfxFileName,
    22             string password)
    23         {
    24             try
    25             {
    26                 return new X509Certificate2(pfxFileName, password, X509KeyStorageFlags.Exportable);
    27             }
    28             catch (Exception e)
    29             {
    30                 return null;
    31             }
    32         }
    33 
    34         /// <summary>
    35         /// 从证书存储区读取证书
    36         /// </summary>
    37         /// <returns></returns>
    38         public static X509Certificate2 GetCertificateFromHost()
    39         {
    40             X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
    41             store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
    42             //Put certificates from the store into a collection so user can select one.
    43             X509Certificate2Collection fcollection = (X509Certificate2Collection)store.Certificates;
    44             //X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(fcollection, "Select an X509 Certificate", "Choose a certificate to examine.", X509SelectionFlag.MultiSelection);//弹出框,可以选择需要的证书
    45             X509Certificate2Collection findResult = fcollection.Find(X509FindType.FindBySerialNumber, "BFA9E49C5E44EA914BA64288CA6B8348", false);//有多个重载,可以用不同的方法查询
    46 
    47             return findResult[0];
    48         }
    49     }
    50 }
  • 相关阅读:
    生成更大的陆地 Making A Large Island
    HDU 3342 Legal or Not【拓扑排序】
    POJ 2367 Genealogical tree【拓扑排序】
    CodeForces Round #290 Div.2
    HDU 1010 Tempter of the Bone【DFS】
    HDU 1312 Red and Black【DFS】
    POJ 1664 放苹果【DFS】
    HDU 1587 Flowers【贪心】
    Codeforces Round #289 Div 2
    HDU 1241 Oil Deposits【DFS】
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/8668255.html
Copyright © 2011-2022 走看看