4

Desarrollo una aplicacion Pc/Sc , establesco el contexto, la conexion y obtengo el ATR perfectamente, pero cuando inicio la trasmision en vez de devolverme (SW1=90 SW2=00) me devuelve (SW1=69 SW2=00) CodigoSnipe

     class MaxSmartCard 
{
    private UInt32 LONGITUD = 0xFF;
    private const UInt32 T0 = 0x01; private const UInt32 T1 = 0x02 ; 
    private const UInt32 EXCLUSIVE = 0x01; private const UInt32 SHARED = 0x02; 
    private SCARD_IO_REQUEST request = new SCARD_IO_REQUEST();

    [DllImport("Winscard.dll")]
    private extern static int SCardTransmit(
      [In] IntPtr cardHandle, 
      IntPtr sendPci,
      Byte [] sendBuffer,
      UInt32 sbLength,
      [In, Out]  SCARD_IO_REQUEST recvPci, 
      [In, Out] Byte[] recvBuffer,
      [In, Out] UInt32 rbLength);

    [DllImport("kernel32.dll", SetLastError = true)]
    private extern static IntPtr LoadLibrary(string lpFileName);

    [DllImport("kernel32.dll")]
    private extern static void FreeLibrary(IntPtr handle);

    [DllImport("kernel32.dll")]
    private extern static IntPtr GetProcAddress(IntPtr handle, string procName);

    [StructLayout(LayoutKind.Sequential)]
     public struct SCARD_IO_REQUEST
     {
        public UInt32 pciProt; public UInt32 pciLen;
        public SCARD_IO_REQUEST (UInt32 a, UInt32 b)
        { this.pciProt = a; this.pciLen = b; }
     }

    public MaxSmartCard() { this.request = new SCARD_IO_REQUEST(0x00,0xFF); }

    public void EstablecerContexto() { this.rContex = SCardEstablishContext(0x00, IntPtr.Zero, IntPtr.Zero, out this.hContext); }

    public void InformacionLectores()
    {
        UInt32 _pcchReaders = 0;
        this.rReader = SCardListReaders(this.hContext, null, null, ref _pcchReaders);
        Byte[] byteLectores = new Byte[_pcchReaders];
        this.rReader2 = SCardListReaders(this.hContext, null, byteLectores, ref _pcchReaders);
        ASCIIEncoding ascii = new ASCIIEncoding();
        this.miLector = ascii.GetString(byteLectores);
    }

    public void EstablecerConexion() { this.rConnect = SCardConnect(this.hContext, miLector, EXCLUSIVE,T1, ref this.hContext, ref ActiveProtocol); }

    public void AtributosCard()
    {
        this.vectorAttr = new Byte[22];
        IntPtr pcbAttrLen = new IntPtr(vectorAttr.Length);
        this.rAtributo = SCardGetAttrib(this.hContext, SCARD_ATTR_ATR_STRING, vectorAttr, ref pcbAttrLen);
    }

    public void Trasmision()
    {
        Byte[] vectorSend = { 0x00, 0xA4, 0x04, 0x00, 0x00};
        UInt32 inT2 = (UInt32)vectorSend.Length; 
        vectorIn = new Byte[2];
        UInt32 inT = (UInt32)vectorIn.Length;
        IntPtr IOPCI = GetPciT1();
        this.rTransmit = SCardTransmit(this.hContext, IOPCI, vectorSend, inT2,  request, vectorIn, inT);

    }
   public IntPtr GetPciT1()
    {
        IntPtr handle = LoadLibrary("Winscard.dll");
        IntPtr pci = GetProcAddress(handle, "g_rgSCardT1Pci");
        FreeLibrary(handle);
        return pci;
    }
}

