public MasFacturacion.StampingConnector.StampingService.comprobante EncryptPFX(MasFacturacion.StampingConnector.StampingService.comprobante cfd, TypeCryptoServiceProvider Algorithm, byte[] pfx, string Password) { string pathXSLT = ConfigurationManager.AppSettings["pathSATFiles"] + @"files SAT/localxslt/cadenaoriginal_3_2.xslt"; string xmlComprobante = Serializer.SerializeComprobante(cfd); if (null == pfx) { throw new Exception("No existe el certificado de la empresa"); } try { X509Certificate2 certX509 = default(X509Certificate2); RSACryptoServiceProvider rsa = default(RSACryptoServiceProvider); SHA1CryptoServiceProvider cspSha1 = default(SHA1CryptoServiceProvider); MD5CryptoServiceProvider cspMd5 = default(MD5CryptoServiceProvider); NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; byte[] bytesFirmados = default(byte[]); nfi.NumberDecimalDigits = 1; certX509 = new X509Certificate2(pfx, Password, X509KeyStorageFlags.MachineKeySet); string strOriginal = MasFacturacion.StampingConnector.Utilities.Util.generaCadenaOriginal(pathXSLT, xmlComprobante); rsa = (RSACryptoServiceProvider)certX509.PrivateKey; byte[] data = Encoding.UTF8.GetBytes(strOriginal); switch (Algorithm) { case TypeCryptoServiceProvider.MD5: cspMd5 = new MD5CryptoServiceProvider(); bytesFirmados = rsa.SignData(data, cspMd5); break; case TypeCryptoServiceProvider.SHA1: cspSha1 = new SHA1CryptoServiceProvider(); bytesFirmados = rsa.SignData(data, cspSha1); break; } cfd.sello = Convert.ToBase64String(bytesFirmados); cfd.noCertificado = hexString2Ascii(certX509.SerialNumber); // cfd.version = certX509.Version.ToString("N", nfi); cfd.certificado = Convert.ToBase64String(certX509.GetRawCertData()); } catch (Exception) { throw new Exception("No se puede generar el sello"); } return cfd; }
public MasFacturacion.StampingConnector.StampingService.comprobante EncryptPFX(MasFacturacion.StampingConnector.StampingService.comprobante cfd, TypeCryptoServiceProvider Algorithm, byte[] pfx, string Password) { string pathXSLT = ConfigurationManager.AppSettings["pathSATFiles"] + @"files SAT/localxslt/cadenaoriginal_3_2.xslt"; string xmlComprobante = Serializer.SerializeComprobante(cfd); if (null == pfx) { throw new Exception("No existe el certificado de la empresa"); } try { X509Certificate2 certX509 = default(X509Certificate2); RSACryptoServiceProvider rsa = default(RSACryptoServiceProvider); SHA1CryptoServiceProvider cspSha1 = default(SHA1CryptoServiceProvider); MD5CryptoServiceProvider cspMd5 = default(MD5CryptoServiceProvider); NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; byte[] bytesFirmados = default(byte[]); nfi.NumberDecimalDigits = 1; certX509 = new X509Certificate2(pfx, Password, X509KeyStorageFlags.MachineKeySet); string strOriginal = MasFacturacion.StampingConnector.Utilities.Util.generaCadenaOriginal(pathXSLT, xmlComprobante); rsa = (RSACryptoServiceProvider)certX509.PrivateKey; byte[] data = Encoding.UTF8.GetBytes(strOriginal); switch (Algorithm) { case TypeCryptoServiceProvider.MD5: cspMd5 = new MD5CryptoServiceProvider(); bytesFirmados = rsa.SignData(data, cspMd5); break; case TypeCryptoServiceProvider.SHA1: cspSha1 = new SHA1CryptoServiceProvider(); bytesFirmados = rsa.SignData(data, cspSha1); break; } cfd.sello = Convert.ToBase64String(bytesFirmados); cfd.noCertificado = hexString2Ascii(certX509.SerialNumber); // cfd.version = certX509.Version.ToString("N", nfi); cfd.certificado = Convert.ToBase64String(certX509.GetRawCertData()); } catch (Exception) { throw new Exception("No se puede generar el sello"); } return(cfd); }