【问题标题】:Key Size in RSA C# is not changing !RSA C# 中的密钥大小没有改变!
【发布时间】:2016-03-28 00:00:52
【问题描述】:

我正在生成密钥对并将它们存储在 xml 文件中使用

ToXmlString(true);

我需要将密钥大小设置为 2048 根据 MSDN,唯一可以做到这一点的地方是 RSACryptoServiceProvider 的构造函数

    private void AssignParameter(ProviderType providerType)
    {
        CspParameters cspParams;

        cspParams = new CspParameters((int)providerType);
        cspParams.KeyContainerName = RSAEncryption.containerName;
        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
        cspParams.KeyNumber = (int)KeyNumber.Exchange;

        this.rsa = new RSACryptoServiceProvider(2048, cspParams);
    }

当我使用检查密钥大小时

int x = this.rsa.KeySize;

我总是得到 1024 那么这里有什么问题吗??

【问题讨论】:

  • 你的代码对我有用,我得到了 2048。你为 providerType 传递了什么?我在创建 cspParams 时使用了值 1 (PROV_RSA_FULL)。我还将自己的字符串传递给 cspParams.KeyContainerName。
  • 我正在使用值为 1 的 PROV_RSA_FULL 以及我的 KeyContainerName 属性的一个字符串!
  • 我得到“消息对象已经存在。”

标签: c# rsa encryption-asymmetric private-key


【解决方案1】:

我以前见过这个,尝试更改容器名称或尝试

using (this.rsa = new RSACryptoServiceProvider(2048, cspParams)) 
{

}

this.rsa.Clear(); 完成后。

如果你已经有一个同名的容器,我相信它会重复使用这个容器。

【讨论】:

  • yes 注意到同样的问题,删除容器并导入更大的密钥,仍然失败。创建了一个不同名称的新容器,它工作正常!
【解决方案2】:

您需要先像这样清除现有容器:

rsa.PersistKeyInCsp = false;
rsa.Clear();

那么它应该适合你。 不要忘记设置:

rsa.PersistKeyInCsp = true;

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多