MainForm.cs 4.62 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
/*
* 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");
}
}
}
}