使用X509Certificate2类操作证书文件
public class CertHelper { string pfxPath = @"E:\开发辅助项目\cert\taisuyuanqing.pfx"; string cerPath = @"E:\开发辅助项目\cert\taisuyuanqing.cer"; #region *从文件中读取公钥和私钥 #region +从文件中读取公钥 /// <summary> /// 从文件中读取公钥 /// </summary> /// <returns></returns> public string GetPlulicKey() { try { X509Certificate2 x509 = new X509Certificate2(cerPath); return x509.PublicKey.Key.ToXmlString(false); } catch (Exception ex) { return string.Empty; } } #endregion #region +从私钥文件中读取私钥 /// <summary> /// 从私钥文件中读取私钥 /// </summary> public void GetPrivateKey() { try { X509Certificate2 x509 = new X509Certificate2(pfxPath, "123456", X509KeyStorageFlags.Exportable); var publicKey = x509.PublicKey.Key.ToXmlString(false); var privateKey = x509.PrivateKey.ToXmlString(true); var str = "逍遥帝君 15210253 xiaoyaodijun"; var result = RSAHelper.RSAEncrypt(publicKey, str); var content = RSAHelper.RSADecrypt(privateKey, result); } catch (Exception) { throw; } } #endregion #endregion #region + 从证书库中获取证书 /// <summary> /// 从证书库中获取证书 /// </summary> /// <param name="subjectName">证书名字</param> /// <returns></returns> public X509Certificate2 GetCertificateFromStore(string subjectName) { try { subjectName = "CN=" + subjectName; X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; foreach (X509Certificate2 x509 in storecollection) { if (x509.Subject == subjectName) { return x509; } } store.Close(); store = null; storecollection = null; return null; } catch (Exception) { throw; } } #endregion #region *导出证书文件 #region +从证书库中导出私钥文件 /// <summary> /// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书, /// 并导出为pfx文件,同时为其指定一个密码 /// 并将证书从个人区删除(如果isDelFromstor为true) /// </summary> /// <param name="subjectName">证书主题,不包含CN=</param> /// <param name="pfxFileName">pfx文件名</param> /// <param name="password">pfx文件密码</param> /// <param name="isDelFromStore">是否从存储区删除</param> /// <returns></returns> public static bool ExportToPfxFile(string subjectName, string pfxFileName, string password, bool isDelFromStore) { subjectName = "CN=" + subjectName; X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; foreach (X509Certificate2 x509 in storecollection) { if (x509.Subject == subjectName) { byte[] pfxByte = x509.Export(X509ContentType.Pfx, password); using (FileStream fileStream = new FileStream(pfxFileName, FileMode.Create)) { // Write the data to the file, byte by byte. for (int i = 0; i < pfxByte.Length; i++) fileStream.WriteByte(pfxByte[i]); // Set the stream position to the beginning of the file. fileStream.Seek(0, SeekOrigin.Begin); // Read and verify the data. for (int i = 0; i < fileStream.Length; i++) { if (pfxByte[i] != fileStream.ReadByte()) { fileStream.Close(); return false; } } fileStream.Close(); } if (isDelFromStore == true) store.Remove(x509); } } store.Close(); store = null; storecollection = null; return true; } #endregion #region + 从证书库中导出公钥文件 /// <summary> /// 从证书库中导出公钥文件 /// </summary> /// <param name="subjectName">证书名字</param> /// <param name="cerFileName">存放公钥的文件路径</param> public void ExportToCerFile(string subjectName, string cerFileName) { try { subjectName = "CN=" + subjectName; X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadWrite); X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; foreach (X509Certificate2 x509 in storecollection) { if (x509.Subject != subjectName) continue; byte[] cerByte = x509.Export(X509ContentType.Cert); using (FileStream fileStream = new FileStream(cerFileName, FileMode.Create)) { // Write the data to the file, byte by byte. for (int i = 0; i < cerByte.Length; i++) fileStream.WriteByte(cerByte[i]); // Set the stream position to the beginning of the file. fileStream.Seek(0, SeekOrigin.Begin); // Read and verify the data. for (int i = 0; i < fileStream.Length; i++) { if (cerByte[i] != fileStream.ReadByte()) { fileStream.Close(); } } fileStream.Close(); } } } catch (Exception) { throw; } } #endregion #endregion #region + 从证书库中获取私钥和公钥 /// <summary> /// 从证书库中获取私钥和公钥 /// </summary> /// <param name="subjectName"></param> /// <param name="password"></param> /// <returns></returns> public string GetPrivateKeyFromStore(string subjectName, string password) { try { subjectName = "CN=" + subjectName; //CurrentUser=当前用户 LocalMachine=本地计算机 X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; foreach (X509Certificate2 x509 in storecollection) { if (x509.Subject != subjectName) continue; x509.Export(X509ContentType.Pfx, password); var privateKey = x509.PrivateKey.ToXmlString(true); var publicKey = x509.PublicKey.Key.ToXmlString(false); var str = "逍遥帝君 15210253 xiaoyaodijun"; var result = RSAHelper.RSAEncrypt(publicKey, str); var content = RSAHelper.RSADecrypt(privateKey, result); } return ""; } catch (Exception) { return ""; } } #endregion
public class RSAHelper { /// RSA加密 /// </summary> /// <param name="xmlPublicKey">公钥</param> /// <param name="m_strEncryptString"></param> /// <returns></returns> public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(xmlPublicKey); byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString); return Convert.ToBase64String(provider.Encrypt(bytes, false)); } /// <summary> /// 解密 /// </summary> /// <param name="xmlPrivateKey">私钥</param> /// <param name="m_strDecryptString"></param> /// <returns></returns> public static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(xmlPrivateKey); byte[] rgb = Convert.FromBase64String(m_strDecryptString); byte[] bytes = provider.Decrypt(rgb, false); return new UnicodeEncoding().GetString(bytes); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2017-12-22 docker部署项目 <三>
2017-12-22 docker 安装mysql数据库 <二>
2017-12-22 docker安装 <一>
2014-12-22 简单的mvvm light 应用