//public byte[] APDU_SendCommand_T0(byte[] sendBuffer, int sendbufferlen, int receivebufferlen) //{ // HiDWinscard.SCARD_IO_REQUEST sioreq; // sioreq.dwProtocol = HiDWinscard.SCARD_PROTOCOL_T0; // sioreq.cbPciLength = 8; // HiDWinscard.SCARD_IO_REQUEST rioreq; // rioreq.cbPciLength = 8; // rioreq.dwProtocol = HiDWinscard.SCARD_PROTOCOL_T0; // for (byte i = 0; i < sendbufferlen; i++) // { // this.sendBuffer[i] = sendBuffer[i]; // } // this.sendbufferlen = sendbufferlen; // this.receivebufferlen = receivebufferlen; // retval = HID.SCardTransmit(hCard, ref sioreq, sendBuffer, sendbufferlen, ref rioreq, receiveBuffer, ref receivebufferlen); // if (retval == 0) // { // return receiveBuffer; // } // else // { // throw new Exception("Błąd wykonania polecenia."); // } //} public byte[] APDU_SendCommand(byte[] sendBuffer, int sendbufferlen, int receivebufferlen, int dwProtocol) { HiDWinscard.SCARD_IO_REQUEST sioreq; sioreq.dwProtocol = dwProtocol; sioreq.cbPciLength = 8; HiDWinscard.SCARD_IO_REQUEST rioreq; rioreq.cbPciLength = 8; rioreq.dwProtocol = dwProtocol; Array.Clear(this.sendBuffer, 0, this.sendBuffer.Length); Array.Clear(this.receiveBuffer, 0, this.receiveBuffer.Length); for (byte i = 0; i < sendbufferlen; i++) { this.sendBuffer[i] = sendBuffer[i]; } this.sendbufferlen = sendbufferlen; this.receivebufferlen = receivebufferlen; retval = HID.SCardTransmit(hCard, ref sioreq, sendBuffer, sendbufferlen, ref rioreq, receiveBuffer, ref receivebufferlen); if (retval == 0) { return(receiveBuffer); } else { throw new Exception("Błąd wykonania polecenia."); } }
public void Authentication(byte blockNumber, int keynum, out string status) { status = ""; currentBlock = blockNumber; HiDWinscard.SCARD_IO_REQUEST sioreq; sioreq.dwProtocol = 0x2; sioreq.cbPciLength = 8; HiDWinscard.SCARD_IO_REQUEST rioreq; rioreq.cbPciLength = 8; rioreq.dwProtocol = 0x2; //'******************************************************************** // ' For Authentication using key number // '********************************************************************* bcla = 0xFF; bins = 0x86; bp1 = 0x0; bp2 = 0x0; //currentBlock len = 0x5; sendBuffer[0] = bcla; sendBuffer[1] = bins; sendBuffer[2] = bp1; sendBuffer[3] = bp2; sendBuffer[4] = len; sendBuffer[5] = 0x1; //Version sendBuffer[6] = 0x0; //Address MSB sendBuffer[7] = currentBlock; //Address LSB sendBuffer[8] = 0x60; //Key Type A sendBuffer[9] = (byte)keynum; //Key Number sendbufferlen = 0xA; receivebufferlen = 255; retval = HID.SCardTransmit(hCard, ref sioreq, sendBuffer, sendbufferlen, ref rioreq, receiveBuffer, ref receivebufferlen); if (retval == 0) { if ((receiveBuffer[receivebufferlen - 2] == 0x90) && (receiveBuffer[receivebufferlen - 1] == 0)) { IsAuthenticated = true; status = "> General Authenticate" + " Successful \n"; } else { status = "> General Authenticate" + " Failed(SW1 SW2 =" + BitConverter.ToString(receiveBuffer, (receivebufferlen - 2), 1) + " " + BitConverter.ToString(receiveBuffer, (receivebufferlen - 1), 1) + ")\n"; } } else { status = "> General Authenticate" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; } }
public string Read(byte blockNumber, out string status) { status = ""; String read_str; //writeLabel.Content = ""; if (IsAuthenticated == true) { HiDWinscard.SCARD_IO_REQUEST sioreq; sioreq.dwProtocol = 0x2; sioreq.cbPciLength = 8; HiDWinscard.SCARD_IO_REQUEST rioreq; rioreq.cbPciLength = 8; rioreq.dwProtocol = 0x2; bcla = 0xFF; bins = 0xB0; bp1 = 0x0; bp2 = blockNumber; sendBuffer[0] = bcla; sendBuffer[1] = bins; sendBuffer[2] = bp1; sendBuffer[3] = bp2; sendBuffer[4] = 0x0; sendbufferlen = 0x5; receivebufferlen = 0x12; retval = HID.SCardTransmit(hCard, ref sioreq, sendBuffer, sendbufferlen, ref rioreq, receiveBuffer, ref receivebufferlen); if (retval == 0) { if ((receiveBuffer[receivebufferlen - 2] == 0x90) && (receiveBuffer[receivebufferlen - 1] == 0)) { //read_pressed = true; read_str = HexToBytenByteToHex.ToString(receiveBuffer); status = "> READ BINARY ( Block " + blockNumber.ToString() + " ) Successful\n"; return(read_str.Substring(0, ((int)(receivebufferlen - 2)) * 2)); } else { status = "> SCardTransmit" + " Failed(SW1 SW2 =" + BitConverter.ToString(receiveBuffer, (receivebufferlen - 2), 1) + " " + BitConverter.ToString(receiveBuffer, (receivebufferlen - 1), 1) + ")\n"; } } else { status = "> SCardTransmit" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; } } return(""); }
public void LoadKey(int keynum, string key, out string status) { HiDWinscard.SCARD_IO_REQUEST sioreq; sioreq.dwProtocol = 0x2; sioreq.cbPciLength = 8; HiDWinscard.SCARD_IO_REQUEST rioreq; rioreq.cbPciLength = 8; rioreq.dwProtocol = 0x2; if (key.Length == 12) { Byte[] str3 = HexToBytenByteToHex.GetBytes(key, out discarded); //Encoding.ASCII.GetBytes(keych1); bcla = 0xFF; bins = 0x82; bp1 = 0x20; bp2 = (byte)keynum; len = 0x6; sendBuffer[0] = bcla; sendBuffer[1] = bins; sendBuffer[2] = bp1; sendBuffer[3] = bp2; sendBuffer[4] = len; for (int k = 0; k <= str3.Length - 1; k++) { sendBuffer[k + 5] = str3[k]; } sendbufferlen = 0xB; receivebufferlen = 255; retval = HID.SCardTransmit(hCard, ref sioreq, sendBuffer, sendbufferlen, ref rioreq, receiveBuffer, ref receivebufferlen); if (retval == 0) { if ((receiveBuffer[receivebufferlen - 2] == 0x90) && (receiveBuffer[receivebufferlen - 1] == 0)) { status = "> LOAD KEY ( No. " + keynum + " ) Successful \n"; } else { status = "> Load Key" + " Failed(SW1 SW2 =" + BitConverter.ToString(receiveBuffer, (receivebufferlen - 2), 1) + " " + BitConverter.ToString(receiveBuffer, (receivebufferlen - 1), 1) + ")\n"; } } else { status = "> Load Key" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; } } else { status = "Klucz nie ma odpowiedniej długości!"; } }