• xamarin uwp数字证书公钥私钥


    对于数字证书存储导入到电脑中,采用如下方式:

     1 /// <summary>
     2         ///  导入证书
     3         /// </summary>
     4         /// <param name="rawData">证书字节数组</param>
     5         /// <param name="keyStorePassWord">keyStore密码</param>
     6         /// <param name="keyPassWord">证书密码</param>
     7         /// <param name="certificateType">证书类型</param>
     8         /// <returns>true表示导入成功 false表示导入失败</returns>
     9         public bool ImportCertificate(byte[] rawData, string keyStorePassWord, string keyPassWord, string mailAccount, CertificateType certificateType = CertificateType.Pkcs12)
    10         {
    11             try
    12             {
    13                 bool result = false;
    14                 if (certificateType == CertificateType.Pkcs12)
    15                 {
    16                     using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
    17                     {
    18                         store.Open(OpenFlags.ReadWrite);
    19                         X509Certificate2 certificate = new X509Certificate2(rawData, keyPassWord, X509KeyStorageFlags.Exportable);
    20                         //X509Certificate2 certificate = new X509Certificate2(rawData, keyPassWord, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
    21                        //获取公钥
    22                         var cerPublicKey = certificate.Export(X509ContentType.Cert);
    23                         result =true;
    24 
    25                         store.Add(certificate);
    26                     }
    27                     return result;
    28                 }
    29                 else
    30                 {
    31                     X509Certificate2 certificate = new X509Certificate2(rawData, keyPassWord);
    32                     result =true;
    33                     return result;
    34                 }
    35 
    36             }
    37             catch (Exception ex)
    38             {
    39                 DAL.ErrorLog.AddErrorLogs("证书保存失败!" + ex.Message);
    40                 return false;
    41             }
    42         }

    对应的获取相应的数字证书的私钥:

     1 /// <summary>
     2         ///  获取P12格式的证书
     3         /// </summary>
     4         /// <param name="serialNumber">证书序列号</param>
     5         /// <param name="keyStorePassWord">keyStore密码</param>
     6         /// <param name="keyPassWord">证书密码</param>
     7         /// <param name="hasCertificate">证书类型</param>
     8         /// <returns>如果返回null 表示没有获取到证书</returns>
     9         public byte[] GetCertificate(string serialNumber, string keyStorePassWord, string keyPassWord)
    10         {
    11             byte[] result = null;
    12             try
    13             {
    14                 using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
    15                 {
    16                     store.Open(OpenFlags.ReadWrite);
    17                     foreach (var item in store.Certificates)
    18                     {
    19                         if (item.SerialNumber == serialNumber)
    20                         {
    21                             result = item.Export(X509ContentType.Pfx, keyPassWord);
    22                             //result = item.RawData;
    23                             break;
    24                         }
    25                     }
    26                 }
    27             }
    28             catch (Exception ex)
    29             {
    30                 string errorMessage = ex.Message;
    31                 result = null;
    32             }
    33             return result;
    34         }

    上面的result = item.Export(X509ContentType.Pfx, keyPassWord);方法需注意:当参数为两个时,及你把证书密码放上去的时候获取的是私钥,当为如下时获取的是公钥:result = item.Export(X509ContentType.Pfx);

  • 相关阅读:
    转载2010年3月4日dojo学习笔记 dojo.disconnect
    转载谈谈JS里的{ }大括号和[ ]中括号的用法,理解后就可以看懂JSON结构了
    设置鹰眼(ArcGIS API for JavaScript)
    不安装Oracle10g客户端连接Oracle10g数据库【转】
    发布“个人知识库管理”软件,欢迎下载使用
    [转]更好地使用Google的服务
    技术资源:国内知名技术网站网址
    UrlRewritingNet与FCKEditor同时使用时需要注意的问题
    webreqeust/webresponse抓取URL信息
    javascript更改iframe链接的注意事项
  • 原文地址:https://www.cnblogs.com/zuimengaitianya/p/5893249.html
Copyright © 2020-2023  润新知