My product needs to be able to generate .snk files (without having Microsoft SDKs installed on the system). I can generate a working SNK file, but I can not seem to get it to work when specifying a password. Can anyone give me some pointers? This is what I have so far:
internal static void CreateKeyPairFile(string fileName, int keySize, string password)
{
if ((keySize % 8) != 0)
{
throw new CryptographicException("Invalid key size. Valid size is 384 to 16384 mod 8. Default 1024.");
}
CspParameters parms = new CspParameters();
parms.KeyNumber = 2;
if (null != password)
{
var passwordString = new System.Security.SecureString();
foreach (char c in password)
{
passwordString.AppendChar(c);
}
parms.Flags = CspProviderFlags.UseUserProtectedKey;
parms.KeyPassword = passwordString;
}
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(keySize, parms);
byte[] array = provider.ExportCspBlob(!provider.PublicOnly);
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(array, 0, array.Length);
}
}