如何将TimesStamp添加到文档(ITextSharp,BouncyCastle)

问题描述 投票:0回答:1

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

当我从服务器获取TimeStamp时,它内部带有证书,但是它没有主键进行签名,还有其他方法可以在没有证书的情况下进行签名,或者我需要它,如果是,如何获取主键来用它签名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;


    }
c# pdf timestamp itext bouncycastle
1个回答
0
投票

谢谢大家的回答。我尝试了很多方法来解决这个问题,但是没有成功。现在,我正在使用Syncfusion FileFormats库,它可以完美运行...并不是免费的,但是它可以提供很多有关文件的功能,并且具有安全性。

//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将TimeStamp添加到Pdf的示例。

© www.soinside.com 2019 - 2024. All rights reserved.