Search code examples
c#rsacryptoserviceprovider

c# RSACryptoServiceProvider with CspParameters cannot set keySize


I read that we cannot set keySize for RSACryptoServiceProvider(int, cspParams)

CspParameters cspParams = new CspParameters
{
    KeyContainerName = "KeyContainer"
};
            
var rsaRoot = new RSACryptoServiceProvider(2048, cspParams);
Console.WriteLine(rsaRoot.KeySize); // 1024

The only way to set keysize is using RSACryptoServiceProvider(int), How can I Set CspParameters KeyContainerName = "KeyContainer" with this method?


Solution

  • Solved I have to use unique KeyContainerName

    CspParameters cspParams = new CspParameters
    {
        KeyContainerName = Guid.NewGuid().ToString()
    
    };
    
    var rsaRoot = new RSACryptoServiceProvider(2048, cspParams);
    

    about readonly statement (url)

    The RSACryptoServiceProvider class does not allow you to change key sizes using the KeySize property. Any value written to this property will fail to update the property without error. To change the key size, use one of the constructor overloads.

    I think me or them is misunderstood, It cannot change the keySize after created but we can initialize the keySize