Cannot export generated certificate with a private key to byte array in .NET 4.0/4.5

后端 未结 1 1689
有刺的猬
有刺的猬 2020-12-06 17:42

I need to export and import generated certificates with private keys to and from byte arrays, and I don\'t have any problems unless I use .NET framework 4.0 and 4.5. I\'m ge

相关标签:
1条回答
  • 2020-12-06 18:28

    I haven't noticed that CspKeyContainerInfo.CspParameters.KeyContainerName is empty after key creation in .NET 4.0 and .NET 4.5, but it was autogenerated in .NET 3.5. I've set a unique name for container and now I'm able to export the private key.

    public static AsymmetricAlgorithm ToDotNetKey(RsaPrivateCrtKeyParameters privateKey)
    {
        var cspParams = new CspParameters
        {
              KeyContainerName = Guid.NewGuid().ToString(),
              KeyNumber = (int)KeyNumber.Exchange,
              Flags = CspProviderFlags.UseMachineKeyStore
        };
    
        var rsaProvider = new RSACryptoServiceProvider(cspParams);
        // ...
    
    0 讨论(0)
提交回复
热议问题