/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2017-07-19
* Time: 14:54
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace CardTest
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
//华大读卡器HD-100
[DllImport("SSSE32.dll")]
public static extern int ICC_Reader_Open ( string name);
//蜂鸣器
[DllImport("SSSE32.dll", EntryPoint = "ICC_PosBeep")]
public static extern System.Int32 ICC_PosBeep(System.Int32 ReaderHandle, Int16 time);
//将字符命令流转为16进制流
[DllImport("SSSE32.dll", EntryPoint = "StrToHex")]
public static extern System.Int32 StrToHex(System.Byte[] strIn, System.Int32 inLen, System.Byte[] strOut);
//密钥认证(模式一),该函数自动调用 存储 于 设备里面的 密钥 进行认证(要先装载密钥)
[DllImport("SSSE32.dll", EntryPoint = "PICC_Reader_Authentication")]
public static extern System.Int32 PICC_Reader_Authentication(System.Int32 ReaderHandle,System.Byte Mode,System.Byte SecNr);
//密钥认证(模式二),使用用户输入传入的 password
//注意:输入的是12位的密钥,例如12个f,但是password必须是6个字节的密钥,需要用StrToHex函数处理。
[DllImport("SSSE32.dll", EntryPoint = "PICC_Reader_Authentication_Pass")]
public static extern System.Int32 PICC_Reader_Authentication_Pass(System.Int32 ReaderHandle,System.Byte Mode,System.Byte SecNr,System.Byte[] PassWord);
//读卡
[DllImport("SSSE32.dll", EntryPoint = "PICC_Reader_Read")]
public static extern System.Int32 PICC_Reader_Read(System.Int32 ReaderHandle, System.Byte Addr,System.Byte[] Data);
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void MainFormLoad(object sender, EventArgs e)
{
for(int i=0; i<17; i++) {
this.comboBox1.Items.Add(i);
}
for(int i=0; i<64; i++) {
this.comboBox2.Items.Add(i);
}
}
void Button1Click(object sender, EventArgs e)
{
read((byte)this.comboBox1.SelectedIndex, (byte)this.comboBox2.SelectedIndex);
// 读取所有数据
// for(int i=0; i<16; i++) {
// for(int j=0; j<64; j++) {
// read((byte)i, (byte)j);
// }
// }
}
void Button2Click(object sender, EventArgs e)
{
this.textBox1.Clear();
}
void read(byte one, byte two) {
string result = "", name = "USB1";
int status = 0, handle = 0;
handle = ICC_Reader_Open( name);
byte[] Data = new byte[16];//存 读出的数据
if(handle > 0) {
ICC_PosBeep(handle, 1000);
string str = "ffffffffffff";//界面输入的 12位密钥
//密钥需要 从 字符串 类型 转换为 byte数组 类型
byte[] data = Encoding.UTF8.GetBytes(str);//string ---> byte[]
byte[] dataOut = new byte[6];//存 处理后 输出来的 密钥(6个字节的)
int test = StrToHex(data, 12, dataOut);//将字符命令流转为16进制流
this.textBox1.AppendText(one + "扇区-" + two + "块:\r\n");
int tt = PICC_Reader_Authentication_Pass(handle, 0x61 , (byte)(one), dataOut);
//使用参数PassWord传入的Key进行认证;
string result2 = "";
//认证成功
if (tt == 0) {
int tmp = PICC_Reader_Read(handle, two, Data);//读(HEX)
if(0 == tmp) {
for ( int i = 1; i < 11; i++ ) {
result2 += (char)Data[i] + "";
}
this.textBox1.AppendText(result2);
this.textBox1.AppendText("\r\n");
} else {
// this.textBox1.AppendText("数据读取失败!失败代码:" + tmp + ";\r\n");
}
} else {
// this.textBox1.AppendText("密码认证失败!失败代码:" + tt + ";\r\n");
}
} else {
// this.textBox1.AppendText("硬件链接失败!\r\n");
}
}
}
}