如何使用 .net 库生成强命名 SNK 密钥文件
我的产品需要能够生成 .snk 文件(无需在系统上安装 Microsoft SDK)。 我可以生成一个工作 SNK 文件,但在指定密码时似乎无法使其工作。 有人能给我一些指点吗? 这是我到目前为止所拥有的:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的 KeyPassword 参数是用于其他目的而不是您尝试使用它的目的。从文档中:
另请参阅此问题的答案:简单使用 RSACryptoServiceProvider KeyPassword 失败
除此之外,您似乎无法使用密码保护 .snk 文件。在这种情况下,您可以做的是使用 PKCS#12 文件 (.pfx),该文件可用于对程序集进行签名,并且还可以受密码保护。您可以使用 BouncyCastle.NET 等库生成 PKCS#12 文件。有关于如何执行此操作的很好的文档,因此我不会在这里详细介绍。
例如,请参阅:是否可以仅使用 C# 以编程方式生成 X509 证书?
The KeyPassword parameter you use is intended for another purpose than the one you are trying to use it for. From the documentation:
See also the answer to this question: Simple use of RSACryptoServiceProvider KeyPassword fails
Besides this, it doesn't seem like you can protect .snk files with a password. What you can do in this case is to use a PKCS#12 file (.pfx) instead which can be used to sign the assemblies and also can be password protected. You can generate a PKCS#12 file using a library such as BouncyCastle.NET. There is good documentation out there on how to do this, so I won't go into detail here.
See for example: Is it possible to programmatically generate an X509 certificate using only C#?