Skip to content

Commit 666d5e6

Browse files
committed
Update to .NET 8
Add Key Diversification parameters Cath exception to display error message
1 parent 2978629 commit 666d5e6

23 files changed

+1482
-894
lines changed

RWCard-DESFire.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2013
44
VisualStudioVersion = 12.0.40629.0
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RWCard-DESFire", "RWCard-DESFire\RWCard-DESFire.csproj", "{BBA4D945-42EB-4A3C-BA6D-EB2556DCB233}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RWCard-DESFire", "RWCard-DESFire\RWCard-DESFire.csproj", "{BBA4D945-42EB-4A3C-BA6D-EB2556DCB233}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution

RWCard-DESFire/App.config

Lines changed: 0 additions & 6 deletions
This file was deleted.

RWCard-DESFire/AuthWithSAMSample.Designer.cs

Lines changed: 234 additions & 154 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RWCard-DESFire/AuthWithSAMSample.cs

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using LibLogicalAccess.Card;
33
using LibLogicalAccess.Reader;
44
using System;
5+
using System.Windows.Forms;
56

67
namespace RWCard_DESFire
78
{
@@ -23,7 +24,7 @@ public DESFireKey GetUnlockKey()
2324
{
2425
var key = new DESFireKey();
2526
key.setKeyType(DESFireKeyType.DF_KEY_AES);
26-
key.fromString(tbxKeyValue.Text);
27+
key.fromString(KeyHelper.FormatKeyString(tbxKeyValue.Text));
2728
return key;
2829
}
2930

@@ -32,54 +33,95 @@ public byte GetKeySlot()
3233
return (byte)nupKeySlot.Value;
3334
}
3435

36+
public byte GetKeyVersion()
37+
{
38+
return (byte)nupKeyVersion.Value;
39+
}
40+
3541
private void btnInit_Click(object sender, System.EventArgs e)
3642
{
37-
var samConfig = readerSelectionControl.GetReaderConfiguration();
38-
39-
var ru = ReaderConfig.getReaderUnit() as ISO7816ReaderUnit;
40-
if (ru == null)
41-
throw new Exception("The Reader Unit needs to be of type ISO7816.");
42-
43-
// We let know the contactless reader about the SAM reader settings
44-
var ruConfig = ru.getConfiguration() as ISO7816ReaderUnitConfiguration;
45-
ruConfig.setSAMReaderName(samConfig.getReaderUnit().getName());
46-
ruConfig.setSAMUnlockKey(GetUnlockKey(), GetUnlockKeyNo());
47-
ruConfig.setSAMType("SAM_AV2");
48-
ru.setConfiguration(ruConfig);
49-
50-
// This is going to connect/unlock the SAM reader/chip as well
51-
ru.connectToReader();
52-
if (ru.waitInsertion(dfparams.GetTimeout()))
43+
try
5344
{
54-
ru.connect();
55-
var chip = ru.getSingleChip();
56-
if (chip.getGenericCardType() != "DESFire")
57-
throw new Exception("DESFire chip required");
45+
var samConfig = readerSelectionControl.GetReaderConfiguration();
46+
47+
var ru = ReaderConfig.getReaderUnit() as ISO7816ReaderUnit;
48+
if (ru == null)
49+
throw new Exception("The Reader Unit needs to be of type ISO7816.");
50+
51+
// We let know the contactless reader about the SAM reader settings
52+
var ruConfig = ru.getConfiguration() as ISO7816ReaderUnitConfiguration;
53+
ruConfig.setSAMReaderName(samConfig.getReaderUnit().getName());
54+
ruConfig.setSAMUnlockKey(GetUnlockKey(), GetUnlockKeyNo());
55+
ruConfig.setSAMType("SAM_AV2");
56+
ru.setConfiguration(ruConfig);
57+
58+
// This is going to connect/unlock the SAM reader/chip as well
59+
ru.setCardType("DESFireEV1");
60+
ru.connectToReader();
61+
if (ru.waitInsertion(dfparams.GetTimeout()))
62+
{
63+
ru.connect();
64+
var chip = ru.getSingleChip();
65+
if (chip.getGenericCardType() != "DESFire")
66+
throw new Exception("DESFire chip required");
5867

59-
cmd = (chip as DESFireChip).getDESFireCommands();
68+
cmd = (chip as DESFireChip).getDESFireCommands();
69+
}
70+
}
71+
catch (Exception ex)
72+
{
73+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
6074
}
6175
}
6276

