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

完整代码:

public sealed class DataCertificate
    {
        #region 生成证书
        /// <summary>   
        /// 根据指定的证书名和makecert全路径生成证书(包含公钥和私钥,并保存在MY存储区)   
        /// </summary>   
        /// <param name="subjectName"></param>   
        /// <param name="makecertPath"></param>   
        /// <returns></returns>   
        public static bool CreateCertWithPrivateKey(string subjectName, string makecertPath)
        {
            subjectName = "CN=" + subjectName;
            string param = " -pe -ss my -n \"" + subjectName + "\" ";
            try
            {
                Process p = Process.Start(makecertPath, param);
                p.WaitForExit();
                p.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        #endregion

        #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)
                {
                    Debug.Print(string.Format("certificate name: {0}", x509.Subject));

                    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();
            return true;
        }
        /// <summary>   
        /// 从WINDOWS证书存储区的个人MY区找到主题为subjectName的证书,   
        /// 并导出为CER文件(即,只含公钥的)   
        /// </summary>   
        /// <param name="subjectName"></param>   
        /// <param name="cerFileName"></param>   
        /// <returns></returns>   
        public static bool ExportToCerFile(string subjectName, string cerFileName)
        {
            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)
                {
                    Debug.Print(string.Format("certificate name: {0}", x509.Subject));
                    //byte[] pfxByte = x509.Export(X509ContentType.Pfx, password);   
                    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();
                                return false;
                            }
                        }
                        fileStream.Close();
                    }
                }
            }
            store.Close();
            store = null;
            storecollection = null;
            return true;
        }
        #endregion

        #region 从证书中获取信息
        /// <summary>   
        /// 根据私钥证书得到证书实体,得到实体后可以根据其公钥和私钥进行加解密   
        /// 加解密函数使用DEncrypt的RSACryption类   
        /// </summary>   
        /// <param name="pfxFileName"></param>   
        /// <param name="password"></param>   
        /// <returns></returns>   
        public static X509Certificate2 GetCertificateFromPfxFile(string pfxFileName,
            string password)
        {
            try
            {
                return new X509Certificate2(pfxFileName, password, X509KeyStorageFlags.Exportable);
            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>   
        /// 到存储区获取证书   
        /// </summary>   
        /// <param name="subjectName"></param>   
        /// <returns></returns>   
        public static X509Certificate2 GetCertificateFromStore(string subjectName)
        {
            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;
        }
        /// <summary>   
        /// 根据公钥证书,返回证书实体   
        /// </summary>   
        /// <param name="cerPath"></param>   
        public static X509Certificate2 GetCertFromCerFile(string cerPath)
        {
            try
            {
                return new X509Certificate2(cerPath);
            }
            catch (Exception e)
            {
                return null;
            }
        }
        #endregion
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: signtool是一个用于将数字签名应用于文件的工具。它可以用来验证文件的真实性和完整性,以及证明它们未被修改过。 要使用.signtool将.cer证书应用于文件,首先需要在计算机上安装.signtool工具。然后,可以按照以下步骤进行: 1. 打开命令提示符或PowerShell窗口,并导航到包含要签名文件的目录。 2. 使用signtool命令和指定的选项运行.signtool。例如,可以使用以下命令: signtool sign /f <证书文件.pfx> /p <密码> /t <时间戳服务器URL> <要签名的文件> 其中,/f选项指定证书文件(.pfx)的路径,/p选项指定证书密码,/t选项指定时间戳服务器的URL,最后是要签名的文件的路径。 3. 如果签名成功,将显示一条消息来确认签名已应用于文件。 4. 可以使用signtool verify命令验证签名是否成功。例如,可以使用以下命令: signtool verify /pa /v <要验证的文件> 其中,/pa选项指定使用公共应用的验证策略,/v选项指定详细输出,在最后是要验证的文件的路径。 5. 如果验证成功,将显示一条消息以确认签名有效。 通过以上步骤,您可以使用.signtool进行文件的数字签名。请记住,在使用.signtool进行签名之前,需要确保已获取有效的.cer证书,并具有相应的私钥。签名文件可以提供保护和验证文件的安全性和可靠性。 ### 回答2: signtool是Windows操作系统的一个工具,用于对软件进行数字签名以验证软件的真实性和完整性。要使用.signtool对.cer证书进行签名,可以按照以下步骤进行操作: 1. 确保你已经安装了signtool工具。signtool工具是Windows SDK的一部分,你可以从微软官方网站下载并安装Windows SDK。 2. 打开命令提示符。你可以按下Win + R组合键,然后输入cmd并按下Enter键来打开命令提示符。 3. 导航到你保存了.cer证书的文件夹。使用cd命令来切换到包含证书的文件夹,例如:cd C:\Certificates。 4. 输入以下命令来使用signtool对.cer证书进行签名: signtool sign /a /v /t http://timestamp.digicert.com <certificate_file.cer> 其中,<certificate_file.cer>是你要签名证书文件的名称。 5. 按下Enter键执行命令。signtool将使用指定的证书对文件进行签名,并通过指定的时间戳服务器对签名进行时间戳。 6. 等待命令执行完毕。签名过程可能需要一些时间,具体时间取决于证书的大小和系统的性能。 7. 完成了!一旦签名过程完成,你的.cer证书就会被正确地签名,并带有时间戳,以确保签名的验证和持久性。 请注意,如果你在签名过程中遇到任何错误或问题,可以参考signtool的文档或在网上搜索相关的解决方法。签名过程中也可能需要提供其他的参数,具体取决于你的需求和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值