Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. ////Con el método de abajo abro el almacén de certificados y selecciono uno para firmar.
  2.  
  3. private void btnExplorar_Click(object sender, EventArgs e)
  4. {
  5. X509Store store= new X509Store(StoreName.My,StoreLocation.CurrentUser);
  6.  
  7.  
  8. store.Open(OpenFlags.ReadOnly);
  9.  
  10. X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
  11. X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
  12.  
  13. X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Lista de Certificados digitales instalados en su equipo", "Seleccion el certificado con el que desea firmar", X509SelectionFlag.SingleSelection);
  14. // X509Certificate2 certificadoElegido = scollection[0];
  15.  
  16. if (scollection.Count > 0)
  17. {
  18.  
  19. certificadoElegido = scollection[0];//certificadoElegido es vble de clase
  20.  
  21. }
  22.  
  23. //Con este método recorro un listbox con la lista de todos los PDF seleccionados para firmar e intento crear un campo, pero no sé como lograr que ese campo se cree en el PDF y hacer la firma. Necesito por supuesto que esa firma se vea luego en el PDF para que los solicitantes sepan quién se los firmó.
  24.  
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. if (cLBoxListadePdf.CheckedItems.Count!=0) {
  28. string pdfAFirmar;
  29. int i;
  30. for (i=0;i<=cLBoxListadePdf.CheckedItems.Count-1;i++) {
  31. pdfAFirmar = cLBoxListadePdf.CheckedItems[i].ToString();
  32.  
  33. PdfReader pdf = new PdfReader(pdfAFirmar);
  34. PdfStamper stamper = PdfStamper.CreateSignature(pdf,new FileStream("C:\Users...", FileMode.Create), '');
  35. PdfSignatureAppearance sap = stamper.SignatureAppearance;
  36. sap.Reason = "";
  37. //sap.Location=100;
  38. stamper.Close();
  39. }
  40. }
  41. }
  42.  
  43. using System;
  44. using System.Collections.Generic;
  45. using System.Linq;
  46. using System.Text;
  47. using System.Threading.Tasks;
  48. using System.Collections;
  49. using System.IO;
  50. using System.Security.Cryptography;
  51. using System.Security.Cryptography.Pkcs;
  52. using iTextSharp.text;
  53. using iTextSharp.text.pdf;
  54. using iTextSharp.text.pdf.security;
  55.  
  56. using Org.BouncyCastle.X509;
  57.  
  58. using SysX509 = System.Security.Cryptography.X509Certificates;
  59.  
  60.  
  61. namespace FirmaPDF
  62. {
  63. public class firma
  64. {
  65.  
  66. /// <summary>
  67. /// Firma un documento PDF
  68. /// </summary>
  69. /// <param name="Source">Path del PDF a firmar</param>
  70. /// <param name="Target">Path del PDF firmado</param>
  71. /// <param name="Certificate">Certificado para realizar la firma</param>
  72. /// <param name="Reason">Motivo</param>
  73. /// <param name="Location">Ubicación</param>
  74. /// <param name="AddVisibleSign">Indica si la firma es visible dentro del documento</param>
  75. /// <param name="AddTimeStamp">Indica si se va a añadir sello de tiempo en el documento</param>
  76. /// <param name="strTSA">TSA del sello de tiempo</param>
  77.  
  78. public static void SignHashed(string Source, string Target, SysX509.X509Certificate2 Certificate, string Reason, string Location, bool AddVisibleSign, bool AddTimeStamp, string strTSA)
  79. {
  80. X509CertificateParser objCP = new X509CertificateParser();
  81. X509Certificate[] objChain = new X509Certificate[] { objCP.ReadCertificate(Certificate.RawData) };
  82.  
  83. IList<ICrlClient> crlList = new List<ICrlClient>();
  84. crlList.Add(new CrlClientOnline(objChain));
  85.  
  86. PdfReader objReader = new PdfReader(Source);
  87. PdfStamper objStamper = PdfStamper.CreateSignature(objReader, new FileStream(Target, FileMode.Create), '',null,true);
  88.  
  89. // Creamos la apariencia
  90. PdfSignatureAppearance signatureAppearance = objStamper.SignatureAppearance;
  91. signatureAppearance.Reason = Reason;
  92. signatureAppearance.Location = Location;
  93.  
  94. // Si está la firma visible:
  95. if (AddVisibleSign)
  96. signatureAppearance.SetVisibleSignature(new Rectangle(100, 100, 300, 200), 1, null); //signatureAppearance.SetVisibleSignature(new Rectangle(100, 100, 250, 150), objReader.NumberOfPages, "Signature");
  97.  
  98. ITSAClient tsaClient = null;
  99. IOcspClient ocspClient = null;
  100.  
  101. // Si se ha añadido el sello de tiempo
  102. if (AddTimeStamp){
  103. ocspClient = new OcspClientBouncyCastle();
  104. tsaClient = new TSAClientBouncyCastle(strTSA);
  105. }
  106.  
  107. // Creating the signature
  108. IExternalSignature externalSignature = new X509Certificate2Signature(Certificate, "SHA-1");
  109. MakeSignature.SignDetached(signatureAppearance, externalSignature, objChain, crlList, ocspClient, tsaClient, 0, CryptoStandard.CMS);
  110.  
  111. if (objReader != null)
  112. objReader.Close();
  113. if (objStamper != null)
  114. objStamper.Close();
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement