該頁面可以包含自動翻譯的文字。

在 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.17664-dev 回歸測試 14,820 已通過 NuGet 總下載量 4,998,853

以下 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 簽名屬性 範例。