Newer
Older
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace uFRSimple
{
using DL_STATUS = System.UInt32;
public partial class frmuFrSimple : Form
{
InitializeComponent();
cboSoundMode.SelectedItem = cboSoundMode.Items[0];
cboLightMode.SelectedItem = cboLightMode.Items[0];
}
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
//DLOGIC CARD TYPE
public enum DLCARDTYPE
{
DL_MIFARE_ULTRALIGHT = 0x01,
DL_MIFARE_ULTRALIGHT_EV1_11 = 0x02,
DL_MIFARE_ULTRALIGHT_EV1_21 = 0x03,
DL_MIFARE_ULTRALIGHT_C = 0x04,
DL_NTAG_203 = 0x05,
DL_NTAG_210 = 0x06,
DL_NTAG_212 = 0x07,
DL_NTAG_213 = 0x08,
DL_NTAG_215 = 0x09,
DL_NTAG_216 = 0x0A,
DL_MIFARE_MINI = 0x20,
DL_MIFARE_CLASSIC_1K = 0x21,
DL_MIFARE_CLASSIC_4K = 0x22,
DL_MIFARE_PLUS_S_2K = 0x23,
DL_MIFARE_PLUS_S_4K = 0x24,
DL_MIFARE_PLUS_X_2K = 0x25,
DL_MIFARE_PLUS_X_4K = 0x26,
DL_MIFARE_DESFIRE = 0x27,
DL_MIFARE_DESFIRE_EV1_2K = 0x28,
DL_MIFARE_DESFIRE_EV1_4K = 0x29,
DL_MIFARE_DESFIRE_EV1_8K = 0x2A
}
//card type
const byte MIFARE_CLASSIC_1k = 0x08,
MIFARE_CLASSIC_4k = 0x18;
//authenticate
const byte MIFARE_AUTHENT1A = 0x60,
MIFARE_AUTHENT1B = 0x61;
const byte DL_OK = 0x00,
KEY_INDEX = 0x00;
//for error
const byte FRES_OK_LIGHT = 0x04,
FRES_OK_SOUND = 0x00,
FERR_LIGHT = 0x02,
FERR_SOUND = 0x00;
// sectors and blocks
const byte MAX_SECTORS_1k = 0x10,
MAX_SECTORS_4k = 0x28,
MAX_BLOCK = 0x0F;
APPROPRIATE_FORMAT = "You must enter the appropriate format !\nEnter a number between 0 and 255 or 0 and FF hexadecimal !";
const string NEW_CARD_KEY_A = "txtNewCardKeyA",
NEW_CARD_KEY_B = "txtNewCardKeyB",
NEW_READER_KEY = "txtNewReaderKey";
private Boolean boCONN = false,
boThreadStart = false,
boFunctionOn = false;
private byte bKeyIndex = 0;
private byte bTypeOfCard = 0;
private string[] ERROR_CODES;
void SetStatusBar(DL_STATUS result, System.Windows.Forms.StatusStrip Status_bar)
{
Status_bar.Items[1].Text = "0x" + result.ToString("X2");
}
void CreateKey(byte bKeyWidth, byte bKeyHeight, byte bKeyX, byte bKeyY, string bKeyName, System.Windows.Forms.Panel pnlContainer)
{
for (byte br = 0; br < 6; br++)
{
TextBox TB = new TextBox();
TB.Width = bKeyWidth;
TB.Height = bKeyHeight;
TB.Font = new Font("Verdana", 8, FontStyle.Bold);
TB.Name = bKeyName;
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
TB.MaxLength = 3;
TB.KeyPress += new KeyPressEventHandler(TB_KeyPress);
TB.Leave += TB_Leave;
TB.Location = new Point((bKeyX + (bKeyWidth * br) + 2), bKeyY);
TB.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
pnlContainer.Controls.Add(TB);
}
}
void TB_Leave(object sender, EventArgs e)
{
TextBox TB = (TextBox)sender;
try
{
if (TB.Text.Trim() == String.Empty)
{
TB.Undo();
return;
}
if (chkCardKeysHex.Checked && TB.Name == NEW_CARD_KEY_A || chkCardKeysHex.Checked && TB.Name == NEW_CARD_KEY_B ||
chkReaderKeyHex.Checked && TB.Name == NEW_READER_KEY) return;
System.Convert.ToByte(TB.Text, 10);
}
catch (OverflowException OWE)
{
MessageBox.Show("Wrong key ! \n Input value must not be greater than 255 !", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error); TB.SelectAll();
TB.Focus();
}
}
void TB_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox TB = (TextBox)sender;
if (e.KeyChar == (char)Keys.Back) return;
if (chkCardKeysHex.Checked && TB.Name == NEW_CARD_KEY_A || chkCardKeysHex.Checked && TB.Name == NEW_CARD_KEY_B ||
chkReaderKeyHex.Checked && TB.Name == NEW_READER_KEY)
{
TB.MaxLength = 2;
if (e.KeyChar == (char)Keys.Back) return;
if (Regex.IsMatch(e.KeyChar.ToString(), "[^a-fA-F0-9]")) e.Handled = true;
}
else
{
TB.MaxLength = 3;
if (Regex.IsMatch(e.KeyChar.ToString(), "[^0-9]")) e.Handled = true;
}
}
private void mniExitItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
get
{
return boFunctionOn;
}
set
{
boFunctionOn = value;
}
}
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
private Boolean LoopStatus
{
get
{
return boThreadStart ;
}
set
{
boThreadStart = value;
}
}
private void MainThread()
{
ulong ulReaderType = 0,
ulReaderSerial = 0,
ulCardSerial = 0;
byte bUidSize = 0,
bDLCardType = 0,
bCardType = 0;
byte[] baCardUID = new byte[9];
String sBuffer = "";
DL_STATUS iRResult,
iCResult;
LoopStatus =true;
{
boCONN = true;
stbReader.Items[0].Text = "CONNECTED";
{
txtReaderType .Clear();
txtReaderSerial.Clear();
txtCardType .Clear();
txtCardSerial .Clear();
txtCardSerial .Clear();
stbReader.Items[0].Text = "NOT CONNECTED";
}
}
unsafe
{
iRResult = uFrCoder1x.GetReaderType(&ulReaderType);
if ( iRResult == DL_OK)
{
txtReaderType.Text = "0x" + ulReaderType.ToString("X");
iRResult = uFrCoder1x.GetReaderSerialNumber(&ulReaderSerial);
if (iRResult == DL_OK)
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
txtReaderSerial.Text = "0x" + ulReaderSerial.ToString("X");
iCResult = uFrCoder1x.GetDlogicCardType(&bDLCardType);
if (iCResult == DL_OK)
{
if (bDLCardType == (byte)DLCARDTYPE.DL_NTAG_203 ||
bDLCardType == (byte)DLCARDTYPE.DL_MIFARE_ULTRALIGHT ||
bDLCardType == (byte)DLCARDTYPE.DL_MIFARE_ULTRALIGHT_C)
{
btnFormatCard.Enabled = false;
}
else
{
btnFormatCard.Enabled = true;
}
uFrCoder1x.GetCardIdEx(&bCardType, pCardUID, &bUidSize);
for (byte bBr = 0; bBr < bUidSize; bBr++)
{
sBuffer += baCardUID[bBr].ToString("X");
}
txtCardType.Text = "0x" + bDLCardType.ToString("X2");
txtUIDSize.Text = "0x" + bUidSize.ToString("X2");
txtCardSerial.Text = "0x" + sBuffer;
bTypeOfCard = bDLCardType;
SetStatusBar(iCResult, stbCardStatus);
}
else
{
btnFormatCard.Enabled = true;
txtCardType .Clear();
txtUIDSize .Clear();
txtCardSerial .Clear();
SetStatusBar(iCResult, stbCardStatus);
}
txtReaderType .Clear();
txtReaderSerial.Clear();
txtCardType .Clear();
txtCardSerial .Clear();
uFrCoder1x.ReaderClose();
}
private void Timer_Tick(object sender, EventArgs e)
{
MainThread();
}
private void btnReaderUISignal_Click(object sender, EventArgs e)
{
if (FunctionOn || LoopStatus ) return;
try
{
FunctionOn =true ;
uFrCoder1x.ReaderUISignal(cboLightMode.SelectedIndex, cboSoundMode.SelectedIndex);
}
finally
{
FunctionOn=false;
}
private void DecHexConversion(String sTextBoxName, Boolean CheckBoxChecked, System.Windows.Forms.Panel Container)
{
foreach (Control ctrlKey in Container.Controls)
{
if (ctrlKey.Name == sTextBoxName)
{
if (ctrlKey.Text == String.Empty)
continue;
else
{
ctrlKey.Text = CheckBoxChecked ? System.Convert.ToByte(ctrlKey.Text).ToString("X")
: System.Convert.ToUInt16(ctrlKey.Text, 16).ToString();
}
}
}
}
private void btnLinearRead_Click(object sender, EventArgs e)
{
if (txtReadLinearAddress.Text.Trim() == String.Empty)
{
MessageBox.Show("You must enter the LINEAR ADDRESS !", "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtReadLinearAddress.Focus();
return;
}
if (txtReadDataLength.Text.Trim() == String.Empty)
{
MessageBox.Show("You must enter the DATA LENGTH !", "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtReadDataLength.Focus();
ushort ushLinearAddress = System.Convert.ToUInt16(txtReadLinearAddress.Text);
ushort ushDataLength = System.Convert.ToUInt16(txtReadDataLength.Text);
byte[] baReadData = new byte[ushDataLength];
ushort ushBytesRet = 0;
byte bAuthMode = (rbAUTH1A.Checked) ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B;
DL_STATUS iFResult;
{
fixed (byte* PData = baReadData)
iFResult = uFrCoder1x.LinearRead(PData, ushLinearAddress, ushDataLength, &ushBytesRet, bAuthMode, bKeyIndex);
txtReadData.Text = System.Text.Encoding.ASCII.GetString(baReadData);
txtReadBytes.Text = ushBytesRet.ToString();
SetStatusBar(iFResult, stbFunction);
uFrCoder1x.ReaderUISignal(FRES_OK_LIGHT, FRES_OK_SOUND);
{
txtReadData.Text = System.Text.Encoding.ASCII.GetString(baReadData);
txtReadBytes.Text = ushBytesRet.ToString();
SetStatusBar(iFResult, stbFunction);
uFrCoder1x.ReaderUISignal(FERR_LIGHT, FERR_SOUND);
}
}
catch (System.FormatException ex)
{
MessageBox.Show(CONVERT_ERROR, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Exception exc)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void btnWriteData_Click(object sender, EventArgs e)
{
if (txtWriteData.Text.Trim() == String.Empty)
{
MessageBox.Show("You must enter any data !", "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (txtWriteLinearAddress.Text.Trim() == String.Empty)
{
MessageBox.Show("You must enter the LINEAR ADDRESS !", "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtWriteLinearAddress.Focus();
return;
}
if (txtWriteDataLength.Text.Trim() == String.Empty)
{
MessageBox.Show("You must enter the DATA LENGTH !", "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtWriteDataLength.Focus();
ushort ushLinearAddress = System.Convert.ToUInt16(txtWriteLinearAddress.Text);
ushort ushDataLength = System.Convert.ToUInt16(txtWriteDataLength.Text);
byte bAuthMode = (rbAUTH1A.Checked) ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B;
ushort ushBytesRet;
byte[] baWriteData = new byte[ushDataLength];
baWriteData = System.Text.Encoding.ASCII.GetBytes(txtWriteData.Text);
DL_STATUS iFResult;
fixed (byte* PData = baWriteData)
iFResult = uFrCoder1x.LinearWrite(PData, ushLinearAddress, ushDataLength, &ushBytesRet, bAuthMode, bKeyIndex);
txtBytesWritten.Text = ushBytesRet.ToString();
SetStatusBar(iFResult, stbFunction);
uFrCoder1x.ReaderUISignal(FRES_OK_LIGHT, FRES_OK_SOUND);
txtBytesWritten.Text = ushBytesRet.ToString();
SetStatusBar(iFResult, stbFunction);
uFrCoder1x.ReaderUISignal(FERR_LIGHT, FERR_SOUND);
}
}
catch (System.FormatException ex)
{
MessageBox.Show(CONVERT_ERROR, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Exception exc)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
}
}
private byte[] WriteArray(byte[] bGetBytesArray, int iDataLength, int iMaxBytes)
{
byte[] bCloneArray = new byte[iMaxBytes];
Array.Copy(bGetBytesArray, bCloneArray, bGetBytesArray.Length);
for (int br = bGetBytesArray.Length; br < iDataLength; br++)
{
bCloneArray[br] = 32;
}
return bCloneArray;
}
private void chkCardKeysHex_Click(object sender, EventArgs e)
{
DecHexConversion(NEW_CARD_KEY_A, chkCardKeysHex.Checked, pnlCardKeys);
DecHexConversion(NEW_CARD_KEY_B, chkCardKeysHex.Checked, pnlCardKeys);
}
private void chkReaderKeyHex_Click(object sender, EventArgs e)
{
DecHexConversion(NEW_READER_KEY, chkReaderKeyHex.Checked, pnlReaderKey);
}
private void btnEnterReaderKey_Click(object sender, EventArgs e)
{
if (FunctionOn || LoopStatus ) return;
try
{
FunctionOn = true ;
byte[] baReaderKey = new byte[6];
byte bCounter = 0;
DL_STATUS iFResult;
foreach (Control CtrlKey in pnlReaderKey.Controls)
{
if (CtrlKey.Name == NEW_READER_KEY)
{
baReaderKey[bCounter] = chkReaderKeyHex.Checked ? System.Convert.ToByte(int.Parse(CtrlKey.Text, System.Globalization.NumberStyles.HexNumber)) : System.Convert.ToByte(CtrlKey.Text, 10);
bCounter++;
}
}
unsafe
{
fixed (byte* PReaderKey = baReaderKey)
{
uFrCoder1x.ReaderUISignal(FRES_OK_LIGHT, FRES_OK_SOUND);
SetStatusBar(iFResult, stbFunction);
MessageBox.Show("Reader key is formatted successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
uFrCoder1x.ReaderUISignal(FERR_LIGHT, FERR_SOUND);
SetStatusBar(iFResult, stbFunction);
MessageBox.Show("Reader key is not formatted successfully !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (FormatException FE)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(ArgumentOutOfRangeException AURE)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private void btnFormatCard_Click(object sender, EventArgs e)
{
if (FunctionOn || LoopStatus ) return;
try
{
FunctionOn =true ;
byte bBlockAccessBits = 0,
bSectorTrailersAccess_bits = 1,
bSectorTrailersByte9 = 45,
bCounter = 0,
bSectorsFormatted = 0;
byte bAuthMode = rbAUTH1A.Checked ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B;
byte[] baKeyA = new byte[6];
byte[] baKeyB = new byte[6];
txtSectorFormatted.Clear();
DL_STATUS iFResult;
foreach (Control ctrlkey in pnlCardKeys.Controls)
{
if (ctrlkey.Name == NEW_CARD_KEY_A)
{
baKeyA[bCounter] = chkCardKeysHex.Checked ? System.Convert.ToByte(int.Parse(ctrlkey.Text, System.Globalization.NumberStyles.HexNumber)) : System.Convert.ToByte(ctrlkey.Text, 10);
bCounter++;
}
}
bCounter = 0;
foreach (Control ctrlkey in pnlCardKeys.Controls)
{
if (ctrlkey.Name == NEW_CARD_KEY_B)
{
baKeyB[bCounter] = chkCardKeysHex.Checked ? System.Convert.ToByte(int.Parse(ctrlkey.Text, System.Globalization.NumberStyles.HexNumber)) : System.Convert.ToByte(ctrlkey.Text);
bCounter++;
}
}
unsafe
{
fixed (byte* PKEY_A = baKeyA, PKEY_B = baKeyB)
iFResult = uFrCoder1x.LinearFormatCard(PKEY_A, bBlockAccessBits, bSectorTrailersAccess_bits, bSectorTrailersByte9, PKEY_B, &bSectorsFormatted, bAuthMode, bKeyIndex);
txtSectorFormatted.Text = bSectorsFormatted.ToString();
MessageBox.Show("Card keys are formatted successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
txtSectorFormatted.Text = bSectorsFormatted.ToString();
MessageBox.Show("Card keys are not formatted successfully !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (FormatException FE)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentOutOfRangeException AURE)
{
MessageBox.Show(APPROPRIATE_FORMAT, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
private void frmuFrSimple_Load(object sender, EventArgs e)
{
CreateKey(32, 21, 3, 10, NEW_CARD_KEY_A, pnlCardKeys);
CreateKey(32, 21, 3, 33, NEW_CARD_KEY_B, pnlCardKeys);
CreateKey(32, 21, 53, 10, NEW_READER_KEY, pnlReaderKey);
bKeyIndex = System.Convert.ToByte(txtReaderKeyIndex.Text);
int[] iErrorValues = (int[])Enum.GetValues(typeof(ERRORCODES));
string[] sErrorNames = Enum.GetNames(typeof(ERRORCODES));
ERROR_CODES=new string[iErrorValues.Max()+1];
for (int i = 0; i < iErrorValues.Length; i++)
ERROR_CODES[iErrorValues[i]] = sErrorNames[i];
}
private void txtWriteData_TextChanged(object sender, EventArgs e)
{
txtWriteDataLength.Text=txtWriteData.Text.Length.ToString();
}