예제 #1
0
        public static string cardProtocol()
        {
            WinSCard scard = new WinSCard();

            try
            {
                scard.EstablishContext();
                scard.ListReaders();
                string readerName = scard.ReaderNames[1];
                scard.WaitForCardPresent(readerName);


                scard.Connect(readerName);
                byte[] cmdApdu    = { 0xFF, 0xCA, 0x00, 0x00, 00 }; // Get Card UID ...
                byte[] respApdu   = new byte[10];
                int    respLength = respApdu.Length;

                scard.Transmit(cmdApdu, cmdApdu.Length, respApdu, ref respLength);

                //find a better place for this
                App.UID = HexFormatting.ToHexString(respApdu, true);

                //he wanted some kinda beeping sound when someone swipes their card
                System.Media.SystemSounds.Beep.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(App.UID);
        }
예제 #2
0
        private void btnread_Click(object sender, EventArgs e)
        {
            try
            {
                scard.EstablishContext();
                scard.ListReaders();

                //chioce reader
                string readerName = scard.ReaderNames[int.Parse("0")];

                txtlog.Text = "Wait for card present...";

                scard.WaitForCardPresent(readerName);

                //建立 Smart Card 連線
                scard.Connect(readerName);

                Console.WriteLine("ATR: 0x" + scard.AtrString);

                byte[] cmdApdu  = { 0x00, 0xA4, 0x04, 0x00, 0x10, 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00 };
                byte[] cmdApdu2 = { 0x00, 0xca, 0x11, 0x00, 0x02, 0x00, 0x00 };


                byte[] respApdu  = new byte[2];
                byte[] respApdu2 = new byte[59];

                int respLength  = respApdu.Length;
                int respLength2 = respApdu2.Length;

                //下達 Select Profile 檔的 APDU
                scard.Transmit(cmdApdu, cmdApdu.Length, respApdu, ref respLength);

                //下達讀取Profile指令
                scard.Transmit(cmdApdu2, cmdApdu2.Length, respApdu2, ref respLength2);

                txtlog.Text = "ok...";


                //健保卡ID
                textBox1.Text = Encoding.Default.GetString(respApdu2, 0, 12);
                //姓名  8個bit 因為有些姓氏4個字
                textBox2.Text = Encoding.Default.GetString(respApdu2, 12, 8);
                //身份証字號
                textBox3.Text = Encoding.Default.GetString(respApdu2, 32, 10);
                //生日
                textBox4.Text = Encoding.Default.GetString(respApdu2, 43, 2) + "/" + Encoding.Default.GetString(respApdu2, 45, 2) + "/"
                                + Encoding.Default.GetString(respApdu2, 47, 2);
                //姓別
                textBox5.Text = Encoding.Default.GetString(respApdu2, 49, 1);
                //發卡日期
                textBox6.Text = Encoding.Default.GetString(respApdu2, 51, 2) + "/" + Encoding.Default.GetString(respApdu2, 53, 2) + "/"
                                + Encoding.Default.GetString(respApdu2, 55, 2);
            }
            catch (WinSCardException ex)
            {
                Console.WriteLine(ex.WinSCardFunctionName + " 0x" + ex.Status.ToString("X08") + " " + ex.Message);
            }
            catch (Exception ex)
            {
                txtlog.Text = "無卡片 或 卡片接觸有問題...";
            }
            finally
            {
                scard.Disconnect();
                scard.ReleaseContext();
            }
        }