Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# X509Certificate2.导出失败,返回“0”;“密钥集不存在”;控制台内应用程序_C#_Cryptography_Certificate_X509certificate_Bouncycastle - Fatal编程技术网

C# X509Certificate2.导出失败,返回“0”;“密钥集不存在”;控制台内应用程序

C# X509Certificate2.导出失败,返回“0”;“密钥集不存在”;控制台内应用程序,c#,cryptography,certificate,x509certificate,bouncycastle,C#,Cryptography,Certificate,X509certificate,Bouncycastle,我不知道如何“正确”地存储和使用代码中的证书,因此将使用“简单的语言”来解释我的问题 我有证书,它是由代码生成的(使用Bouncy Castle),实际上是用来创建证书的 代码返回Org.BouncyCastle.X509.X509Certificatecertificate 然后使用Pkcs12Store(Bouncy Castle类)将此证书对象转换为byte[]数组,并保存到c:\temp\cert.pfx文件中 现在我需要将证书从文件加载到代码中,在私钥上更新加密服务提供商(CSP)(这

我不知道如何“正确”地存储和使用代码中的证书,因此将使用“简单的语言”来解释我的问题

我有证书,它是由代码生成的(使用Bouncy Castle),实际上是用来创建证书的

  • 代码返回
    Org.BouncyCastle.X509.X509Certificate
    certificate
  • 然后使用
    Pkcs12Store
    (Bouncy Castle类)将此证书对象转换为
    byte[]
    数组,并保存到
    c:\temp\cert.pfx
    文件中 现在我需要将证书从文件加载到代码中,在私钥上更新加密服务提供商(CSP)(这是不正确的),并将证书和私钥写入新的
    cert2.pfx
    文件

    更新

    在第一次运行时,我的console应用程序将通过,而所有其他运行时,我将在此行中得到错误“密钥集不存在”
    certificate.Export(X509ContentType.Pkcs12,“密码”)

    注:

    代码正在控制台应用程序中运行,文件夹
    c:\temp\
    已获得
    所有人的完全权限

    大多数与“密钥集不存在”相关的主题都与权限相关(例如,网络服务没有正确的访问权限),我想这不是我的情况

    我尚未将
    cert.pfx
    导入windows应用商店(个人或本地计算机),因此不希望导入它

    这是代码示例:

    var certificate = new X509Certificate2(certFileBytes, "password", X509KeyStorageFlags.Exportable);
    
    var privKey = certificate.PrivateKey as RSACryptoServiceProvider;
    
    // will be needed later
    var exported = privKey.ToXmlString(true);
    
    // change CSP
    var cspParams = new CspParameters()
    {
        ProviderType = 24,
        ProviderName = "Microsoft Enhanced RSA and AES Cryptographic Provider"
    };
    
    var rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);
    cspParams.CryptoKeySecurity = new CryptoKeySecurity();
    cspParams.CryptoKeySecurity.SetAccessRule(rule);
    
    // create new PrivateKey from CspParameters and exported privkey
    var newPrivKey = new RSACryptoServiceProvider(cspParams);
    newPrivKey.PersistKeyInCsp = true;
    newPrivKey.FromXmlString(exported);
    
    // Assign edited private key back
    certificate.PrivateKey = newPrivKey;
    
    // export as PKCS#12/PFX
    var bytes = certificate.Export(X509ContentType.Pkcs12, "password");
    
    File.WriteAllBytes(@"c:\temp\cert2.pfx", bytes);
    

    谢谢。

    我在使用Export-PfxCertificate cmdlet时遇到了这个问题(它给出了类似的错误)。以管理员身份运行powershell窗口解决了此问题。

    我在使用Export-PfxCertificate cmdlet时遇到了此问题(它给出了类似的错误)。以管理员身份运行powershell窗口解决了问题。

    我不认为仅仅替换私钥就足以生成有效的证书。我知道这是一个长期目标,但不知道如何更新私钥上的加密服务提供商以更正私钥并保持证书有效。不确定这是否可行,但由于我们的系统中有许多集成,因此它能以某种方式工作是非常方便的。你曾经解决过这个@mimo吗?很抱歉,回复太晚,我确实通过变通解决了这个问题。我使用命令行工具
    openssl
    更新了不正确的证书CSP。这是一个示例:
    opensslpkcs12-in C:\temp\cert.pfx-out C:\temp\cert.pem-password:password-passout-password:password
    和使用corrent CSP重新导出证书:
    opensslpkcs12-export-in C:\temp\cert.pem-out C:\temp\cert2.pfx-CSP“Microsoft增强的RSA和AES加密提供程序”-passin pass:password-passout pass:password
    我想用代码实现它,但由于这个错误“密钥集不存在”,所以无法实现。我认为它发生了,因为我是直接将证书导出到文件中,而不是通过某个
    存储
    。我不认为仅仅替换私钥就足以生成有效的证书。我知道这是一个长期目标,但不知道如何更新私钥上的加密服务提供商以更正私钥并保持证书有效。不确定这是否可行,但由于我们的系统中有许多集成,因此它能以某种方式工作是非常方便的。你曾经解决过这个@mimo吗?很抱歉,回复太晚,我确实通过变通解决了这个问题。我使用命令行工具
    openssl
    更新了不正确的证书CSP。这是一个示例:
    opensslpkcs12-in C:\temp\cert.pfx-out C:\temp\cert.pem-password:password-passout-password:password
    和使用corrent CSP重新导出证书:
    opensslpkcs12-export-in C:\temp\cert.pem-out C:\temp\cert2.pfx-CSP“Microsoft增强的RSA和AES加密提供程序”-passin pass:password-passout pass:password
    我想用代码实现它,但由于这个错误“密钥集不存在”,所以无法实现。我想它发生了,因为我是直接将证书导出到文件中,而不是通过一些
    存储