Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#以编程方式创建.SNK文件_C# - Fatal编程技术网

C#以编程方式创建.SNK文件

C#以编程方式创建.SNK文件,c#,C#,如何以编程方式创建.snk文件并将其保存到桌面? 除了如何从代码中创建.snk文件而不打开visual studio命令提示符之外,我什么都懂。但是可以单击表单上的按钮并生成桌面密钥。以下是一些可以创建有效.snk文件的C#实用程序代码: 示例用法: ... // write a .snk file StrongNameUtilities.GenerateKeyFile("test.snk"); // default key size is 1024 bits // reread an che

如何以编程方式创建
.snk
文件并将其保存到桌面? 除了如何从代码中创建
.snk
文件而不打开visual studio命令提示符之外,我什么都懂。但是可以单击表单上的按钮并生成桌面密钥。

以下是一些可以创建有效.snk文件的C#实用程序代码:

示例用法:

...
// write a .snk file
StrongNameUtilities.GenerateKeyFile("test.snk"); // default key size is 1024 bits

// reread an check it worked
var pk = StrongNameUtilities.GetPublicKey("test.snk");
...
公用事业代码:

public sealed class StrongNameUtilities
{
    public static byte[] GetPublicKeyToken(byte[] publicKey)
    {
        if (publicKey == null)
            throw new ArgumentNullException(nameof(publicKey));

        var blob = IntPtr.Zero;
        try
        {
            if (!StrongNameTokenFromPublicKey(publicKey, publicKey.Length, out blob, out var size))
                throw new Win32Exception(StrongNameErrorInfo());

            var bytes = new byte[size];
            Marshal.Copy(blob, bytes, 0, size);
            return bytes;
        }
        finally
        {
            if (blob != IntPtr.Zero)
            {
                StrongNameFreeBuffer(blob);
            }
        }
    }

    public static byte[] GetPublicKeyToken(string snkFile)
    {
        if (snkFile == null)
            throw new ArgumentNullException(nameof(snkFile));

        return GetPublicKeyToken(GetPublicKey(snkFile));
    }

    public static byte[] GetPublicKey(string snkFile)
    {
        if (snkFile == null)
            throw new ArgumentNullException(nameof(snkFile));

        var bytes = File.ReadAllBytes(snkFile);
        var blob = IntPtr.Zero;
        try
        {
            if (!StrongNameGetPublicKey(null, bytes, bytes.Length, out blob, out var size))
                throw new Win32Exception(StrongNameErrorInfo());

            var pkBlob = new byte[size];
            Marshal.Copy(blob, pkBlob, 0, size);
            return pkBlob;
        }
        finally
        {
            if (blob != IntPtr.Zero)
            {
                StrongNameFreeBuffer(blob);
            }
        }
    }

    public static void GenerateKeyFile(string snkFile, int keySizeInBits = 1024)
    {
        if (snkFile == null)
            throw new ArgumentNullException(nameof(snkFile));

        var bytes = GenerateKey(keySizeInBits);
        File.WriteAllBytes(snkFile, bytes);
    }

    public static byte[] GenerateKey(int keySizeInBits = 1024)
    {
        if (!StrongNameKeyGenEx(null, 0, keySizeInBits, out var blob, out var size))
            throw new Win32Exception(StrongNameErrorInfo());

        try
        {
            var bytes = new byte[size];
            Marshal.Copy(blob, bytes, 0, size);
            return bytes;
        }
        finally
        {
            if (blob != IntPtr.Zero)
            {
                StrongNameFreeBuffer(blob);
            }
        }
    }

    [DllImport("mscoree")]
    private extern static void StrongNameFreeBuffer(IntPtr pbMemory);

    [DllImport("mscoree", CharSet = CharSet.Unicode)]
    private static extern bool StrongNameGetPublicKey(
        string szKeyContainer,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbKeyBlob,
        int cbKeyBlob,
        out IntPtr ppbPublicKeyBlob,
        out int pcbPublicKeyBlob);

    [DllImport("mscoree")]
    private static extern bool StrongNameTokenFromPublicKey(
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pbPublicKeyBlob,
        int cbPublicKeyBlob,
        out IntPtr ppbStrongNameToken,
        out int pcbStrongNameToken);

    [DllImport("mscoree", CharSet = CharSet.Unicode)]
    private static extern bool StrongNameKeyGenEx(string wszKeyContainer, int dwFlags, int dwKeySize, out IntPtr ppbKeyBlob, out int pcbKeyBlob);

    [DllImport("mscoree")]
    private static extern int StrongNameErrorInfo();
}

看一看:它可能会给你一些与指针相关的信息,可能是dup@enkryptor,或者我已经看过了,但代码不起作用。@Swoomi介意在问题中添加细节吗?