-
Notifications
You must be signed in to change notification settings - Fork 2
/
MifareTag.cs
74 lines (59 loc) · 1.96 KB
/
MifareTag.cs
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
using SharpFreeFare.PInvoke;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SharpFreeFare
{
public class MifareTag : IDisposable
{
public IntPtr TagPointer;
internal MifareTag(IntPtr mifare_tag)
{
this.TagPointer = mifare_tag;
}
public mifare_tag GetStructure()
{
return (mifare_tag)Marshal.PtrToStructure(TagPointer, typeof(mifare_tag));
}
public void Connect()
{
var result = Functions.mifare_desfire_connect(TagPointer);
if (result < 0)
{
throw new Exception("Can't connect to Mifare DESFire target.");
}
}
public void SelectApplication(uint applicationId)
{
var aidPtr = Functions.mifare_desfire_aid_new(applicationId);
var result = Functions.mifare_desfire_select_application(TagPointer, aidPtr);
if (result < 0)
{
throw new Exception("mifare_desfire_select_application failed.");
}
}
public bool Authenticate(byte keyNumber, byte[] key)
{
IntPtr keyPtr = Functions.mifare_desfire_3des_key_new(key);
var result = Functions.mifare_desfire_authenticate(TagPointer, keyNumber, keyPtr);
if (result < 0)
{
return false;
}
return true;
}
public byte[] ReadData(byte fileID, int startOffset, uint length)
{
byte[] data = new byte[length];
Functions.mifare_desfire_read_data(TagPointer, fileID, startOffset, length, data);
return data;
}
public void Dispose()
{
Functions.mifare_desfire_disconnect(TagPointer);
}
}
}