开发者

Key Size in RSA C# is not changing !

I am generating key pair and store them in xml file using

ToXmlString(true);

I need to set the key size to 2048 according to the MSDN the only place to do this is from the constructor of the RSACryptoSe开发者_如何转开发rviceProvider

    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);
    }

when I check the key size using

int x = this.rsa.KeySize;

I always get 1024 so whats the wrong here??


I've seen this before, try changing the container name or try

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

}

or this.rsa.Clear(); after you are done with it.

If you already have a container with the same name it will re-use the container I believe.


You need first to clear the existing container like this:

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

Then it should work with you. Don't forget to set:

rsa.PersistKeyInCsp = true;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