Ver Mensaje Individual
  #102 (permalink)  
Antiguo 18/08/2010, 16:21
dcordova_Cedano
 
Fecha de Ingreso: agosto-2010
Mensajes: 1
Antigüedad: 14 años, 3 meses
Puntos: 0
Respuesta: Factura electroncia sat mexico

Todos ustedes han sido tan amables con todo lo que les eh aprendido, que no tengo mas que agradecer.

Y que mejor que con un pequeño aporte.

Tengo una forma de saber los datos del .Cer y aqui se las mando, aunque en relación al número de certificado solo me falta quitar de cada grupo de dos caracteres el de la izquierda.

ok. aqui los pasos en VB.Net 2005

Primeramente necesitamos una clase, la cual llamaremos CerInfo

Este es el codigo.

Imports System
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates
Imports Microsoft.VisualBasic




Public Class CertInfo



'Reads a file.
Friend Shared Function ReadFile(ByVal fileName As String) As Byte()
Dim f As New FileStream(fileName, FileMode.Open, FileAccess.Read)
Dim size As Integer = Fix(f.Length)
Dim data(size) As Byte
size = f.Read(data, 0, size)
f.Close()

Return data

End Function

<SecurityPermission(SecurityAction.LinkDemand, Unrestricted:=True)> _
Shared Sub Main2(ByVal args() As String)
'Test for correct number of arguments.
If args.Length < 1 Then
Console.WriteLine("Usage: CertInfo <filename>")
Return
End If
Try
Dim x509 As New X509Certificate2()

'Create X509Certificate2 object from .cer file.
Dim rawData As Byte() = ReadFile(args(0))

x509.Import(rawData)

'Print to console information contained in the certificate.
Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject)
Sujeto = x509.Subject
Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer)
Issuer = x509.Issuer

Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version)
Version = x509.Version
Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore)
NotBefore = x509.NotBefore
Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter)
NotAfter = x509.NotAfter

Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint)
Thumbprint = x509.Thumbprint
Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber)
SerialNumber = x509.SerialNumber
Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName)
FriendlyName = x509.FriendlyName
Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(True))
EncodedKeyValue = x509.PublicKey.EncodedKeyValue.Format(True)

Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length)
Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(True))
Certificate = x509.ToString(True)

Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(False))

'Add the certificate to a X509Store.
Dim store As New X509Store()
store.Open(OpenFlags.MaxAllowed)
store.Add(x509)
store.Close()

Catch dnfExcept As DirectoryNotFoundException
Console.WriteLine("Error: The directory specified could not be found.")
Catch ioExpcept As IOException
Console.WriteLine("Error: A file in the directory could not be accessed.")
Catch nrExcept As NullReferenceException
Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.")
End Try

End Sub


End Class


Perdon pero no sé como poner todo esto en un cuadro de dialogo.

Segundo

Necesitamos algunas variables publicas y las podemos declarar un un modulo al que llamaremos Certificados.vb

Module Certificados
Public Sujeto As String
Public Issuer As String
Public Version As Integer
Public NotBefore As Date
Public NotAfter As Date
Public Thumbprint As String
Public SerialNumber As String
Public FriendlyName As String
Public EncodedKeyValue As String
Public Certificate As String

End Module


Tercero

Necesiamos un control OpenFileDialog1

Cuarto

En nuestra forma debemos tener los siguientes controles de texto

Me.PathKey.Text
Me.Issuer1.Text
Me.Version1.Text
Me.NotBefore1.Text
Me.NotAfter1.Text
Me.Thumbprint1.Text
Me.FriendlyName1.Text
Me.EncodedKeyValue1.Text
Me.Certificate1.Text
Me.NumeroDeCertificado.Text


Quinto


Necesitamos un Botton y en el evento clik, pegaremos este codigo

With OpenFileDialog1

.CheckFileExists = True
.ShowReadOnly = False

.Filter = "All Files|*.cer"
.FilterIndex = 2
If .ShowDialog = System.Windows.Forms.DialogResult.OK Then
Me.PathCer.Text = .FileName


Dim MiArray() As String = {Me.PathCer.Text.Trim}
CertInfo.Main2(MiArray)
Me.PathKey.Text = Sujeto
Me.Issuer1.Text = Issuer
Me.Version1.Text = CStr(Version)
Me.NotBefore1.Text = CStr(NotBefore)
Me.NotAfter1.Text = CStr(NotAfter)
Me.Thumbprint1.Text = Thumbprint
Me.FriendlyName1.Text = FriendlyName
Me.EncodedKeyValue1.Text = EncodedKeyValue
Me.Certificate1.Text = Certificate
Me.NumeroDeCertificado.Text = SerialNumber




End With


Y listo