开发者

How to set key size of RSA key created using aspnet_regiis?

I have created a customer RSA key container (for encrypting connection string in web.config) using the following command:

aspnet_regiis -pc "Test开发者_JS百科Keys" -size 2048 -exp

I then exported the key to an xml file and used it to initialise an instance of RSACryptoServiceProvider so that I could check the key size was definitely 2048. However, using the code below, the key size is displayed as 1024.

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
    using (FileStream fs = new FileStream(@"C:\TestKeys.xml", FileMode.Open))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            rsa.FromXmlString(sr.ReadToEnd());
        }
    }

    Console.WriteLine(rsa.KeySize.ToString());
}

It seems that aspnet_regiis is ignoring the -size argument. Am I missing something?

Also, is there a recommended key size for encrypting .Net config sections using RSA?


I ended up using the RSACryptoServiceProvider class to create the key container as it creates the keys with the size specified.

CspParameters cp = new CspParameters();

cp.KeyContainerName = keyContainerName;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
cp.KeyNumber = 1;

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize, cp))
{
    using (FileStream fs = new FileStream(fileName, FileMode.Create))
    {
        using (StreamWriter sw = new StreamWriter(fs))
        {
            sw.WriteLine(rsa.ToXmlString(true));
        }
    }
}

NIST receommend a key size of 3072 bits for security required beyond 2030.

Recommendations for key management - see Table 4.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