📄 t0demo.cs
字号:
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
// Select a file
apdu.CLA = 0x80;
apdu.INS = 0xA4;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x02;
apdu.Le = 0x00;
apdu.dataIn[0] = 0xFF;
apdu.dataIn[1] = 0x04;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
// Write a record
apdu.CLA = 0x80;
apdu.INS = 0xD2;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x06;
apdu.Le = 0x00;
apdu.dataIn[0] = 1;
apdu.dataIn[1] = 2;
apdu.dataIn[2] = 3;
apdu.dataIn[3] = 4;
apdu.dataIn[4] = 5;
apdu.dataIn[5] = 6;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
// Read a record
apdu.CLA = 0x80;
apdu.INS = 0xB2;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x00;
apdu.Le = 0x06;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
// Display the data read from the card
Data.Text = "";
for(i = 0; i < 6; i ++)
Data.Text += apdu.dataOut[i].ToString();
}
private void RefreshButton_Click(object sender, System.EventArgs e)
{
RefreshDisplay();
}
private void onSmartCardInserted(object sender, System.EventArgs e)
{
Data.Text += "Smartcard inserted \r\n";
}
private void onSmartCardRemoved(object sender, System.EventArgs e)
{
Data.Text += "Smartcard removed \r\n";
}
private void onGrabbaReconnectedEvent(object sender, System.EventArgs e)
{
Data.Text += "Grabba Reconnected!\r\n";
}
private void onGrabbaDisconnectedEvent(object sender, System.EventArgs e)
{
Data.Text += "Grabba Disconnected!\r\n";
}
private void enableBtn_Click(object sender, EventArgs e)
{
if (enableBtn.Text.Equals("Enable"))
{
myGrabba.SmartCardEnable();
enableBtn.Text = "Disable";
}
else
{
myGrabba.SmartCardDisable();
enableBtn.Text = "Enable";
}
}
private void testBtn_Click(object sender, EventArgs e)
{
/*Grabba.SmartCardSession scSession = new Grabba.SmartCardSession();
scSession.cardType = Grabba.SmartCardType.SC_T1;
uint returnCode = myGrabba.SmartCardStartSession(ref scSession);
if (returnCode != myGrabba.errNone)
{
MessageBox.Show("Problem! SmartCardStartSession rc:" + returnCode.ToString(), "Error",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
}
byte[] hostChallenge = { 0x58, 0x67, 0xef, 0x4c, 0xde, 0xad, 0x00, 0x01 };
uint rc;
int i;
byte[] keyDiversificationData = new byte[10];
byte[] keyInformation = new byte[2];
byte[] cardChallenge = new byte[8];
byte[] cardCryptogram = new byte[8];
Grabba.SmartCardAPDU apdu = new Grabba.SmartCardAPDU();
apdu.CLA = 0x80;
apdu.INS = 0x50;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x08;
apdu.Le = 0x00;
for (i = 0; i < 8; i++)
apdu.dataIn[i] = hostChallenge[i];
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Grabba RC on send INIT UPDATE: " + rc.ToString(), "Error",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
// this returns 29650 -> 73 D2
}
if (!(apdu.status.Equals(0x9000)))
{
MessageBox.Show("Card Status on send INIT UPDATE: " + apdu.ToString(), "Error",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
// this returns 32848 -> 80 50
}*/
Grabba.SmartCardSession scSession = new Grabba.SmartCardSession();
scSession.cardType = Grabba.SmartCardType.SC_T0;
uint returnCode = myGrabba.SmartCardStartSession(ref scSession);
if (returnCode != myGrabba.errNone)
{
MessageBox.Show("Problem! SmartCardStartSession rc:" + returnCode.ToString(), "Error",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
}
Grabba.SmartCardInfo info = new Grabba.SmartCardInfo();
returnCode = myGrabba.SmartCardGetInfo(ref info);
SelectDHSHeader();
GetMedicareNumber();
}
public void SelectDHSHeader()
{
uint rc = 0;
Grabba.SmartCardAPDU apdu = new Grabba.SmartCardAPDU();
// Do a Select File command and select Header applet
apdu.CLA = 0x00;
apdu.INS = 0xA4;
apdu.P1 = 0x04;
apdu.P2 = 0x00;
apdu.Lc = 0x06;
apdu.Le = 0x00;
// Hex of Header applet = A0 00 00 00 77 01
apdu.dataIn[0] = 0xA0;
apdu.dataIn[1] = 0x00;
apdu.dataIn[2] = 0x00;
apdu.dataIn[3] = 0x00;
apdu.dataIn[4] = 0x77;
apdu.dataIn[5] = 0x01;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (rc != 0)
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
}
public string GetMedicareNumber()
{
uint rc = 0;
int i = 0;
Grabba.SmartCardAPDU apdu = new Grabba.SmartCardAPDU();
string medicareNumber = new string('\0', 50);
// Select Medicare Number
apdu.CLA = 0x00;
apdu.INS = 0xA4;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x02;
apdu.Le = 0x00;
apdu.dataIn[0] = 0x10;
apdu.dataIn[1] = 0x01;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (rc != 0)
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
}
// Read Medicare Number
apdu.CLA = 0x00;
apdu.INS = 0xB0;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x00;
apdu.Le = 0x09;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (rc != 0)
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
}
// Display the data read from the card
for (i = 0; i < 9; i++)
medicareNumber += Convert.ToChar(Convert.ToInt32(apdu.dataOut[i].ToString()));
return medicareNumber;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -