public void sCardEstablishContext(out string status) { status = ""; try { dwscope = 2; if (readerName != "" && readerName != null) { retval = HID.SCardEstablishContext(dwscope, IntPtr.Zero, IntPtr.Zero, out hContext); if (retval == 0) { IsAuthenticated = false; status = "> SCardEstablishContext" + " Successful"; release_flag = true; } else { status = "> SCardEstablishContext" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; //timer.Enabled = false; } } else { status = "Failed... " + String.Format("{0:x}", retval) + "H\n"; //timer.Enabled = false; } } catch { } }
//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 Connect_T1(out string status) //{ // status = ""; // retval = HID.SCardConnect(hContext, readerName, HiDWinscard.SCARD_SHARE_SHARED, HiDWinscard.SCARD_PROTOCOL_T1, // ref hCard, ref protocol // ); //Command to connect the card ,protocol T=0 // ReaderState.RdrName = readerName; // ReaderState.RdrCurrState = HiDWinscard.SCARD_STATE_UNAWARE; // ReaderState.RdrEventState = 0; // //ReaderState.UserData = "Mifare Card"; // value_Timeout = 0; // ReaderCount = 1; // if (retval == 0) // { // status = "> SCardConnect" + " Successful \n"; // retval = HID.SCardGetStatusChange(hContext, value_Timeout, ref ReaderState, ReaderCount); // } // else if (retval != 0) // { // status = "> SCardConnect" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; // } //} public void Connect(int dwProtocol, out string status) { status = ""; retval = HID.SCardConnect(hContext, readerName, HiDWinscard.SCARD_SHARE_SHARED, (uint)dwProtocol, ref hCard, ref protocol ); //Command to connect the card ,protocol T=0 ReaderState.RdrName = readerName; ReaderState.RdrCurrState = HiDWinscard.SCARD_STATE_UNAWARE; ReaderState.RdrEventState = 0; ReaderState.UserData = "Starcos Card"; value_Timeout = 0; ReaderCount = 1; if (retval == 0) { status = "> SCardConnect" + " Successful \n"; retval = HID.SCardGetStatusChange(hContext, value_Timeout, ref ReaderState, ReaderCount); } else if (retval != 0) { status = "> SCardConnect" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; } }
public int GetStatusChange(out string status) { status = ""; ReaderState.RdrName = readerName; ReaderState.RdrCurrState = HiDWinscard.SCARD_STATE_UNAWARE; ReaderState.RdrEventState = 0; ReaderState.UserData = "Mifare Card"; value_Timeout = 0; ReaderCount = 1; if (ReaderList == "") { status = "SmartCard Removed"; return(1); } else { retval = HID.SCardGetStatusChange(hContext, value_Timeout, ref ReaderState, ReaderCount); if ((ReaderState.ATRLength == 0) || (retval != 0)) { status = "SmartCard Removed"; return(1); } else { return(0); } } }
public void Disconnect(out string status) { status = ""; retval = HID.SCardDisconnect(hCard, HiDWinscard.SCARD_UNPOWER_CARD); //Command to disconnect the card if (retval == 0) { status = "> SCardDisconnect" + " Successful \n"; //HID.Beep(2000, 200); //Thread.Sleep(1500); //timer.Enabled = true; _isConnected = false; } else //if (retval != 0) { try { status = "> SCardDisConnect" + " Failed... " + " Error Code: " + String.Format("{0:x}"); //timer.Enabled = true;} } catch { } } }
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 bool Connect(out string status) { status = ""; logManager.LogToFile("============================================", false); logManager.LogToFile("Start new session", true); logManager.LogToFile("============================================", false); retval = HID.SCardConnect(hContext, readerName, HiDWinscard.SCARD_SHARE_SHARED, HiDWinscard.SCARD_PROTOCOL_T1, ref hCard, ref protocol ); //Command to connect the card ,protocol T=1 ReaderState.RdrName = readerName; ReaderState.RdrCurrState = HiDWinscard.SCARD_STATE_UNAWARE; ReaderState.RdrEventState = 0; ReaderState.UserData = "Mifare Card"; value_Timeout = 0; ReaderCount = 1; if (retval == 0) { status = "> SCardConnect" + " Successful \n"; retval = HID.SCardGetStatusChange(hContext, value_Timeout, ref ReaderState, ReaderCount); _isConnected = true; //if (ReaderState.ATRValue[ReaderState.ATRLength - 0x6].Equals(1)) //{ // card_Type = 1; // //ATR_UID(card_Type); //} //else if (ReaderState.ATRValue[ReaderState.ATRLength - 0x6].Equals(2)) //{ // card_Type = 2; // //ATR_UID(card_Type); //} //else //{ // card_Type = 3; // //ATR_UID(card_Type); //} //timer.Enabled = false; logManager.LogToFile("Connected to card", true); return(true); } else //if (retval != 0) { logManager.LogToFile("Error in connecting to card", true); status = "> SCardConnect" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; //timer.Enabled = true; return(false); } }
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!"; } }
// public void APDU_getMF(out string status) // { // status = ""; // String read_str; // HiDWinscard.SCARD_IO_REQUEST sioreq; // sioreq.dwProtocol = 0x2; // sioreq.cbPciLength = 8; // HiDWinscard.SCARD_IO_REQUEST rioreq; // rioreq.cbPciLength = 8; // rioreq.dwProtocol = 0x2; // sendBuffer[0] = 0x00; // sendBuffer[1] = 0xA4; // sendBuffer[2] = 0x00; // sendBuffer[3] = 0x0C; // sendBuffer[4] = 0x02; // sendBuffer[5] = 0x3F; // sendBuffer[6] = 0x00; // sendbufferlen = 0x7; // receivebufferlen = 0x0A; // 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 = "> MF SELECTED Successful\n"; // } // 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"; // } // } // public void APDU_getAIDD6160000300101(out string status) //{ // status = ""; // String read_str; // HiDWinscard.SCARD_IO_REQUEST sioreq; // sioreq.dwProtocol = 0x2; // sioreq.cbPciLength = 8; // HiDWinscard.SCARD_IO_REQUEST rioreq; // rioreq.cbPciLength = 8; // rioreq.dwProtocol = 0x2; // sendBuffer[0] = 0x00; // sendBuffer[1] = 0xA4; // sendBuffer[2] = 0x04; // sendBuffer[3] = 0x0C; // sendBuffer[4] = 0x07; // sendBuffer[5] = 0xD6; // sendBuffer[6] = 0x16; // sendBuffer[7] = 0x00; // sendBuffer[8] = 0x00; // sendBuffer[9] = 0x30; // sendBuffer[10] = 0x01; // sendBuffer[11] = 0x01; // sendbufferlen = 0x0C; // receivebufferlen = 0x0A; // 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 = "> AIDD6160000300101 SELECTED Successful\n"; // } // 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"; // } // } // public void APDU_FID0002(out string status) // { // status = ""; // String read_str; // HiDWinscard.SCARD_IO_REQUEST sioreq; // sioreq.dwProtocol = 0x2; // sioreq.cbPciLength = 8; // HiDWinscard.SCARD_IO_REQUEST rioreq; // rioreq.cbPciLength = 8; // rioreq.dwProtocol = 0x2; // sendBuffer[0] = 0x00; // sendBuffer[1] = 0xA4; // sendBuffer[2] = 0x02; // sendBuffer[3] = 0x0C; // sendBuffer[4] = 0x02; // sendBuffer[5] = 0x00; // sendBuffer[6] = 0x02; // sendbufferlen = 0x07; // receivebufferlen = 0x0A; // 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 = "> FID0002 SELECTED Successful\n"; // } // 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"; // } // } // public byte[] APDU_ReadBinary(int index, int count, out string status) // { // status = ""; // String read_str; // HiDWinscard.SCARD_IO_REQUEST sioreq; // sioreq.dwProtocol = 0x2; // sioreq.cbPciLength = 8; // HiDWinscard.SCARD_IO_REQUEST rioreq; // rioreq.cbPciLength = 8; // rioreq.dwProtocol = 0x2; // sendBuffer[0] = 0x00; // sendBuffer[1] = 0xB0; // sendBuffer[2] = 0x00; // sendBuffer[3] = 0x25; // sendBuffer[4] = 0xBF; // sendbufferlen = 0x05; // receivebufferlen = 0xC1; // 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); // //System.IO.FileStream oFileStream = null; // //oFileStream = new System.IO.FileStream("001.txt", System.IO.FileMode.Create); // //oFileStream.Write(receiveBuffer, 0, receivebufferlen); // //oFileStream.Close(); // status = "> READ BINARY Successful\n"; // } // 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"; // } // byte[] outBuffer = new byte[receivebufferlen]; // Array.Copy(receiveBuffer, outBuffer, receivebufferlen); // return outBuffer; // } public void ReleaseContext(out string status) { status = ""; if (release_flag == true) { retval = HID.SCardReleaseContext(hContext); if (retval == 0) { status = "> SCardReleaseContext" + " Successful \n"; } else { status = "> SCardReleaseContext" + " Failed... " + " Error Code: " + String.Format("{0:x}", retval) + "H\n"; } } }
public List <string> getListReaders() { uint pcchReaders = 0; int nullindex = -1; char nullchar = (char)0; dwscope = 2; // Establish context. retval = HID.SCardEstablishContext(dwscope, IntPtr.Zero, IntPtr.Zero, out hContext); if (retval != 0) { throw new Exception("Błąd wykonania polecenia HID.SCardEstablishContext"); } retval = HID.SCardListReaders(hContext, null, null, ref pcchReaders); byte[] mszReaders = new byte[pcchReaders]; // Fill readers buffer with second call. retval = HID.SCardListReaders(hContext, null, mszReaders, ref pcchReaders); // Populate List with readers. string currbuff = Encoding.ASCII.GetString(mszReaders); ReaderList = currbuff; int len = (int)pcchReaders; if (len > 0) { while (currbuff[0] != nullchar) { nullindex = currbuff.IndexOf(nullchar); // Get null end character. string reader = currbuff.Substring(0, nullindex); readers.Add(reader); len = len - (reader.Length + 1); currbuff = currbuff.Substring(nullindex + 1, len); } } return(readers); }