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

☆樱花仙子☆ 提交于 2019-12-22 14:38:12

问题


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

aspnet_regiis -pc "TestKeys" -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?


回答1:


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.



来源:https://stackoverflow.com/questions/5282096/how-to-set-key-size-of-rsa-key-created-using-aspnet-regiis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!