【问题标题】:How to add TimesStamp to document (ITextSharp, BouncyCastle)如何将 TimesStamp 添加到文档(ITextSharp、BouncyCastle)
【发布时间】:2020-02-21 14:03:36
【问题描述】:

您好,我想使用 ItextSharp 将 TimeStamp 从权威服务器添加到 Pdf,但我不知道如何。我只能将时间戳添加到带有数字签名的 PDF 中,但我想在没有它的情况下添加。

当我从服务器获取时间戳时,它内部带有证书,但它没有要签名的主键,是否有其他方法可以在没有证书的情况下进行签名,或者我需要它,如果是,如何获取主键用它签署 PDF。

通过 Bouncy Castle 库获取时间戳记令牌

private static TimeStampToken Timestamp(string InputPath, string annexFileName, stringsignedFileName,string UserTs, string PassTs, string UrlTs)
    {

      FileStream stream = File.OpenRead(InputPath);

        SHA1Managed sha = new SHA1Managed();
        byte[] hashedFile = sha.ComputeHash(stream);
        TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
        reqGen.SetCertReq(true);

        TimeStampRequest request = reqGen.Generate(
            TspAlgorithms.Sha1, hashedFile, BigInteger.ValueOf(100));
      stream.Close();


      byte[] reqData = request.GetEncoded();
      ServicePointManager.Expect100Continue = true;
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 |
                                             SecurityProtocolType.Tls | SecurityProtocolType.Tls11;

      HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(UrlTs);
      httpReq.Method = "POST";
      httpReq.ContentType = "application/timestamp-query";
      httpReq.ContentLength = reqData.Length;
      httpReq.Credentials = new NetworkCredential(UserTs, PassTs);


      // Write the request content
      Stream reqStream = httpReq.GetRequestStream();
      reqStream.Write(reqData, 0, reqData.Length);
      reqStream.Close();

      HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

      // Read the response
      Stream respStream = new BufferedStream(httpResp.GetResponseStream());
      TimeStampResponse response = new TimeStampResponse(respStream);

      respStream.Close();
      response.Validate(request);
      TimeStampToken tsToken = response.TimeStampToken;

      Org.BouncyCastle.X509.Store.IX509Store x509Certs = tsToken.GetCertificates("Collection");
      ArrayList certs = new ArrayList(x509Certs.GetMatches(null));
      tsToken.Validate((Org.BouncyCastle.X509.X509Certificate)certs[0]);

      return tsToken;


    }

【问题讨论】:

  • 你的意思是私钥而不是主键?如果是这样,则只有签名者才能知道私钥-在这种情况下是时间戳权限。它永远不会包含在签名中。这里有一个概述,解释了数字签名的原理(我知道它不适用于 iText,但它强调了这些原理)。 link
  • 相关部分的标题是“签名、密钥和证书”
  • 是的,我的意思是主键。您发送的只是有关签名的基本内容。我已经有了功能性数字签名,但证书和时间戳结合在一起。如何在没有证书的情况下签署 PDF,而只需在 PDF 中添加时间戳。
  • 在文档签名中使用术语“主密钥”并不常见。我们使用签名证书的“私钥”进行签名。 PDF中有两种类型的时间戳。您可以传递 ASN.1 编码的签名 CMS 的字节以由时间戳服务器签名,并将其放入签名内容(用于时间戳签名);或者您可以仅对文档摘要添加时间戳,从而创建 DocTimestamp 签名。看起来您正在尝试添加 DocTimestamp。一个参考在 A2 here
  • iText7 确实有添加文档时间戳的方法,但看起来您想单独使用它,因此您需要手动创建一个签名表单字段及其对应的 DocTimeStamp 条目 - 在这种情况下,我会推荐 PDF 2.0 规范 (ISO 32000-2) - 遗憾的是不是免费的。或者您可以从上面的 ETSI 链接中找出答案。你的时间戳代码看起来很合理。要检查,您可以将返回的字节粘贴到 this webpage

标签: c# pdf timestamp itext bouncycastle


【解决方案1】:

谢谢大家的回答。 我已经尝试了很多方法来弄清楚它,但没有成功。 现在我正在使用 Syncfusion FileFormats Library,它运行良好......遗憾的是它不是免费的,但它可以提供很多关于文件的功能和安全性......

//Creates a new PDF document

PdfDocument document = new PdfDocument();

//Adds a new page

PdfPage page = document.Pages.Add();

//Creates a digital signature

PdfSignature signature = new PdfSignature(page, "Signature");

//Add the time stamp by using the server URI

signature.TimeStampServer = new TimeStampServer(new Uri("http://syncfusion.digistamp.com"), "user", "123456");

//Save and close the document

document.Save("Output.pdf");

document.Close(true);

这里是如何通过 Syncfusion 将时间戳添加到 Pdf 的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多