[Solved] Create CNG Keys

May 3, 2012 at 6:38 AM
Edited May 3, 2012 at 6:48 AM

Hi,

I have been able to create a CNG Key with this, even gone as far as manipulating some of the parameters... Now only need to be able to specify the key length, which I have not been able to do... Below is a snippet that generates a new key with the details I want, except the setting the key size... I tried adding as a parameter but get an exception.

 

CngKeyCreationParameters cngParams = new CngKeyCreationParameters(); 
cngParams.KeyUsage = CngKeyUsages.Decryption; 
cngParams.Provider = CngProvider.MicrosoftSoftwareKeyStorageProvider; 
//Throws unsupported exception 
//cngParams.Parameters.Add(new CngProperty("KeySize", new System.Text.UTF8Encoding().GetBytes("2048"),CngPropertyOptions.Persist));
key = CngKey.Create(CngAlgorithm2.Rsa, keyName, cngParams);
May 3, 2012 at 6:48 AM
Edited May 3, 2012 at 6:49 AM

[SOLVED] Posting for other for the benefit of others. Change the original posting's line #5 to:

cngParams.Parameters.Add(new CngProperty("Length", BitConverter.GetBytes(2048), CngPropertyOptions.None));

Will write a blog about it on my TechNet blog later.