.NET+iText移动端签名问题:PDF提示文档已篡改
问题描述
我使用.NET结合iText开发移动端签名应用时遇到问题:将签名添加至PDF文件后,文档提示“已被篡改或损坏”。我可以从响应XML中解析出用户证书与Base64格式的签名值,但通过iText将它们添加到PDF时,仍会出现该错误提示。
相关代码
public Envelope Imzala() { Byte[] bytes = null; PdfReader pdfReader = null; FileInfo fInfo = new FileInfo(@"C:\mobilimza\imzasiz.pdf"); byte[] hashValue = null; using (SHA256 mySHA256 = SHA256.Create()) { using (FileStream fileStream = fInfo.Open(FileMode.Open)) { pdfReader = new PdfReader(fileStream); try { fileStream.Position = 0; hashValue = mySHA256.ComputeHash(fileStream); } catch (IOException e) { Console.WriteLine($"I/O Exception: {e.Message}"); } } } bytes = hashValue; DataToBeSigned = Convert.ToBase64String(bytes); string soapResult = string.Empty; XmlDocument responseXML = new XmlDocument(); try { WebRequest webRequest = CreateWebRequest(GlobalParameters.imzaServisLink); XmlDocument soapEnvelopeXml = new XmlDocument(); string xml1 = @""// my sign request; soapEnvelopeXml.LoadXml(xml1); using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } using (WebResponse response = webRequest.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { soapResult = rd.ReadToEnd(); Envelope envelope; var mySerializer = new XmlSerializer(typeof(Envelope)); using (TextReader reader = new StringReader(soapResult)) { envelope = (Envelope)mySerializer.Deserialize(reader); string base64sign = envelope.Body.MSS_SignatureResponse.MSS_SignatureResp.MSS_Signature.Base64Signature; byte[] data = Convert.FromBase64String(base64sign); string decodedText = "<Root>" + Encoding.UTF8.GetString(data) + "</Root>"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(decodedText); XmlElement keyInfoElement = (XmlElement)xmlDoc.SelectSingleNode("//KeyInfo"); XmlElement x509CertificateElement = (XmlElement)keyInfoElement.SelectSingleNode(".//X509Certificate"); XmlElement signvalue = (XmlElement)xmlDoc.SelectSingleNode(".//SignatureValue"); if (x509CertificateElement != null) { string x509CertificateText = x509CertificateElement.InnerText; string signText = signvalue.InnerText; byte[] x509data = Convert.FromBase64String(x509CertificateText); X509Certificate2 x509 = new(x509data); PdfWriter pdfWriter = new PdfWriter(@"c:\mobilimza\imzali.pdf"); StampingProperties sp = new StampingProperties(); sp.UseAppendMode(); PdfSigner pdfSigner = new PdfSigner(pdfReader, pdfWriter, sp); PdfSignatureAppearance appearance = pdfSigner.GetSignatureAppearance(); //pdfSigner.SetFieldName("Signature"); //appearance.SetPageNumber(1); // Set the X.509 certificate chain X509CertificateParser parser = new X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { DotNetUtilities.FromX509Certificate(x509) }; pdfSigner.SignDetached(new CustomExternalSignature1(Convert.FromBase64String(signText)), chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS); } return envelope; } } } } catch (Exception ee) { throw; } }
错误现象
PDF文档打开后提示“已被篡改或损坏”
内容的提问来源于stack exchange,提问作者tolgahan özden
相关产品推荐
相关产品推荐

