Skip to content
Form1.cs 55.7 KiB
Newer Older
developer29's avatar
developer29 committed
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:
Loading full blame...