Alexey 提供的答案对我不起作用,我最终使用了此页面的源代码:
https://msdn.microsoft.com/en-us/library/windows/hardware/mt674914%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
我不得不做一些额外的调整:
在使用它之前,请确保 EV 证书在您的个人证书存储中。在您拥有的 USB 令牌工具中,您应该能够打开证书并选择“安装证书”。
在 Visual Studio 中创建一个新的控制台应用程序并将此源代码粘贴到其中。安装 nugget 包“WindowsBase”以获取 System.IO.Packaging 命名空间。
使用一些额外的源代码,我们可以让这个工作:
class Program
{
static void Main(string[] args)
{
X509Store store = new X509Store("My");
store.Open(OpenFlags.ReadOnly);
X509Certificate2 evCert = null;
foreach (X509Certificate2 mCert in store.Certificates)
{
if (mCert.Thumbprint == "3DF652D7EyourThumbprintF")
{
evCert = mCert;
}
}
Sign(@"C:\Path\To\Your\HLKXFile.hlkx", evCert);
}
public static void Sign(string package, X509Certificate2 certificate)
{
// Open the package to sign it
Package packageToSign = Package.Open(package);
// Specify that the digital signature should exist
// embedded in the signature part
PackageDigitalSignatureManager signatureManager = new PackageDigitalSignatureManager(packageToSign);
signatureManager.CertificateOption = CertificateEmbeddingOption.InCertificatePart;
// We want to sign every part in the package
List<Uri> partsToSign = new List<Uri>();
foreach (PackagePart part in packageToSign.GetParts())
{
partsToSign.Add(part.Uri);
}
// We will sign every relationship by type
// This will mean the signature is invalidated if *anything* is modified in //the package post-signing
List<PackageRelationshipSelector> relationshipSelectors = new List<PackageRelationshipSelector>();
foreach (PackageRelationship relationship in packageToSign.GetRelationships())
{
relationshipSelectors.Add(new PackageRelationshipSelector(relationship.SourceUri, PackageRelationshipSelectorType.Type, relationship.RelationshipType));
}
try
{
signatureManager.Sign(partsToSign, certificate, relationshipSelectors);
}
finally
{
packageToSign.Close();
}
}
}
将指纹替换为您的 EV 证书 SHA1。