如何检查签名的 C# DLL 的完整性?

How to check the integrity of a signed C# DLL?

我有一个签名的 C# Dll,我想测试它的真实性和文件完整性。 到目前为止我所做的是获取证书并调用在该证书上找到的 Verify 方法:

public ValidatorResult CheckDll()
{
    X509Certificate cert = null;
    try
    {
       cert = X509Certificate.CreateFromSignedFile(dllFilepath);
    }
    catch (CryptographicException)
    {
       return new ValidatorResult(ValidatorResult.DllStatus.INVALID_SIGNATURE);
    }

    if (cert == null || !ValidateCertificate(cert))
    {
       return new ValidatorResult(ValidatorResult.DllStatus.INVALID_SIGNATURE);
    }
}   

private bool ValidateCertificate(X509Certificate cert)
{
    var chain = new X509Chain();

    /* Do a chain verification */
    var primaryCert = new X509Certificate2(cert.GetRawCertData());
    if (!chain.Build(primaryCert))
       return false;

    /* Call The Verify method on the newer X509Certificate2 class */
    if (!primaryCert.Verify())
       return false;

    return true;
}

我可能是错的,所以请纠正我,但从我在互联网上看到的 Verify() 方法只检查证书的有效性。我还需要检查文件的完整性。

我使用 "Resource Hacker" 工具做了一个小测试,我加载了 dll 并更改了公司名称。这个修改后的dll通过了上面的代码。

我应该如何更改代码以便检查文件是否被修改?

谢谢。

使用此代码验证 DLL

[DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
public static extern bool StrongNameSignatureVerificationEx(
        string wszFilePath, bool fForceVerification, ref bool pfWasVerified);    

bool pfWasVerified = false;
if (!StrongNameSignatureVerificationEx("path...to...DLL", true, ref pfWasVerified))
{           
    // it's a modified DLL file!   
}