要验证已签名的DLL是否由您签名,您可以使用Windows操作系统自带的工具“Authenticode签名工具”。以下是详细步骤:
<DLL文件路径>
替换为您要检查的DLL文件的路径。
3. 查看输出结果中的“Signature”一栏。如果DLL文件已签名,您将看到“Signed”字样。
4. 若DLL文件已签名,您可以查看“Signing Certificate”一栏,以确认签名者的身份。如果您需要使用编程方式验证DLL签名,您可以使用Windows API函数WinVerifyTrust
。以下是一个简单的示例代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
public class Authenticode
{
[DllImport("Wintrust.dll", ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)]
static extern bool WinVerifyTrust(IntPtr hwnd, Guid pgActionID, WinTrustData pWVTData);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct WinTrustData
{
public uint StructSize;
public IntPtr PolicyCallbackData;
public IntPtr SIPClientData;
public uint UIChoice;
public uint RevocationChecks;
public uint UnionChoice;
public IntPtr FileOrCatalogOrBlobOrSgnrOrCert;
public IntPtr StateAction;
public IntPtr StateData;
public string URLReference;
public uint ProviderFlags;
public uint UIContext;
public string SignatureSettings;
}
const int WINTRUST_ACTION_GENERIC_VERIFY_V2 = 102;
static bool Verify(string fileName)
{
WinTrustData wtd = new WinTrustData();
wtd.StructSize = (uint)Marshal.SizeOf(typeof(WinTrustData));
wtd.UnionChoice = 1;
wtd.FileOrCatalogOrBlobOrSgnrOrCert = Marshal.StringToHGlobalUni(fileName);
wtd.UIChoice = 2;
bool result = WinVerifyTrust(IntPtr.Zero, new Guid("{00AAC56B-CD44-11d0-8CC2-00C04FC295EE}"), wtd);
Marshal.FreeHGlobal(wtd.FileOrCatalogOrBlobOrSgnrOrCert);
return result;
}
static void Main(string[] args)
{
string dllPath = @"C:\path\to\your\dll.dll";
bool isVerified = Verify(dllPath);
if (isVerified)
{
Console.WriteLine("DLL is signed and verified.");
}
else
{
Console.WriteLine("DLL is not signed or verification failed.");
}
}
}
这个示例代码使用WinAPI函数WinVerifyTrust
来验证指定DLL文件的签名。如果验证成功,则返回true
,否则返回false
。
请注意,这个示例代码仅供参考,实际应用中可能需要根据具体需求进行修改和调整。
领取专属 10元无门槛券
手把手带您无忧上云