22using LibLogicalAccess . Card ;
33using LibLogicalAccess . Reader ;
44using System ;
5+ using System . Windows . Forms ;
56
67namespace 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}
0 commit comments