6377
private void btnRunAuthenticate_Click(object sender, System.EventArgs e)
6478
{
65-
cmd.selectApplication(dfparams.GetApplicationID());
79+
try
80+
{
81+
cmd.selectApplication(dfparams.GetApplicationID());
6682

67-
var key = dfparams.GetKey();
68-
// Here is where we define to use the SAM for furthers key usage (authenticate/changeKey commands)
69-
var ks = new SAMKeyStorage();
70-
ks.setKeySlot(GetKeySlot());
71-
key.setKeyStorage(ks);
83+
var key = keyparams.GetKey();
84+
// Here is where we define to use the SAM for furthers key usage (authenticate/changeKey commands)
85+
var ks = new SAMKeyStorage();
86+
ks.setKeySlot(GetKeySlot());
87+
key.setKeyVersion(GetKeyVersion());
88+
key.setKeyStorage(ks);
7289

73-
cmd.authenticate(dfparams.GetKeyNo(), key);
90+
cmd.authenticate(dfparams.GetKeyNo(), key);
91+
}
92+
catch (Exception ex)
93+
{
94+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
95+
}
7496
}
7597

7698
private void btnUnInit_Click(object sender, System.EventArgs e)
7799
{
78-
var ru = ReaderConfig.getReaderUnit();
79-
ru.disconnect();
80-
ru.waitRemoval(dfparams.GetTimeout());
81-
// Will release SAM resources too
82-
ru.disconnectFromReader();
100+
try
101+
{
102+
var ru = ReaderConfig.getReaderUnit();
103+
ru.disconnect();
104+
ru.waitRemoval(dfparams.GetTimeout());
105+
// Will release SAM resources too
106+
ru.disconnectFromReader();
107+
}
108+
catch (Exception ex)
109+
{
110+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
111+
}
112+
}
113+
114+
private void btnReadData_Click(object sender, EventArgs e)
115+
{
116+
try
117+
{
118+
var data = cmd.readData(dfparams.GetFileNo(), 0, 8, EncryptionMode.CM_ENCRYPT);
119+
MessageBox.Show(Convert.ToHexString(data.ToArray()));
120+
}
121+
catch (Exception ex)
122+
{
123+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
124+
}
83125
}
84126
}
85127
}

RWCard-DESFire/AuthWithSAMSample.resx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
3+
<!--
44
Microsoft ResX Schema
5-
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
5151
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->

RWCard-DESFire/CardServiceTask.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ public T Run<T>(Func<T, IChip, Location, AccessInfo, AccessInfo, T> action, T pa
6767
readerUnit.waitRemoval(2000);
6868
}
6969
else
70-
MessageBox.Show("No card detected !", "Error:", MessageBoxButtons.OK);
70+
MessageBox.Show("No card detected !", Properties.Resources.Error, MessageBoxButtons.OK);
7171
readerUnit.disconnectFromReader();
7272
}
7373
else
7474
throw new Exception("Cannot connect to the PCSC reader.");
7575
}
7676
catch (Exception ex)
7777
{
78-
MessageBox.Show(ex.Message, "Error:", MessageBoxButtons.OK);
78+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButtons.OK);
7979
}
8080
return ret;
8181
}

RWCard-DESFire/CardStorageServiceSample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private string ReadData(string tmp , IChip chip, Location location, AccessInfo a
4141
var data = (storage as StorageCardService).readData(location, aiToWrite, 16, CardBehavior.CB_DEFAULT);
4242
ret = GetString(data);
4343

44-
MessageBox.Show("Read succeeded", "Info:", MessageBoxButtons.OK);
44+
MessageBox.Show("Read succeeded", "Info", MessageBoxButtons.OK);
4545
}
4646
else
4747
throw new Exception("Impossible to get card service.");
@@ -68,7 +68,7 @@ private string WriteData(string data, IChip chip, Location location, AccessInfo
6868
var arrayData = GetBytes(data, 16);
6969
(storage as StorageCardService).writeData(location, aiToUse, aiToWrite, arrayData, CardBehavior.CB_DEFAULT);
7070

71-
MessageBox.Show("Write succeeded", "Info:", MessageBoxButtons.OK);
71+
MessageBox.Show("Write succeeded", "Info", MessageBoxButtons.OK);
7272
}
7373
else
7474
throw new Exception("Impossible to get card service.");

0 commit comments

Comments
 (0)