y desde este boton invoco los metodos de la clase

  private void button9_Click(object sender, EventArgs e)
    {
        Byte[] v; Int64 Exito = 0X00;
        textBox1.Text = "";
        Metodo.EstablecerContexto(); if (Metodo.RContext() != Exito) { MessageBox.Show("CONTEXTO NO ESTABLECIDO, ERROR " + Metodo.RContext().ToString("X2"), "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        Metodo.InformacionLectores();
        if (Metodo.RReader() != Exito) { MessageBox.Show("1era LLAMADA AL LECTOR FALLIDA", "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        if (Metodo.RReader2() != Exito) { MessageBox.Show("2da LLAMADA AL LECTOR FALLIDA", "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        textBox1.Text = "NOMBRE DEL LECTOR  " + Metodo.MiLector();
        Metodo.EstablecerConexion(); if (Metodo.RConnect() != Exito) { MessageBox.Show("CONEXION NO ESTABLECIDA, ERROR " + Metodo.RConnect().ToString("X2"), "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        Metodo.AtributosCard(); if (Metodo.RAtributo() != Exito) { MessageBox.Show("OBTENCION DE ATRIBUTO FALLIDA, ERROR " + Metodo.RAtributo().ToString("X2"), "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        v = Metodo.VectorAtributo();
        textBox1.Text = textBox1.Text + Environment.NewLine + "ATR   ";
        for (int i = 0; i <= v.Length - 1; i++) { textBox1.Text = textBox1.Text + v[i].ToString("X2") + " "; }
        textBox1.Text = textBox1.Text + Environment.NewLine + "MODELO TARJETA   " + Metodo.CadenaHexArray(v, false);
        Metodo.Trasmision(); if (Metodo.RTransmit() != Exito) { MessageBox.Show("TRASMISION FALLIDA, ERROR " + Metodo.RTransmit().ToString("X2"), "INFORMACION DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        v = Metodo.VectorSalida();
        textBox1.Text = textBox1.Text + Environment.NewLine + "SW1=" + v[0].ToString("X2") + "  " + "SW2=" + v[1].ToString("X2");
        Metodo.Desconectar();
    }

introducir la descripción de la imagen aquí

3
  • luna duda, porque el 69 no es un valor correcto ? o sea obtienes una respuesta pero quien determina cual es el valor de respuesta valido el 16 may. 2016 a las 13:45
  • porque programas el if en una sola linea sin dejar un salto, la verdad se hace complejo de seguir el codigo el 16 may. 2016 a las 13:46
  • 1
    hola El valor de respuesta valido es SW1=90 , SW2=00 esta normalizado ISO....los if solo importarian si me devolviera un error el cual no es el caso el 16 may. 2016 a las 13:56

1 respuesta 1

1

Para enviar n bytes de datos a la tarjeta, donde n > 0, los buffers de envío y recepción deben formatearse:

  • Primeros cuatro bytes de pbSendBuffer contienen los valores CLA, INS, P1 y P2 para la operación T = 0.

  • El quinto byte se debe establecer en n (tamaño, en bytes, de los datos que se van a transferir a la tarjeta). Los siguientes n bytes deben contener los datos que se enviarán a la tarjeta.

cbSendLength debe ajustarse al tamaño de la información de encabezado T = 0 (CLA, INS, P1 y P2) más un byte que contiene la longitud de los datos que se van a transferir (n), más el tamaño de los datos a ser expedido. En este ejemplo, esto es n + 5.

pbRecvBuffer recibe los códigos de estado SW1 y SW2 de la operación.

pcbRecvLength debe ser al menos dos y se establecerá en dos al

LONG WINAPI SCardTransmit(
    _In_        SCARDHANDLE         hCard,
      _In_        LPCSCARD_IO_REQUEST pioSendPci,
      _In_        LPCBYTE             pbSendBuffer,
      _In_        DWORD               cbSendLength,
      _Inout_opt_ LPSCARD_IO_REQUEST  pioRecvPci,
      _Out_       LPBYTE              pbRecvBuffer,
      _Inout_     LPDWORD             pcbRecvLength
);

La llamada quedaría:

lReturn = SCardTransmit(hCardHandle,
    SCARD_PCI_T0,
    pbSend,
    dwSend,
    NULL,
    pbRecv,
    &dwRecv );

if ( SCARD_S_SUCCESS != lReturn )
{
       // Fallo
}

Igualmente aquí tienes un ejemplo completo del uso de una clase similar para Smart Card.

El lector si es de SCM Microsystems pertenecerá (no puedo asegurarte al 100%) a Identive ID que si dispone de herramientas para desarrolladores, igualmente es un punto de referencia para PS/SC: SDKs para PC/SC aquí.

Referencias de los posibles códigos de error.

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.