首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Aspose PDF和Azure key Vault上的外部私钥对PDF进行签名

使用Aspose PDF和Azure key Vault上的外部私钥对PDF进行签名
EN

Stack Overflow用户
提问于 2018-11-26 21:25:45
回答 1查看 454关注 0票数 2

我正在尝试通过使用外部设备实际执行签名来使用Aspose PDF对Pdf进行数字签名,在本例中为Azure Key Vault。iText对此有一个非常好的机制。他们提供的IExternalSignature接口,您可以实现提供签名功能,但我找不到任何类似的Aspose Pdf。

我正在使用这篇博客文章中的示例:https://rahulpnath.com/blog/signing-a-pdf-file-using-azure-key-vault/

有人知道如何使用Aspose Pdf实现第三个示例(不可导出证书)吗?

EN

回答 1

Stack Overflow用户

发布于 2019-03-01 05:38:13

您可以使用提供X509Certificate2ExternalSignature对象对文档进行签名。请使用以下代码片段。在这些示例中,Windows证书存储用于获取用于签名的证书:

代码语言:javascript
运行
复制
// The System.Security.dll assembly should be added into References

// Signing 1. Using SignatureField
public void Sign_With_SmartCard_1()
{
    const string dataDir = @"c:\";

    File.Copy(dataDir + "blank.pdf", dataDir + "externalSignature1.pdf", true);
    using (FileStream fs = new FileStream(dataDir + "externalSignature1.pdf", FileMode.Open, FileAccess.ReadWrite))
    {
        using (Document doc = new Document(fs))
        {
            SignatureField field1 = new SignatureField(doc.Pages[1], new Rectangle(100, 400, 10, 10));

            // Sign with certificate selection in the windows certificate store
            X509Store store = new X509Store(StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            // Manually chose the certificate in the store
            X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);

            Aspose.Pdf.Forms.ExternalSignature externalSignature = new Forms.ExternalSignature(sel[0])
            {
                Authority = "Me",
                Reason = "Reason",
                ContactInfo = "Contact"
            };

            field1.PartialName = "sig1";
            doc.Form.Add(field1, 1);
            field1.Sign(externalSignature);
            doc.Save();
        }
    }

    using (PdfFileSignature pdfSign = new PdfFileSignature(dataDir + "externalSignature1.pdf"))
    {
        IList<string> sigNames = pdfSign.GetSignNames();
        for (int index = 0; index <= sigNames.Count - 1; index++)
        {
            if (!pdfSign.VerifySigned(sigNames[index]) || !pdfSign.VerifySignature(sigNames[index]))
            {
                throw new ApplicationException("Not verified");
            }
        }
    }
}

// Signing 2. Using PdfFileSignature
public void Sign_With_SmartCard_2()
{
    const string dataDir = @"c:\";

    Document doc = new Document(dataDir + "blank.pdf");

    using (PdfFileSignature pdfSign = new PdfFileSignature())
    {
        pdfSign.BindPdf(doc);

        //Sign with certificate selection in the windows certificate store
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        //manually chose the certificate in the store
        X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);

        Aspose.Pdf.Forms.ExternalSignature externalSignature = new Forms.ExternalSignature(sel[0]);
        pdfSign.SignatureAppearance = dataDir + "demo.png";
        pdfSign.Sign(1, "Reason", "Contact", "Location", true, new System.Drawing.Rectangle(100, 100, 200, 200), externalSignature);
        pdfSign.Save(dataDir + "externalSignature2.pdf");
    }

    using (PdfFileSignature pdfSign = new PdfFileSignature(dataDir + "externalSignature2.pdf"))
    {
        IList<string> sigNames = pdfSign.GetSignNames();
        for (int index = 0; index <= sigNames.Count - 1; index++)
        {
            if (!pdfSign.VerifySigned(sigNames[index]) || !pdfSign.VerifySignature(sigNames[index]))
            {
                throw new ApplicationException("Not verified");
            }
        }
    }
}

我们希望这将是有帮助的。如果您需要进一步的帮助,请随时与我们联系。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53482131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档