繁体   English   中英

PDF 签名 - 使用 ItextSharp 将时间戳记令牌设置为签名

[英]PDF Signature - Set TimeStamp token to Signature using ItextSharp

我目前正在开发一个 web 应用程序 (1),它允许将 pdf 文件的 hash 发送到另一个应用程序 (2)。 应用程序 (2) 返回一个签名的 hash。 我有一个时间戳令牌,它是另一个 API 的返回,我正在尝试将它添加到我的签名方法中,但我不知道怎么做

签名法

   public string GetHash(string AttachID, string InternalAttch_ID, string Certif, string AttachName, bool notPdfFile, bool hasPdf )
    {
      try
      {
         string fileName = ConfigurationManager.AppSettings["unsignedPdf"] + AttachID + ".pdf";
         string SignedFileName = ConfigurationManager.AppSettings["signedPdf"] + AttachID + "_Signed.pdf";
         string SignatureImg = ConfigurationManager.AppSettings["ImagePath"];
         Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[1];
         byte[] bytes = Convert.FromBase64String(Certif);
         var cert = new X509Certificate2(bytes);
         chain[0] = new Org.BouncyCastle.X509.X509CertificateParser().ReadCertificate(cert.GetRawCertData());       
         using (PdfReader reader = new PdfReader(fileName))
          {
            using (FileStream os = File.OpenWrite(SignedFileName))
             {
               PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0', null, true);   
               PdfSignatureAppearance appearance = null;
               appearance = stamper.SignatureAppearance;
               appearance.Reason = "Reason";
               appearance.Location = "location";
               appearance.SignDate = DateTime.Now.Date;
               appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(50, 100, 270, 270), 1, UserName); 
               appearance.IsInvisible();
               appearance.Acro6Layers = true;
               IExternalSignature signature = new RemoteSignature();
               MakeSignature.SignDetached(appearance, signature, chain, null, null, null, 0, CryptoStandard.CADES);
               stamper.Close();
               os.Close();
             }
          }
              return "GetHash successfully done";
      }
     catch (Exception e)
      {
         string err = e.Message;
         return null;
      }
    }
  internal class RemoteSignature : IExternalSignature
    {
      public virtual byte[] Sign(byte[] message)
       {
         IDigest messageDigest = DigestUtilities.GetDigest(GetHashAlgorithm());
         byte[] messageHash = DigestAlgorithms.Digest(messageDigest, message);      
         string urlEncoded = HttpUtility.UrlEncode(Convert.ToBase64String(messageHash));
         byte[] urlEncodedByte = Encoding.ASCII.GetBytes(urlEncoded);
         var i = 0;
         while (readySignHashFile != true)
          {
            i++;
          }
        return Convert.FromBase64String(signedHash);
       }
      public virtual String GetHashAlgorithm()
       {
         return "SHA-256";
       }

      public virtual String GetEncryptionAlgorithm()
       {
         return "RSA";
       }
    }

你打电话

MakeSignature.SignDetached(appearance, signature, chain, null, null, null, 0, CryptoStandard.CADES);

null参数之一是ITSAClient

要为您的签名添加时间戳,请提供非空ITSAClient ,例如TSAClientBouncyCastle实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM