• 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);

  • 相关阅读:
    基于Kalman filter 的北斗卫星三维坐标后数据处理
    原子串口使用0D0A结尾作为结束字符
    GY90614 读取温度,修改辐射率,拟合曲线
    CRH PC11配置
    _I2C_SDA_READ 状态确定为输入
    read_byte
    SDA 右移赋值
    积极心态-刚入职的我们
    mysql基础知识点(复习一)
    java基础知识点(复习一)
  • 原文地址:https://www.cnblogs.com/zuimengaitianya/p/5893249.html
Copyright © 2020-2023  润新知