Skip to content
Form1.cs 56.2 KiB
Newer Older
developer29's avatar
developer29 committed
            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;
                        }
                    }
                }

            }

        }

        private void bReadValue_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            UInt16 card_status;
            UInt16 exec_time;
            byte file_id = Byte.Parse(FileIDForWork.Text);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            byte communication_settings = 0;
            Int32 value;

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

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                    status = (UInt32)uFCoder.uFR_int_DesfireReadValueFile(aes_key_nr, aid, aid_key_nr_auth, file_id, communication_settings, out value, 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_DesfireReadValueFile_PK(aes_key_ext, aid, aid_key_nr_auth, file_id, communication_settings, out value, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireReadValueFile_no_auth(aid, aid_key_nr_auth, file_id, communication_settings, out value, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireReadValueFile = " + 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";
                    ValueView.Text = value.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_DesfireReadValueFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bIncreaseValue_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            UInt16 card_status;
            UInt16 exec_time;
            byte file_id = Byte.Parse(FileIDForWork.Text);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            byte communication_settings = 0;
            Int32 value = Int32.Parse(ValueView.Text);

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

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                    status = (UInt32)uFCoder.uFR_int_DesfireIncreaseValueFile(aes_key_nr, aid, aid_key_nr_auth, file_id, communication_settings, value, 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_DesfireIncreaseValueFile_PK(aes_key_ext, aid, aid_key_nr_auth, file_id, communication_settings, value, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireIncreaseValueFile_no_auth(aid, aid_key_nr_auth, file_id, communication_settings, value, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireIncreaseValueFile =" + 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_DesfireIncreaseValueFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bDecreaseValue_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            UInt16 card_status;
            UInt16 exec_time;
            byte file_id = Byte.Parse(FileIDForWork.Text);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            byte communication_settings = 0;
            Int32 value = Int32.Parse(ValueView.Text);

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

            if (MasterKeyAuthRequired.Checked)
            {
                if (UseInternal.Checked)
                {
                    byte aes_key_nr = Byte.Parse(InternalKeyNumberAuth.Text);
                    status = (UInt32)uFCoder.uFR_int_DesfireDecreaseValueFile(aes_key_nr, aid, aid_key_nr_auth, file_id, communication_settings, value, 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_DesfireDecreaseValueFile_PK(aes_key_ext, aid, aid_key_nr_auth, file_id, communication_settings, value, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireDecreaseValueFile_no_auth(aid, aid_key_nr_auth, file_id, communication_settings, value, out card_status, out exec_time);
            }

            if (status == 0)
            {
                StatusLabel.Text = "Operation completed";
                StatusRTB.Text = "uFR_int_DesfireDecreaseValueFile =" + 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_DesfireDecreaseValueFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                 "\nExecution time : " + exec_time.ToString() + " ms";
            }
        }

        private void bFileReading_Click(object sender, EventArgs e)
        {
            UInt32 aid = Convert.ToUInt32(AIDCardAuth.Text, 16);
            UInt16 card_status;
            UInt16 exec_time;
            byte file_id = Byte.Parse(FileIDForWork.Text);
            byte aid_key_nr_auth = Byte.Parse(AIDKeyNrAuth.Text);
            UInt16 file_length = UInt16.Parse(FileLength.Text);
developer29's avatar
developer29 committed
            byte communication_settings = 0;
            byte[] 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;
            }

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

                    status = (UInt32)uFCoder.uFR_int_DesfireReadStdDataFile(aes_key_nr, aid, aid_key_nr_auth, file_id, 0, file_length,
                    communication_settings, 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_DesfireReadStdDataFile_PK(aes_key_ext, aid, aid_key_nr_auth, file_id, 0, file_length,
                    communication_settings, data, out card_status, out exec_time);
                }
            }
            else
            {
                status = (UInt32)uFCoder.uFR_int_DesfireReadStdDataFile_no_auth(aid, aid_key_nr_auth, file_id, 0, file_length,
                communication_settings, data, out card_status, out exec_time);
            }

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

                if (card_status == (UInt16)DESFIRE_CARD_STATUS_CODES.CARD_OPERATION_OK)
                {
                    byte[] reading = new byte[file_length];
                    Array.Copy(data, reading, file_length);

                    StatusRTB.Text += "\nCard status : CARD_OPERATION_OK";
                    StatusRTB.Text += "\n\nExecution time : " + exec_time.ToString() + " ms";

                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName != "")
                    {
                        File.WriteAllBytes(saveFileDialog1.FileName, reading);
                    }
                }
                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_DesfireReadStdDataFile" + uFCoder.status2str((uFR.DL_STATUS)status) +
                                     "\nExecution time : " + exec_time.ToString() + " ms";
                }
            }

            private void bAEStoDES_Click(object sender, EventArgs e)
            {

                var confirmResult = MessageBox.Show("Are you sure?",
                                     "Confirm AES to DES key type",
                                     MessageBoxButtons.YesNo);
    
                if (confirmResult == DialogResult.Yes)
developer29's avatar
developer29 committed
                {
                    status = (UInt32)uFCoder.AES_to_DES_key_type();

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

            private void timer1_Tick(object sender, EventArgs e)
            {
                status = (UInt32)uFCoder.ReaderOpen();
developer29's avatar
developer29 committed
                if (status == 0)
                {
                    StatusLabel.Text = "Communication port opened";
                    timer1.Stop();
                    StatusRTB.Text = uFCoder.GetDescription();
                }
                else
                {
                    StatusLabel.Text = "Port not opened. Status is : " + uFCoder.status2str((uFR.DL_STATUS)status) + "    Searching for reader ...";
                }
            }
        }
    }