Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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
RSA C#中的密钥大小不变!_C#_Rsa_Encryption Asymmetric_Private Key - Fatal编程技术网

RSA C#中的密钥大小不变!

RSA C#中的密钥大小不变!,c#,rsa,encryption-asymmetric,private-key,C#,Rsa,Encryption Asymmetric,Private Key,我正在生成密钥对,并使用 ToXmlString(true); 我需要将密钥大小设置为2048 根据MSDN,唯一可以这样做的地方是RSACryptServiceProvider的构造函数 private void AssignParameter(ProviderType providerType) { CspParameters cspParams; cspParams = new CspParameters((int)providerTyp

我正在生成密钥对,并使用

ToXmlString(true);
我需要将密钥大小设置为2048 根据MSDN,唯一可以这样做的地方是RSACryptServiceProvider的构造函数

    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分
那么这里出了什么问题???

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

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

}
或者
this.rsa.Clear()完成后


如果您已经有一个同名的容器,我相信它将重新使用该容器。

您需要首先清除现有容器,如下所示:

rsa.PersistKeyInCsp = false;
rsa.Clear();
那它应该适合你。 不要忘记设置:

rsa.PersistKeyInCsp = true;

你的代码对我有用,我得到了2048。你认为providerType是什么?我在创建CSParams时使用了值1(PROV_RSA_FULL)。我还将自己的字符串传递到csparams.KeyContainerName中。我正在使用值为1的PROV_RSA_FULL以及我的一个字符串作为KeyContainerName属性!我得到“消息对象已经存在。”是的,注意到同样的问题,删除了容器并导入了更大的密钥,仍然失败。创建了一个不同名称的新容器,效果很好!