Skip to content
Form1.cs 55.7 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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
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
            {
                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);
            }

            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);
            byte file_length = 0;
            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 = (byte)file_data.Length;
            }


            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_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 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_DesfireDeleteFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\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;

            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(GroupBox))
                {
                    foreach (Control a in c.Controls)
                    {
                        if (a.GetType() == typeof(ComboBox))
                        {
                            ComboBox cb = (ComboBox)a;
                            cb.DropDownStyle = ComboBoxStyle.DropDownList;
                        }