本示例使用发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d292c1bSxJTb6&ft=t&id=917152255720
Public Class Form1
Public Declare Function pcdbeep Lib "OUR_MIFARE.dll" (ByVal xms As Integer) As Byte
Public Declare Function pcdgetdevicenumber Lib "OUR_MIFARE.dll" (ByRef devicenumber As Byte) As Byte
Public Declare Function getmifareversion Lib "OUR_MIFARE.dll" (ByRef cardtypestr As Byte, ByRef AtqaSak As Byte, ByRef versionbuf As Byte, ByRef versionlen As Byte, ByRef retsw As Byte) As Byte
Public Declare Function cpurequest1 Lib "OUR_MIFARE.dll" (ByRef mypiccserial As Byte, ByRef myparam As Byte, ByRef myver As Byte, ByRef mycode As Byte, ByRef AtqaSak As Byte) As Byte
Public Declare Function desfireselectapplication Lib "OUR_MIFARE.dll" (ByRef aid As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfireauthkeyev1 Lib "OUR_MIFARE.dll" (ByRef keybuf As Byte, ByVal keyid As Byte, ByVal authmode As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirechangekeyev1 Lib "OUR_MIFARE.dll" (ByRef newkeybuf As Byte, ByVal keyid As Byte, ByVal authmode As Byte, ByVal zeno As Byte, ByRef oldkeybuf As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfireauthkeyev2 Lib "OUR_MIFARE.dll" (ByRef keybuf As Byte, ByVal keyid As Byte, ByVal authmode As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfireformatpicc Lib "OUR_MIFARE.dll" (ByRef retsw As Byte) As Byte
Public Declare Function desfiredeleteapplication Lib "OUR_MIFARE.dll" (ByRef aid As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirecreateapplication Lib "OUR_MIFARE.dll" (ByRef aid As Byte, ByVal keysetting As Byte, ByVal keynumver As Byte, ByVal keytype As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirechangefilesettings Lib "OUR_MIFARE.dll" (ByVal fileid As Byte, ByVal comset As Byte, ByRef accessrights As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirecreatestddatafile Lib "OUR_MIFARE.dll" (ByVal fileid As Byte, ByVal comset As Byte, ByRef accessrights As Byte, ByVal filesize As Int32, ByRef retsw As Byte) As Byte
Public Declare Function desfircreatebackupdatafile Lib "OUR_MIFARE.dll" (ByVal fileid As Byte, ByVal comset As Byte, ByRef accessrights As Byte, ByVal filesize As Int32, ByRef retsw As Byte) As Byte
Public Declare Function desfiregetfilesettings Lib "OUR_MIFARE.dll" (ByVal fileid As Byte, ByRef filesettingsbuf As Byte, ByRef revbuflen As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfiredeletefile Lib "OUR_MIFARE.dll" (ByVal fileid As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirereaddata Lib "OUR_MIFARE.dll" (ByVal cmd As Byte, ByVal fileno As Byte, ByVal offset As Int32, ByVal datalen As Int32, ByRef databuf As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirewritedata Lib "OUR_MIFARE.dll" (ByVal cmd As Byte, ByVal fileno As Byte, ByVal offset As Int32, ByVal datalen As Int32, ByRef databuf As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfiregetfileids Lib "OUR_MIFARE.dll" (ByRef fileidbuf As Byte, ByRef fidsize As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfiregetapplicationids Lib "OUR_MIFARE.dll" (ByRef aidbuf As Byte, ByRef aidsize As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfiregetkeysettings Lib "OUR_MIFARE.dll" (ByRef aidbuf As Byte, ByRef keysetting As Byte, ByRef keysize As Byte, ByRef retsw As Byte) As Byte
Public Declare Function desfirechangekeysettings Lib "OUR_MIFARE.dll" (ByRef aidbuf As Byte, ByVal keysetting As Byte, ByRef retsw As Byte) As Byte
Private Sub MessageDispInfo(errno As Byte) '函数返回状态解析
Select Case errno
Case 0
MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 8
MessageBox.Show("请重新拿开卡后再放到感应区!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 23
MessageBox.Show("错误代码:" + errno.ToString("D") + ",驱动程序错误或尚未安装!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 24
MessageBox.Show("错误代码:" + errno.ToString("D") + ",操作超时,一般是动态库没有反映!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 25
MessageBox.Show("错误代码:" + errno.ToString("D") + ",发送字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 26
MessageBox.Show("错误代码:" + errno.ToString("D") + ",发送的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 27
MessageBox.Show("错误代码:" + errno.ToString("D") + ",接收的字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 28
MessageBox.Show("错误代码:" + errno.ToString("D") + ",接收的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 50
MessageBox.Show("RATS错误,厂家调试代码,用户不需理会!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 51
MessageBox.Show("PPS错误,厂家调试代码,用户不需理会!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 52
MessageBox.Show("已进入了14443-4协议状态,可进行CPU卡功能所有操作了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 53
MessageBox.Show("CPU卡功能通讯错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 54
MessageBox.Show("数据不足,需要接着发送未完成的数据至卡上!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 55
MessageBox.Show("发送ACK指令给卡,让卡接着发送数据回来!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 56
MessageBox.Show("清空根目录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 57
MessageBox.Show("卡片不支持功能!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 58
MessageBox.Show("卡片初始化失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 59
MessageBox.Show("分配的空间不足!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 60
MessageBox.Show("本次操作的实体已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 61
MessageBox.Show("无足够空间!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 62
MessageBox.Show("文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 63
MessageBox.Show("权限不足,有可能是用只读密码认证,导致无法更改读写密码或无法写文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 64
MessageBox.Show("密码不存在,或密钥文件未创建!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 65
MessageBox.Show("传送长度错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 66
MessageBox.Show("Le错误,即接收的数据长度指定过大!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 67
MessageBox.Show("功能不支持或卡中无MF 或卡片已锁定!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 68
MessageBox.Show("密码认证错识次数过多,该密码已被锁死!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 86
MessageBox.Show("更改后的密码长度必须和创建时的长度一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 87
MessageBox.Show("应用目录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 88
MessageBox.Show("应用文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 89
MessageBox.Show("文件号不能超过5!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 90
MessageBox.Show("读取文件时返回的长度不足,数据可能不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 91
MessageBox.Show("一次读文件的长度不能超过255!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 92
MessageBox.Show("一次写文件的长度不能超过247!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 70
Case 71
Case 72
Case 73
Case 74
Case 75
Case 76
Case 77
Case 78
Case 79
Case 80
Case 81
Case 82
Case 83
Case 84
Case 85
MessageBox.Show("密码错误,剩余次数为" + Convert.ToString(errno - 70) + ",如果为0,该密码将锁死,无法再认证", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else
MessageBox.Show("操作失败,返回错误代码!" + Convert.ToString(errno), "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
End Sub
Function RetTextFromStr(ByVal inputstr As String) As String '卡操作返回代码解析
RetTextFromStr = ""
Select Case inputstr
Case "9000"
RetTextFromStr = "成功!"
Case "9100"
RetTextFromStr = "成功!"
pcdbeep(20)
Case "6281"
RetTextFromStr = "回送的数据可能错误!"
Case "6283"
RetTextFromStr = "选择文件无效,文件或密钥校验错误"
Case "6400"
RetTextFromStr = "状态标志未改变"
Case "6581"
RetTextFromStr = "写 EEPROM 不成功!"
Case "6700"
RetTextFromStr = "长度错误"
Case "6900"
RetTextFromStr = "CLA 与线路保护要求不匹配"
Case "6901"
RetTextFromStr = "无效的状态!"
Case "6981"
RetTextFromStr = "命令与文件结构不相容"
Case "6982"
RetTextFromStr = "不满足安全状态"
Case "6983"
RetTextFromStr = "密钥被锁死!"
Case "6984"
RetTextFromStr = "MAC格式不符合"
Case "6985"
RetTextFromStr = "使用条件不满足"
Case "6986"
RetTextFromStr = "请先选择文件!"
Case "6987"
RetTextFromStr = "无安全报文"
Case "6988"
RetTextFromStr = "安全报文数据项不正确"
Case "6A80"
RetTextFromStr = "数据域参数错误!"
Case "6A81"
RetTextFromStr = "功能不支持或卡中无MF 或卡片已锁定"
Case "6A82"
RetTextFromStr = "文件未找到"
Case "6A83"
RetTextFromStr = "记录未找到!"
Case "6A84"
RetTextFromStr = "文件无足够空间"
Case "6A86"
RetTextFromStr = "参数P1 P2 错"
Case "6A88"
RetTextFromStr = "密钥未找到!"
Case "6B00"
RetTextFromStr = "在达到Le/Lc 字节之前文件结束,偏移量错误"
Case "6E00"
RetTextFromStr = "无效的CLA"
Case "6F00"
RetTextFromStr = "数据无效!"
Case "9302"
RetTextFromStr = "MAC 错误"
Case "9303"
RetTextFromStr = "应用已被锁定"
Case "9401"
RetTextFromStr = "金额不足!"
Case "9403"
RetTextFromStr = "密钥未找到!"
Case "9406"
RetTextFromStr = "所需的MAC 不可用!"
Case "91AE"
RetTextFromStr = "认证失败,请检查命行的参数和前期计算是否错误!"
Case "91CA"
RetTextFromStr = "上一个命令未完全完成!"
Case "917E"
RetTextFromStr = "指令长度错误!"
Case "9140"
RetTextFromStr = "当前目录或应用密钥不存在,请先选择正确的目录或应用!"
Case "919D"
RetTextFromStr = "处于未验证密码的状态,该指令无法操作!"
Case "911E"
RetTextFromStr = "MAC错误!"
Case "91F0"
RetTextFromStr = "该文件号不存在!"
Case "919E"
RetTextFromStr = "参数无效!"
Case "91BE"
RetTextFromStr = "试图读取/写入的数据超出文件/记录的边界!"
Case "91A0"
RetTextFromStr = "请求的 AID 不存在!"
Case Else
If inputstr.Substring(0, 3) = "63C" Then
Dim i As Integer = Convert.ToInt16(inputstr.Substring(3, 1), 16)
If i > 0 Then
RetTextFromStr = "再试 " + i.ToString("D") + " 次错误密码将锁定!"
Else
RetTextFromStr = "密码已被锁定"
End If
Else
RetTextFromStr = "未知的异常"
End If
End Select
Return RetTextFromStr
End Function
Function checkhexstr(ByVal inputstr As String, ByVal hexlen As Integer, ByRef bytebuf() As Byte) As Boolean '判断16进制字符串数据是否正确
Try
inputstr = inputstr.Replace(" ", "") '去除空格
inputstr = inputstr.Replace(vbCrLf, "") '去除回车换行符
inputstr = inputstr.Replace(vbLf, "")
inputstr = inputstr.Replace(vbCr, "")
Dim i As Integer
For i = 0 To hexlen - 1
bytebuf(i) = Convert.ToByte(Convert.ToInt32(inputstr.Substring(i * 2, 2), 16))
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
comboBox1.SelectedIndex = 0
comboBox3.SelectedIndex = 0
comboBox14.SelectedIndex = 0
comboBox2.SelectedIndex = 0
comboBox4.SelectedIndex = 0
comboBox5.SelectedIndex = 1
comboBox6.SelectedIndex = 1
comboBox7.SelectedIndex = 1
comboBox8.SelectedIndex = 1
comboBox9.SelectedIndex = 0
comboBox10.SelectedIndex = 14
comboBox11.SelectedIndex = 14
comboBox12.SelectedIndex = 14
comboBox13.SelectedIndex = 14
End Sub
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox1.SelectedIndexChanged
Select comboBox1.SelectedIndex
Case 0
textBox3.Text = "00 00 00 00 00 00 00 00"
Case 1
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
Case 2
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
Case Else
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
End Select
End Sub
Private Sub comboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox3.SelectedIndexChanged
Select comboBox3.SelectedIndex
Case 0
textBox3.Text = "00 00 00 00 00 00 00 00"
textBox4.Text = "00 00 00 00 00 00 00 00"
Case 1
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
textBox4.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
Case 2
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
textBox4.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
Case Else
textBox3.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
textBox4.Text = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
End Select
End Sub
Private Sub button4_Click(sender As Object, e As EventArgs) Handles button4.Click
Dim xms As Integer
xms = 50
Dim status As Byte = pcdbeep(xms)
If status <> 0 Then
MessageDispInfo(status)
End If
End Sub
Private Sub button8_Click(sender As Object, e As EventArgs) Handles button8.Click
Dim status As Byte '存放返回值
Dim devno(3) As Byte '设备编号
status = pcdgetdevicenumber(devno(0))
If status = 0 Then
MsgBox("设备编号:" + devno(0).ToString("D3") + "-" + devno(1).ToString("D3") + "-" + devno(2).ToString("D3") + "-" + devno(3).ToString("D3"), vbInformation + vbOKOnly, "提示")
Else
MessageDispInfo(status)
End If
End Sub
Private Sub button7_Click(sender As Object, e As EventArgs) Handles button7.Click
Dim AtqaSak(3) As Byte
Dim retsw(1) As Byte
Dim versionbuf(100) As Byte
Dim versionlen(2) As Byte
Dim cardtypebuf(1024) As Byte
Dim status As Byte = getmifareversion(cardtypebuf(0), AtqaSak(0), versionbuf(0), versionlen(0), retsw(0))
Dim cardtypestr As String = System.Text.Encoding.ASCII.GetString(cardtypebuf).Trim()
Dim retstr As String = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status > 0 Then
MessageDispInfo(status)
Else
MessageBox.Show("获取IC卡芯片型号操作,卡片返回代码:" + retstr + vbCrLf + "型号:" + cardtypestr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Dim mypiccserial(7) As Byte
Dim myparam(4) As Byte
Dim AtqaSak(3) As Byte
Dim myver(1) As Byte
Dim mycode(1) As Byte
Dim cardhohex As String = ""
Dim parastr As String = ""
Dim verstr As String = ""
Dim codestr As String = ""
Dim i As Integer
Dim status As Byte = cpurequest1(mypiccserial(0), myparam(0), myver(0), mycode(0), AtqaSak(0))
If status = 0 Or status = 52 Then
pcdbeep(20)
If AtqaSak(0) \ 64 > 0 Then
For i = 0 To 6
cardhohex = cardhohex + mypiccserial(i).ToString("X2")
Next
For i = 0 To 3
parastr = parastr + myparam(i).ToString("X2")
Next
verstr = myver(0).ToString("X2")
codestr = mycode(0).ToString("X2")
textBox1.Text = cardhohex
MessageBox.Show("激活Desfire卡成功,可以接着重复操作第二步进行调试了。" + vbCrLf + "16进制卡号:" + cardhohex + vbCrLf + "参数:" + parastr + vbCrLf + "版本信息:" + verstr + vbCrLf + "厂商代码(复旦为90):" + codestr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
For i = 0 To 3
cardhohex = cardhohex + mypiccserial(i).ToString("X2")
Next
For i = 0 To 3
parastr = parastr + myparam(i).ToString("X2")
Next
verstr = myver(0).ToString("X2")
codestr = mycode(0).ToString("X2")
textBox1.Text = cardhohex
MessageBox.Show("激活FM1208CPU卡成功,可以接着重复操作第二步进行调试了。" + vbCrLf + "16进制卡号:" + cardhohex + vbCrLf + "参数:" + parastr + vbCrLf + "版本信息:" + verstr + vbCrLf + "厂商代码(复旦为90):" + codestr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageDispInfo(status)
End If
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Dim aidbuf(3) As Byte
Dim retsw(1) As Byte
Dim retstr As String = ""
If Not checkhexstr(textBox2.Text.Trim(), 3, aidbuf) Then
MessageBox.Show("十六进制AID输入错误,请输入3字节的16进制AID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfireselectapplication(aidbuf(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status > 0 Then
MessageBox.Show("选择卡内应用AID操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("选择卡内应用AID操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button6_Click(sender As Object, e As EventArgs) Handles button6.Click
Dim newkeybuf(24) As Byte
Dim oldkeybuf(24) As Byte
Dim retsw(1) As Byte
Dim retstr As String
Dim keylen As Integer
Select Case comboBox3.SelectedIndex
Case 0
keylen = 8
Case 1
keylen = 16
Case 2
keylen = 24
Case Else
keylen = 16
End Select
If Not checkhexstr(textBox4.Text.Trim(), keylen, newkeybuf) Then
MessageBox.Show("十六进制新密钥输入错误,请输入 " + keylen.ToString("D") + " 字节的16进制新密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
If Not checkhexstr(textBox3.Text.Trim(), keylen, oldkeybuf) Then
MessageBox.Show("十六进制旧密钥输入错误,请输入 " + keylen.ToString("D") + " 字节的16进制旧密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfirechangekeyev1(newkeybuf(0), Convert.ToByte(keyid.Value), Convert.ToByte(comboBox3.SelectedIndex), 0, oldkeybuf(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status > 0 Then
MessageBox.Show("EV1更改密码操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("EV1更改密码操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
Dim authkeybuf(24) As Byte
Dim retsw(1) As Byte
Dim retstr As String
Dim keylen As Integer
Select Case comboBox1.SelectedIndex
Case 0
keylen = 8
Case 1
keylen = 16
Case 2
keylen = 24
Case Else
keylen = 16
End Select
If Not checkhexstr(textBox3.Text.Trim(), keylen, authkeybuf) Then
MessageBox.Show("十六进制认证密钥输入错误,请输入 " + keylen.ToString("D") + " 字节的16进制认证密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfireauthkeyev1(authkeybuf(0), Convert.ToByte(keyid.Value), Convert.ToByte(comboBox1.SelectedIndex), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("认证密码操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("认证密码操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button18_Click(sender As Object, e As EventArgs) Handles button18.Click
Dim authkeybuf(24) As Byte
Dim retsw(1) As Byte
Dim retstr As String
Dim keylen As Integer = 16
If Not checkhexstr(textBox6.Text.Trim(), keylen, authkeybuf) Then
MessageBox.Show("十六进制认证密钥输入错误,请输入 " + keylen.ToString("D") + " 字节的16进制认证密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfireauthkeyev2(authkeybuf(0), Convert.ToByte(keyid.Value), Convert.ToByte(comboBox4.SelectedIndex), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("认证密码操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("认证密码操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button5_Click(sender As Object, e As EventArgs) Handles button5.Click
Dim retsw(1) As Byte
Dim retstr As String
Dim status As Byte = desfireformatpicc(retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status > 0 Then
MessageBox.Show("初始格式化卡操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("初始格式化卡操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button10_Click(sender As Object, e As EventArgs) Handles button10.Click
Dim aidbuf(3) As Byte
Dim retsw(1) As Byte
Dim retstr As String
If Not checkhexstr(textBox5.Text.Trim(), 3, aidbuf) Then
MessageBox.Show("十六进制AID输入错误,请输入3字节的16进制AID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfiredeleteapplication(aidbuf(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("删除卡内应用操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("删除卡内应用操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button9_Click(sender As Object, e As EventArgs) Handles button9.Click
Dim aidbuf(3) As Byte
Dim retsw(1) As Byte
Dim retstr As String
If Not checkhexstr(textBox5.Text.Trim(), 3, aidbuf) Then
MessageBox.Show("十六进制AID输入错误,请输入3字节的16进制AID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim keysetting As Byte = Convert.ToByte(comboBox4.SelectedIndex * 16 + comboBox5.SelectedIndex * 8 + comboBox6.SelectedIndex * 4 + comboBox7.SelectedIndex * 2 + comboBox8.SelectedIndex)
Dim status As Byte = desfirecreateapplication(aidbuf(0), keysetting, Convert.ToByte(keynumber.Value), Convert.ToByte(comboBox2.SelectedIndex), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("创建新应用操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("创建新应用操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button11_Click(sender As Object, e As EventArgs) Handles button11.Click
Dim status As Byte '函数返回值
Dim comset As Byte '通讯模式
Dim accessrights(1) As Byte '文件访问时的密钥认证方式
Dim retsw(1) As Byte
Dim retstr As String
Dim filebytes As Int32 = Convert.ToInt32(filesize.Value)
If (comboBox9.SelectedIndex < 2) Then
comset = Convert.ToByte(comboBox9.SelectedIndex)
Else
comset = 3
End If
accessrights(1) = Convert.ToByte(comboBox10.SelectedIndex * 16 + comboBox11.SelectedIndex)
accessrights(0) = Convert.ToByte(comboBox12.SelectedIndex * 16 + comboBox13.SelectedIndex)
If (radioButton1.Checked) Then
status = desfirecreatestddatafile(Convert.ToByte(fileid.Value), comset, accessrights(0), filebytes, retsw(0))
Else
status = desfircreatebackupdatafile(Convert.ToByte(fileid.Value), comset, accessrights(0), filebytes, retsw(0))
End If
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status = 53 Then
MessageBox.Show("在应用内创建新文件操作返回异常:" + status.ToString("D") + ",说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf status = 64 Then
If (retstr = "91DE") Then
MessageBox.Show("在应用内创建新文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",可能的原因:1 应用没选择对,2 密码未认证,3 文件已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
MessageBox.Show("在应用内创建新文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",请选择正确的应用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
ElseIf (status > 0) Then
MessageBox.Show("在应用内创建新文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
MessageBox.Show("在应用内创建新文件操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub button12_Click(sender As Object, e As EventArgs) Handles button12.Click
Dim revbuflen(4) As Byte
Dim filesettingsbuf(32) As Byte
Dim retsw(1) As Byte
Dim retstr As String
Dim revstr As String = ""
Dim status As Byte = desfiregetfilesettings(Convert.ToByte(fileid.Value), filesettingsbuf(0), revbuflen(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("读取文件配置信息返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim revlen As ULong = BitConverter.ToInt32(revbuflen, 0)
Dim j As ULong
For j = 0 To revlen - 1
revstr = revstr + filesettingsbuf(j).ToString("X2")
Next
If filesettingsbuf(1) Mod 4 = 3 Then '解析通信模式
comboBox9.SelectedIndex = 2
Else
If (filesettingsbuf(1) Mod 4 = 1) Then
comboBox9.SelectedIndex = 1
Else
comboBox9.SelectedIndex = 0
End If
End If
comboBox10.SelectedIndex = filesettingsbuf(3) \ 16 '只读时需要的密码号
comboBox11.SelectedIndex = filesettingsbuf(3) Mod 16 '只写时需要的密码号
comboBox12.SelectedIndex = filesettingsbuf(2) \ 16 '读写时需要的密码号
comboBox13.SelectedIndex = filesettingsbuf(2) Mod 16 '更改本配置时需要的密码号
filesize.Value = filesettingsbuf(4) + filesettingsbuf(5) * 256 + filesettingsbuf(6) * 65536
MessageBox.Show("读取文件配置返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr) + ",16进制文件配置信息:" + revstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub button14_Click(sender As Object, e As EventArgs) Handles button14.Click
Dim retsw(1) As Byte
Dim retstr As String
Dim status As Byte = desfiredeletefile(Convert.ToByte(fileid.Value), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("删除文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("删除文件操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button17_Click(sender As Object, e As EventArgs) Handles button17.Click
Dim i As Int32 = 0
Dim j As Int32 = 0
Dim maxnum As Int32 = Convert.ToInt32(rwlen.Value) - 1
Dim datastr As String = ""
For j = 0 To maxnum
datastr = datastr + i.ToString("X2") + " "
i = i + 1
If i = 256 Then i = 0
Next
textBox8.Text = datastr
End Sub
Private Sub button15_Click(sender As Object, e As EventArgs) Handles button15.Click
Dim cmd As Byte = &HBD '参数为&HBD表示读数据,为&hBB表示读记录
Dim databuf(8192) As Byte
Dim retsw(1) As Byte
Dim retstr As String
Dim status As Byte = desfirereaddata(cmd, Convert.ToByte(fileid.Value), Convert.ToInt32(rwbegin.Value), Convert.ToInt32(rwlen.Value), databuf(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status = 63) Then
MessageBox.Show("读文件操作返回异常:" + status.ToString("D") + ",说明:权限不足,请先用正确的密钥号认证!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf (status > 0) Then
MessageBox.Show("读文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
Dim datastr As String = ""
Dim j As Int32
For j = 0 To Convert.ToInt32(rwlen.Value) - 1
datastr = datastr + databuf(j).ToString("X2") + " "
Next
textBox8.Text = datastr
MessageBox.Show("读文件操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button16_Click(sender As Object, e As EventArgs) Handles button16.Click
Dim cmd As Byte = &H3D '参参数为&H3D表示写数据,为&h3B表示写记录
Dim databuf(8192) As Byte
Dim retsw(1) As Byte
If Not checkhexstr(textBox8.Text.Trim(), Convert.ToInt32(rwlen.Value), databuf) Then
If (MessageBox.Show("写入数据输入不足或错误,是否要生成写入数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
button17.PerformClick()
Return
End If
End If
Dim status As Byte = desfirewritedata(cmd, Convert.ToByte(fileid.Value), Convert.ToInt32(rwbegin.Value), Convert.ToInt32(rwlen.Value), databuf(0), retsw(0))
Dim retstr As String = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status = 63) Then
MessageBox.Show("写文件操作返回异常:" + status.ToString("D") + ",说明:权限不足,请先用正确的密钥号认证!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf (status > 0) Then
MessageBox.Show("写文件操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
MessageBox.Show("写文件操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub button13_Click(sender As Object, e As EventArgs) Handles button13.Click
Dim status As Byte '函数返回值
Dim comset As Byte '通讯模式
Dim accessrights(1) As Byte '文件访问时的密钥认证方式
Dim retsw(1) As Byte
Dim retstr As String
If (comboBox9.SelectedIndex < 2) Then
comset = Convert.ToByte(comboBox9.SelectedIndex)
Else
comset = 3
End If
accessrights(1) = Convert.ToByte(comboBox10.SelectedIndex * 16 + comboBox11.SelectedIndex)
accessrights(0) = Convert.ToByte(comboBox12.SelectedIndex * 16 + comboBox13.SelectedIndex)
status = desfirechangefilesettings(Convert.ToByte(fileid.Value), comset, accessrights(0), retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status = 53 Then
MessageBox.Show("更改文件配置操作返回异常:" + status.ToString("D") + ",说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf (status > 0) Then
MessageBox.Show("更改文件配置操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
MessageBox.Show("更改文件配置操作卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
Dim retsw(1) As Byte
Dim fidsize(0) As Byte '返回的应用数量,最大19个
Dim fidbuf(16) As Byte '文件ID号数据缓冲,每个ID号占用1个字节,最多16个文件ID号
Dim status As Byte = desfiregetfileids(fidbuf(0), fidsize(0), retsw(0))
Dim retstr As String = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("搜索当前应用内所有文件ID操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim strls1 As String = ""
If fidsize(0) > 0 Then
strls1 = "文件ID号:" & CStr(fidbuf(0))
Dim j As Byte = 1
While j < fidsize(0)
strls1 = strls1 & "," & CStr(fidbuf(j))
j = j + 1
End While
End If
MessageBox.Show("搜索当前应用内所有文件ID操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr) + vbCrLf + "文件数量:" & fidsize(0).ToString("D") + vbCrLf + strls1, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
Dim retsw(1) As Byte
Dim aidsize(0) As Byte '返回的应用数量,最大19个
Dim aidbuf(56) As Byte '应用AID号数据缓冲,每个AID号占用3个字节,最多19个应AID号
Dim status As Byte = desfiregetapplicationids(aidbuf(0), aidsize(0), retsw(0))
Dim retstr As String = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If (status > 0) Then
MessageBox.Show("搜索当前卡片内所有应用AID操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim strls1 As String = ""
If aidsize(0) > 0 Then
strls1 = "应用AID:" & aidbuf(0).ToString("X2") & aidbuf(1).ToString("X2") & aidbuf(2).ToString("X2")
Dim j As Byte = 1
While j < aidsize(0)
strls1 = strls1 & "," & aidbuf(j * 3).ToString("X2") & aidbuf(j * 3 + 1).ToString("X2") & aidbuf(j * 3 + 2).ToString("X2")
j = j + 1
End While
End If
MessageBox.Show("搜索当前卡片内所有应用AID操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr) + vbCrLf + "应用数量:" & aidsize(0).ToString("D") + vbCrLf + strls1, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
Dim aidbuf(2) As Byte
Dim keysetting(0) As Byte
Dim keysize(0) As Byte
Dim retsw(1) As Byte
Dim keynum As Integer
If Not checkhexstr(textBox5.Text.Trim(), 3, aidbuf) Then
MessageBox.Show("十六进制AID输入错误,请输入3字节的16进制AID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim status As Byte = desfiregetkeysettings(aidbuf(0), keysetting(0), keysize(0), retsw(0))
Dim retstr As String = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status = 94 Then
MessageBox.Show("请先执行选择应用AID:" + textBox5.Text.Trim(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf status > 0 Then
MessageBox.Show("读取应用的配置信息操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
comboBox4.SelectedIndex = keysetting(0) \ 16 '更改应用密钥(非应用主密钥) 需要验证的选项
If (keysetting(0) And &H8) > 0 Then
comboBox5.SelectedIndex = 1 '需要验证应用主密钥(默认)
Else
comboBox5.SelectedIndex = 0 '配置值不允许修改(锁定)
End If
If (keysetting(0) And &H4) > 0 Then
comboBox6.SelectedIndex = 1 '无需验证应用主密钥(默认)
Else
comboBox6.SelectedIndex = 0 '需要验证应用主密钥
End If
If (keysetting(0) And &H2) > 0 Then
comboBox7.SelectedIndex = 1 '需要验证应用主密钥
Else
comboBox7.SelectedIndex = 0 '无需验证应用主密钥(默认)
End If
If (keysetting(0) And &H1) > 0 Then
comboBox8.SelectedIndex = 1 '允许更改主密钥(默认)
Else
comboBox8.SelectedIndex = 0 '锁定主密钥
End If
keynum = (keysize(0) Mod 64)
keynumber.Value = keynum
If (keysize(0) And &HC0) = &H80 Then
comboBox2.SelectedIndex = 3
ElseIf (keysize(0) And &HC0) = &H40 Then
comboBox2.SelectedIndex = 2
Else
comboBox2.SelectedIndex = 0 '也可能是comboBox2.SelectedIndex = 1
End If
MessageBox.Show("读取应用的密钥配置信息操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr) + vbCrLf + "密钥配置:" & keysetting(0).ToString("X2") + vbCrLf + "密钥数量:" + keynum.ToString("D"), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
Dim aidbuf(3) As Byte
Dim retsw(1) As Byte
Dim retstr As String
If Not checkhexstr(textBox5.Text.Trim(), 3, aidbuf) Then
MessageBox.Show("十六进制AID输入错误,请输入3字节的16进制AID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim keysetting As Byte = Convert.ToByte(comboBox4.SelectedIndex * 16 + comboBox5.SelectedIndex * 8 + comboBox6.SelectedIndex * 4 + comboBox7.SelectedIndex * 2 + comboBox8.SelectedIndex)
Dim status As Byte = desfirechangekeysettings(aidbuf(0), keysetting, retsw(0))
retstr = retsw(0).ToString("X2") + retsw(1).ToString("X2")
If status = 94 Then
MessageBox.Show("请先执行选择应用AID:" + textBox5.Text.Trim(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf status = 63 Then
MessageBox.Show("需要先验证当前应用的主密钥!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
ElseIf (status > 0) Then
MessageBox.Show("更改应用密钥配置操作返回异常:" + status.ToString("D") + ",卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
MessageBox.Show("更改应用密钥配置操作,卡片返回代码:" + retstr + ",说明:" + RetTextFromStr(retstr), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class