【问题标题】:How to import an .snk file generated with sn.exe in .NET?如何在 .NET 中导入使用 sn.exe 生成的 .snk 文件?
【发布时间】:2023-03-06 18:54:01
【问题描述】:
我已经用这个命令创建了一个密钥文件:
sn.exe -k 2048 Build.snk
我想用 .NET 读入这个密钥。我找不到任何关于 .snk 格式的文档,所以我想知道 C# 中是否有办法读取 .snk 文件?
我提出这个问题的最初原因是将 .snk 文件用于签署程序集以外的目的。正如下面的答案之一所述,.snk 文件的目的实际上只是用于签署程序集。
【问题讨论】:
标签:
c#
.net
encryption
sn.exe
【解决方案1】:
在 MSDN here 中找到了提及。代码如下:
public static void Main()
{
// Open a file that contains a public key value. The line below
// assumes that the Strong Name tool (SN.exe) was executed from
// a command prompt as follows:
// SN.exe -k C:\Company.keys
using (FileStream fs = File.Open("C:\\Company.keys", FileMode.Open))
{
// Construct a StrongNameKeyPair object. This object should obtain
// the public key from the Company.keys file.
StrongNameKeyPair k = new StrongNameKeyPair(fs);
// Display the bytes that make up the public key.
Console.WriteLine(BitConverter.ToString(k.PublicKey));
// Close the file.
fs.Close();
}
}