该页面可以包含自动翻译的文本。

在 C# 和 VB.NET 中验证 PDF 签名

PDF 文档中的数字签名可让您检测 PDF 是否被篡改。 您可以使用Docotic.Pdf库来验证PDF 文件中的数字签名。 该库可以帮助您读取 .NET Framework 和 .NET Core 应用程序中的签名属性。 在 下载 C# .NET PDF 库 页面获取库和免费限时许可证密钥。

验证 PDF 中的数字签名

使用 C# 验证 PDF 中的数字签名

Docotic.Pdf 库提供了检查文档的签名部分在签名后是否发生更改的方法。 您可以检查签名是否包含嵌入的 OCSP 和/或 CRL 数据。 对于任何签名,都可以检查其签名证书是否在任何给定日期被撤销。

Docotic.Pdf 库 9.5.17573-dev 回归测试 14,726 通过 NuGet 总下载量 4,765,230

以下 C# 示例展示了如何验证 PDF 签名并检查签名证书的吊销:

using BitMiracle.Docotic.Pdf;

using (var pdf = new PdfDocument("signed.pdf"))
{
    PdfControl field = pdf.GetControls().FirstOrDefault(c => c.Type == PdfWidgetType.Signature);
    if (field == null)
    {
        Console.WriteLine("Document does not contain signature fields", "Verification result");
        return;
    }

    PdfSignature signature = ((PdfSignatureField)field).Signature;
    PdfSignatureContents contents = signature.Contents;
    Console.WriteLine("Signed part is intact: {0}", contents.VerifyDigest());

    DateTime signingTime = signature.SigningTime ?? DateTime.MinValue;
    Console.WriteLine("Signed on: {0}\n", signingTime.ToShortDateString());

    if (contents.CheckHasEmbeddedOcsp())
    {
        Console.WriteLine("Signature has OCSP embedded.");
        checkRevocation(signature, PdfCertificateRevocationCheckMode.EmbeddedOcsp);
    }

    if (contents.CheckHasEmbeddedCrl())
    {
        Console.WriteLine("Signature has CRL embedded.");
        checkRevocation(signature, PdfCertificateRevocationCheckMode.EmbeddedCrl);
    }

    checkRevocation(signature, PdfCertificateRevocationCheckMode.OnlineOcsp);
    checkRevocation(signature, PdfCertificateRevocationCheckMode.OnlineCrl);

    if (contents.Timestamp != null)
    {
        Console.WriteLine("Signature has timestamp embedded.");
        Console.WriteLine("Timestamp: {0}", contents.Timestamp);
        Console.WriteLine("Timestamp is intact: {0}", contents.VerifyTimestamp());
    }
}

private static void checkRevocation(PdfSignature signature, PdfCertificateRevocationCheckMode mode)
{
    PdfSignatureContents contents = signature.Contents;
    DateTime signingTime = signature.SigningTime ?? DateTime.MinValue;

    foreach (DateTime time in new DateTime[] { signingTime, DateTime.UtcNow })
    {
        bool revoked = contents.CheckIfRevoked(mode, time);
        string status = revoked ? "Revoked" : "Valid";
        string date = time.ToShortDateString();
        Console.WriteLine("Checking using {0} mode: {1} on {2}", mode, status, date);
    }
}

在 GitHub 上,您可以下载并尝试使用 C# 和 VB.NET 编写的完整验证 PDF 签名 示例。

在 C# 中读取 PDF 签名属性

Docotic.Pdf 可以读取签名属性,例如签名者姓名、签名时间、签名者联系信息。 还可以访问签名中嵌入的签名 证书。 如果签名证书嵌入在签名中,则库还可以检索签名证书的颁发者证书。

此示例演示如何在 C# 中读取签名和相应的证书属性:

using (var pdf = new PdfDocument("signed.pdf"))
{
    PdfControl control = pdf.GetControls().FirstOrDefault(c => c.Type == PdfWidgetType.Signature);
    if (control == null)
    {
        Console.WriteLine("Document does not contain signature fields");
        return;
    }

    PdfSignatureField field = (PdfSignatureField)control;
    Console.WriteLine("Signature field is invisible: {0}", isInvisible(field));

    PdfSignature signature = field.Signature;
    Console.WriteLine("Signed by: {0}", signature.Name);
    Console.WriteLine("Signing time: {0}", signature.SigningTime);
    Console.WriteLine("Signed at: {0}", signature.Location);
    Console.WriteLine("Reason for signing: {0}", signature.Reason);
    Console.WriteLine("Signer's contact: {0}", signature.ContactInfo);

    PdfSignatureContents contents = signature.Contents;
    Console.WriteLine("Has OCSP embedded: {0}", contents.CheckHasEmbeddedOcsp());
    Console.WriteLine("Has CRL embedded: {0}", contents.CheckHasEmbeddedCrl());

    PdfSignatureCertificate certificate = contents.GetSigningCertificate();
    Console.WriteLine("== Signing certificate:");
    Console.WriteLine("Name: {0}", certificate.Name);
    Console.WriteLine("Algorithm: {0}", certificate.AlgorithmName);
    Console.WriteLine("Subject DN: {0}", certificate.Subject.Name);
    Console.WriteLine("Issuer DN: {0}", certificate.Issuer.Name);
    Console.WriteLine("Serial number: {0}", certificate.SerialNumber);
    Console.WriteLine("Valid from {0} up to {1}", certificate.ValidFrom, certificate.ValidUpto);
    Console.WriteLine("Timestamp Authority URL: {0}", certificate.GetTimestampAuthorityUrl());

    PdfSignatureCertificate issuer = contents.GetIssuerCertificateFor(certificate);
    Console.WriteLine("== Issuer certificate:");
    Console.WriteLine("Subject DN: {0}", issuer.Subject.Name);
    Console.WriteLine("Issuer DN: {0}", issuer.Issuer.Name);
    Console.WriteLine("Serial number: {0}", issuer.SerialNumber);
}

private static bool isInvisible(PdfSignatureField field)
{
    return (field.Width == 0 && field.Height == 0) ||
            field.Flags.HasFlag(PdfWidgetFlags.Hidden) ||
            field.Flags.HasFlag(PdfWidgetFlags.NoView);
}

从 GitHub 下载并尝试完整的在 C# 和 VB.NET 中读取 PDF 签名属性 示例。