简体   繁体   English

如何使用 .net 库生成强命名 SNK 密钥文件

[英]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.

相关问题 .NET 强名称 API 调用中的异常。 获取 ' 的关键信息时出错<my .snk file path> ': 未找到强名称密钥容器</my> - Exception in .NET strong name API invocation. Error while getting key info for '<my .snk file path>': Strong name key container not found 没有强命名的代码签名是否会让您的应用程序被滥用? - Does code-signing without strong-naming leave your app open to abuse? 如何导入在.NET中使用sn.exe生成的.snk文件? - How to import an .snk file generated with sn.exe in .NET? 每个项目是否应使用单独的强名称密钥(.snk)进行签名? - Should each project being signed with a separate Strong Name Key (.snk)? 如何修复错误“ CS7032密钥文件&#39;myfile.snk&#39;缺少签名所需的私钥 - How to fix error "CS7032 Key file 'myfile.snk' is missing the private key needed for signing .snk文件的位置及其管理 - location of .snk file and management of it 什么是强命名以及如何对二进制文件进行强命名? - What is strong naming and how do I strong name a binary? 如何区分两个具有相同名称的.snk文件? - How to differentiate between two .snk file having same name? .SNK文件也在哪里创建? - Where is the .SNK file created too? .snk是否同时包含私钥和公钥? - is .snk containing both private key and public key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM