Skip to content
Form1.cs 56.2 KiB
Newer Older
developer29's avatar
developer29 committed
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uFR;

namespace uFR_AES_tester
{
    using DL_STATUS = System.UInt32;

    public partial class Form1 : Form
    {
        DL_STATUS status;

        public Form1()
        {
            InitializeComponent();
        }

        public static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        private void bStoreAESKeyIntoReader_Click(object sender, EventArgs e)
        {
            byte[] aes_key = new byte[16];
            byte aes_key_no = Byte.Parse(InternalKeyNumberCB.Text);

            aes_key = StringToByteArray(AesKeyInternalTB.Text);

            status = (UInt32)uFCoder.uFR_int_DesfireWriteAesKey(aes_key_no, aes_key);

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireWriteAesKey = " + uFCoder.status2str((uFR.DL_STATUS)status);
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireWriteAesKey = " + uFCoder.status2str((uFR.DL_STATUS)status);
            }
        }

        private void bInternalKeysLock_Click(object sender, EventArgs e)
        {
            String pass = PasswordTB.Text;

            if (pass.Length != 8)
            {
                MessageBox.Show("Pasword must be 8 character length");
            }
            else
            {
                status = (UInt32)uFCoder.ReaderKeysLock(pass.ToCharArray());

                if (status == 0)
                {
                    StatusLabel.Text = "Operation completed";
                    StatusRTB.Text = "ReaderKeysLock = " + uFCoder.status2str((uFR.DL_STATUS)status);
                }
                else
                {
                    StatusLabel.Text = "Operation not completed";
                    StatusRTB.Text = "ReaderKeysLock = " + uFCoder.status2str((uFR.DL_STATUS)status);
                }
            }
        }

        private void bInternalKeysUnlock_Click(object sender, EventArgs e)
        {
            String pass = PasswordTB.Text;

            if (pass.Length != 8)
            {
                MessageBox.Show("Pasword must be 8 character length");
            }
            else
            {
                status = (UInt32)uFCoder.ReaderKeysUnlock(pass.ToCharArray());

                if (status == 0)
                {
                    StatusLabel.Text = "Operation completed";
                    StatusRTB.Text = "ReaderKeysUnlock = " + uFCoder.status2str((uFR.DL_STATUS)status);
                }
                else
                {
                    StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status); ;
                    StatusRTB.Text = "ReaderKeysUnlock = " + uFCoder.status2str((uFR.DL_STATUS)status);
                }
            }
        }

        private void bChangeAesKeyCard_Click(object sender, EventArgs e)
        {
            byte[] old_key = new byte[16];
            byte[] new_key = new byte[16];
            byte aid_key_no = Byte.Parse(AIDKeyNrChangeCH.Text);
            UInt16 card_status;
            UInt16 exec_time;

            old_key = StringToByteArray(OldKeyTB.Text);
            new_key = StringToByteArray(NewKeyTB.Text);

            if (UseInternal.Checked == true)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
                byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireChangeAesKey(aes_key_nr, aid, aid_key_nr_auth, new_key, aid_key_no, old_key, out card_status, out exec_time);

            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);
                UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
                byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireChangeAesKey_PK(aes_key_ext, aid, aid_key_nr_auth, new_key, aid_key_no, old_key, out card_status, out exec_time);

            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireChangeAesKey = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }

            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireChangeAesKey = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bGetKeySettings_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte setting;
            byte set_temp = 0;
            byte max_key_no;
            UInt16 card_status;
            UInt16 exec_time;

            if (UseInternal.Checked == true)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireGetKeySettings(aes_key_nr, aid, out setting, out max_key_no, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireGetKeySettings_PK(aes_key_ext, aid, out setting, out max_key_no, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireGetKeySettings = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nExecution time : " + exec_time.ToString() + " ms";
                    StatusRTB.Text += "\n\nMax key number : " + max_key_no.ToString();

                    setting &= 0x0F;
                    switch (setting)
                    {
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_CHANGE:
                            set_temp = 0;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_NOT_CHANGE:
                            set_temp = 1;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_CHANGE:
                            set_temp = 2;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_NOT_CHANGE:
                            set_temp = 3;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_CHANGE:
                            set_temp = 4;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE:
                            set_temp = 5;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_CHANGE:
                            set_temp = 6;
                            break;
                        case (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE:
                            set_temp = 7;
                            break;
                    }

                    int ChBxSett = int.Parse((set_temp & 0x04).ToString());
                    int chBxCreate = int.Parse((set_temp & 0x02).ToString());
                    int ChBxMaster = int.Parse((set_temp & 0x01).ToString());


                    if (ChBxSett > 0)
                        ChBxSett_1.Checked = true;
                    else
                        ChBxSett_1.Checked = false;
                    if (chBxCreate > 0)
                        ChBxCreate_1.Checked = true;
                    else
                        ChBxCreate_1.Checked = false;
                    if (ChBxMaster > 0)
                        ChBxMaster_1.Checked = true;
                    else
                        ChBxMaster_1.Checked = false;
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }

            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireGetKeySettings = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bGetCardUID_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            byte[] card_uid = new byte[7];
            byte card_uid_len;
            UInt16 card_status;
            UInt16 exec_time;

            if (UseInternal.Checked == true)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                status = (UInt32)uFCoder.uFR_int_GetDesfireUid(aes_key_nr, aid, aid_key_nr_auth, card_uid, out card_uid_len, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_GetDesfireUid_PK(aes_key_ext, aid, aid_key_nr_auth, card_uid, out card_uid_len, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_GetDesfireUid = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nExecution time : " + exec_time.ToString() + " ms";
                    StatusRTB.Text += "\n\nCard UID : " + BitConverter.ToString(card_uid).Replace("-", " ");
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }

            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_GetDesfireUid = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }

        }

        private void bChangeKeySettings_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte setting = 0;
            byte set_temp = 0;
            UInt16 card_status;
            UInt16 exec_time;

            if (ChBxSett_1.Checked)
                set_temp |= 0x04;
            if (ChBxCreate_1.Checked)
                set_temp |= 0x02;
            if (ChBxMaster_1.Checked)
                set_temp |= 0x01;

            switch (set_temp)
            {
                case 0:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_CHANGE;
                    break;
                case 1:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 2:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_CHANGE;
                    break;
                case 3:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 4:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_CHANGE;
                    break;
                case 5:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 6:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_CHANGE;
                    break;
                case 7:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE;
                    break;
            }

            if (UseInternal.Checked == true)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireChangeKeySettings(aes_key_nr, aid, setting, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireChangeKeySettings_PK(aes_key_ext, aid, setting, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireChangeKeySettings = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nKey settings are changed\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }

            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireChangeKeySettings = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bMakeApplication_Click(object sender, EventArgs e)
        {
            byte setting = 0;
            byte set_temp = 0;
            UInt16 card_status;
            UInt16 exec_time;

            if (ChBxSett_1.Checked)
                set_temp |= 0x04;
            if (ChBxCreate_1.Checked)
                set_temp |= 0x02;
            if (ChBxMaster_1.Checked)
                set_temp |= 0x01;

            switch (set_temp)
            {
                case 0:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_CHANGE;
                    break;
                case 1:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 2:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_CHANGE;
                    break;
                case 3:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 4:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_CHANGE;
                    break;
                case 5:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITHOUT_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE;
                    break;
                case 6:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_CHANGE;
                    break;
                case 7:
                    setting = (byte)DESFIRE_KEY_SETTINGS.DESFIRE_KEY_SET_CREATE_WITH_AUTH_SET_NOT_CHANGE_KEY_NOT_CHANGE;
                    break;
            }

            UInt32 aid = Convert.ToUInt32(CreateAppAID.Text, 16);
            byte max_key_no = Byte.Parse(CreateAppMaxKeyNo.Text);

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked == true)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireCreateAesApplication(aes_key_nr, aid, setting, max_key_no, out card_status, out exec_time);
                }
                else
                {
                    byte[] aes_key_ext = new byte[16];
                    aes_key_ext = StringToByteArray(AESkeyTB.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireCreateAesApplication_PK(aes_key_ext, aid, setting, max_key_no, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireCreateAesApplication_no_auth(aid, setting, max_key_no, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireCreateAesApplication = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nApplication created\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }

            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireCreateAesApplication = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }

        }

        private void bMakeFile_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte file_id = Byte.Parse(FileIDTB.Text);
            UInt32 file_size = UInt32.Parse(SizeOfFileTB.Text);
            byte read_key_no = Byte.Parse(ReadKeyNoCB.Text);
            byte write_key_no = Byte.Parse(WriteKeyNoCB.Text);
            byte read_write_key_no = Byte.Parse(RWKeyNoCB.Text);
            byte change_key_no = Byte.Parse(ChangeKeyNoCB.Text);
            byte communication_settings = 0;
            Int32 lower_limit = Int32.Parse(LowerLimitTB.Text);
            Int32 upper_limit = Int32.Parse(UpperLimitTB.Text);
            Int32 value = Int32.Parse(ValueFileTB.Text);
            byte limited_credit_enabled = 0;

            if (LimitedCreditCB.Checked)
            {
                limited_credit_enabled = 1;
            }
            else
            {
                limited_credit_enabled = 0;
            }

            if (FreeGetValueCB.Checked)
            {
                limited_credit_enabled |= 0x02;
            }

            if (FileCommMode.SelectedIndex == 0)
            {
                communication_settings = 0;
            }
            else if (FileCommMode.SelectedIndex == 1)
            {
                communication_settings = 1;
            }
            else if (FileCommMode.SelectedIndex == 2)
            {
                communication_settings = 3;
            }
            else
            {
                communication_settings = 0;
            }

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                    if (RGStandard.Checked)
                    {
                        status = (UInt32)uFCoder.uFR_int_DesfireCreateStdDataFile(aes_key_nr, aid, file_id, file_size, read_key_no,
                                 write_key_no, read_write_key_no, change_key_no, communication_settings, out card_status, out exec_time);

                    }
                    else
                    {

                        status = (UInt32)uFCoder.uFR_int_DesfireCreateValueFile(aes_key_nr, aid, file_id, lower_limit, upper_limit, value,
                                 limited_credit_enabled, read_key_no, write_key_no, read_write_key_no, change_key_no, communication_settings,
                                 out card_status, out exec_time);
                    }
                }
                else
                {
                    byte[] aes_key_ext = new byte[16];
                    aes_key_ext = StringToByteArray(AESkeyTB.Text);

                    if (RGStandard.Checked)
                    {
                        status = (UInt32)uFCoder.uFR_int_DesfireCreateStdDataFile_PK(aes_key_ext, aid, file_id, file_size, read_key_no,
                                 write_key_no, read_write_key_no, change_key_no, communication_settings, out card_status, out exec_time);

                    }
                    else
                    {

                        status = (UInt32)uFCoder.uFR_int_DesfireCreateValueFile_PK(aes_key_ext, aid, file_id, lower_limit, upper_limit, value,
                                 limited_credit_enabled, read_key_no, write_key_no, read_write_key_no, change_key_no, communication_settings,
                                 out card_status, out exec_time);
                    }
                }
            }
            else
            {
developer29's avatar
developer29 committed
                if (RGStandard.Checked)
                {
                    status = (UInt32)uFCoder.uFR_int_DesfireCreateStdDataFile_no_auth(aid, file_id, file_size, read_key_no,
                             write_key_no, read_write_key_no, change_key_no, communication_settings, out card_status, out exec_time);

                }
                else
                {

                    status = (UInt32)uFCoder.uFR_int_DesfireCreateValueFile_no_auth(aid, file_id, lower_limit, upper_limit, value,
                             limited_credit_enabled, read_key_no, write_key_no, read_write_key_no, change_key_no, communication_settings,
                             out card_status, out exec_time);
                }
developer29's avatar
developer29 committed
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireCreateStdDataFile = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nFile created\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireCreateStdDataFile = " + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void RGStandard_CheckedChanged(object sender, EventArgs e)
        {
            LowerLimitTB.Enabled = false;
            UpperLimitTB.Enabled = false;
            ValueFileTB.Enabled = false;
            LimitedCreditCB.Enabled = false;
            FreeGetValueCB.Enabled = false;
            SizeOfFileTB.Enabled = true;
        }

        private void RGValue_CheckedChanged(object sender, EventArgs e)
        {
            LowerLimitTB.Enabled = true;
            UpperLimitTB.Enabled = true;
            ValueFileTB.Enabled = true;
            LimitedCreditCB.Enabled = true;
            FreeGetValueCB.Enabled = true;
            SizeOfFileTB.Enabled = false;
        }

        private void bDeleteFile_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte file_id = Byte.Parse(FileIDTB.Text);

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireDeleteFile(aes_key_nr, aid, file_id, out card_status, out exec_time);
                }
                else
                {
                    byte[] aes_key_ext = new byte[16];
                    aes_key_ext = StringToByteArray(AESkeyTB.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireDeleteFile_PK(aes_key_ext, aid, file_id, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireDeleteFile_no_auth(aid, file_id, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireDeleteFile = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nFile deleted\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireDeleteFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bFileWriting_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            byte file_id = Byte.Parse(FileIDForWork.Text);
            UInt16 file_length = 0;
developer29's avatar
developer29 committed
            byte communication_settings = 0;
            byte[] file_data = new byte[10000];

            if (StandardCommMode.SelectedIndex == 0)
            {
                communication_settings = 0;
            }
            else if (StandardCommMode.SelectedIndex == 1)
            {
                communication_settings = 1;
            }
            else if (StandardCommMode.SelectedIndex == 2)
            {
                communication_settings = 3;
            }
            else
            {
                communication_settings = 0;
            }

            OpenFileDialog openDialog = new OpenFileDialog();

            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                /* System.IO.StreamReader sr = new System.IO.StreamReader(openDialog.FileName);
                 String dataFromFile = sr.ReadToEnd();
                 file_data = sr.
                 file_length = (byte)dataFromFile.Length;

                 sr.Close();*/

                file_data = File.ReadAllBytes(openDialog.FileName);
                file_length = (UInt16)file_data.Length;
developer29's avatar
developer29 committed
            }


            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireWriteStdDataFile(aes_key_nr, aid, aid_key_nr_auth, file_id, 0, file_length,
                             communication_settings, file_data, out card_status, out exec_time);
                }
                else
                {
                    byte[] aes_key_ext = new byte[16];
                    aes_key_ext = StringToByteArray(AESkeyTB.Text);

                    status = (UInt32)uFCoder.uFR_int_DesfireWriteStdDataFile_PK(aes_key_ext, aid, aid_key_nr_auth, file_id, 0, file_length,
                             communication_settings, file_data, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireWriteStdDataFile_no_auth(aid, aid_key_nr_auth, file_id, 0, file_length,
                             communication_settings, file_data, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireWriteStdDataFile = " + uFCoder.status2str((uFR.DL_STATUS)status);
developer29's avatar
developer29 committed

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nFile written\nExecution time : " + exec_time.ToString() + " ms";
                    FileLength.Text = file_length.ToString();
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireWriteStdDataFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
developer29's avatar
developer29 committed
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bFormatCard_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;

            if (UseInternal.Checked)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireFormatCard(aes_key_nr, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireFormatCard_PK(aes_key_ext, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireFormatCard = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nCard formatted\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireFormatCard" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bGetFreeMemory_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;
            UInt32 free_mem_byte;

            status = (UInt32)uFCoder.uFR_int_DesfireFreeMem(out free_mem_byte, out card_status, out exec_time);

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireFreeMem = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nFree memory : " + free_mem_byte.ToString() + "\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireFreeMem" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bDEStoAES_Click(object sender, EventArgs e)
        {
            status = (UInt32)uFCoder.DES_to_AES_key_type();

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "DES_to_AES_key_type = " + uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text += "\nKey type is changed to AES\nNew AES key is 00000000000000000000000000000000";
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "DES_to_AES_key_type" + uFCoder.status2str((uFR.DL_STATUS)status);
            }
        }

        private void bSetRandomID_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;

            if (UseInternal.Checked)
            {
                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                status = (UInt32)uFCoder.uFR_int_DesfireSetConfiguration(aes_key_nr, 1, 0, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireSetConfiguration_PK(aes_key_ext, 1, 0, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireSetConfiguration = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nRandom ID is set : \nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireSetConfiguration" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bDeleteApplication_Click(object sender, EventArgs e)
        {
            UInt16 card_status;
            UInt16 exec_time;
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);

            if (UseInternal.Checked)
            {

                byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                status = (UInt32)uFCoder.uFR_int_DesfireDeleteApplication(aes_key_nr, aid, out card_status, out exec_time);
            }
            else
            {
                byte[] aes_key_ext = new byte[16];
                aes_key_ext = StringToByteArray(AESkeyTB.Text);

                status = (UInt32)uFCoder.uFR_int_DesfireDeleteApplication_PK(aes_key_ext, aid, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireDeleteApplication = " + uFCoder.status2str((uFR.DL_STATUS)status);

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK\nApplication deleted\nExecution time : " + exec_time.ToString() + " ms";
                }
                else
                {
                    StatusLabel.Text = Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status);
                    StatusRTB.Text += "\nCard status : " + Enum.GetName(typeof(DESFIRE_CARD_STATUS_CODES), card_status) + "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "uFR_int_DesfireDeleteApplication" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bSetSpeed_Click(object sender, EventArgs e)
        {
            byte tx_speed;
            byte rx_speed;

            tx_speed = (byte)txCB.SelectedIndex;
            rx_speed = (byte)rxCB.SelectedIndex;

            status = (UInt32)uFCoder.SetSpeedPermanently(tx_speed, rx_speed);

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "SetSpeedPermanently = " + uFCoder.status2str((uFR.DL_STATUS)status);
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "SetSpeedPermanently" + uFCoder.status2str((uFR.DL_STATUS)status);
            }
        }

        private void bGetSpeed_Click(object sender, EventArgs e)
        {
            byte tx_speed;
            byte rx_speed;

            status = (UInt32)uFCoder.GetSpeedParameters(out tx_speed, out rx_speed);

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "GetSpeedParameters = " + uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text += "\nTX : " + txCB.Items[tx_speed];
                StatusRTB.Text += "\nRX : " + rxCB.Items[rx_speed];
                rxCB.SelectedIndex = rx_speed;
                txCB.SelectedIndex = tx_speed;
developer29's avatar
developer29 committed
            }
            else
            {
                StatusLabel.Text = uFCoder.status2str((uFR.DL_STATUS)status);
                StatusRTB.Text = "GetSpeedParameters" + uFCoder.status2str((uFR.DL_STATUS)status);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                uFCoder.uFrOpen();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }

developer29's avatar
developer29 committed
            txCB.SelectedIndex = 0;
            rxCB.SelectedIndex = 0;