[英]How to generate strong-naming SNK key file with .net libraries
My product needs to be able to generate.snk files (without having Microsoft SDKs installed on the system).我的产品需要能够生成 .snk 文件(无需在系统上安装 Microsoft SDK)。 I can generate a working SNK file, but I can not seem to get it to work when specifying a password.我可以生成一个有效的 SNK 文件,但在指定密码时我似乎无法让它工作。 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);
}
}
The KeyPassword parameter you use is intended for another purpose than the one you are trying to use it for.您使用的KeyPassword参数用于其他目的,而不是您尝试使用它的目的。 From the documentation:从文档中:
Use the KeyPassword property to supply a password for a smart card key.使用 KeyPassword 属性为智能卡密钥提供密码。 When you specify a password using this property, a password dialog will not be presented to the user.当您使用此属性指定密码时,不会向用户显示密码对话框。
See also the answer to this question: Simple use of RSACryptoServiceProvider KeyPassword fails另请参阅此问题的答案: 简单使用 RSACryptoServiceProvider KeyPassword 失败
Besides this, it doesn't seem like you can protect.snk files with a password.除此之外,您似乎无法使用密码保护 .snk 文件。 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.在这种情况下,您可以使用PKCS#12文件 (.pfx) 代替,该文件可用于对程序集进行签名,也可以使用密码保护。 You can generate a PKCS#12 file using a library such as BouncyCastle.NET .您可以使用BouncyCastle.NET等库生成 PKCS#12 文件。 There is good documentation out there on how to do this, so I won't go into detail here.关于如何做到这一点有很好的文档,所以我不会在这里详细介绍 go。
See for example: Is it possible to programmatically generate an X509 certificate using only C#?参见例如: 是否可以仅使用 C# 以编程方式生成 X509 证书?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.