Exemplo n.º 1
0
        public static bool AppendHistoricalRecord(HistoricalRecord record)
        {
            try
            {
                // validate


                // write data to smart card
                bool actionResult = SmartCardReaderUtil.Instance.AppendHistoricalRecord(_cardData_Original, record);

                // get new data from smart card
                //if (actionResult)
                //{
                //    actionResult = ReadData_FromSmartCard();
                //}

                // return value
                return(actionResult);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("WriteHistoricalRecord exception: " + ex.ToString());
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool AppendHistoricalRecord(SmartCardData_Original cardData, HistoricalRecord record)
        {
            try
            {
                // get list blocks will be read/write
                List <MifareCard_Block> lstBlocks = GetAll_Blocks_MifareClassic(EnumSmartCardTypes.MifareClassic4K);
                if (lstBlocks == null || lstBlocks.Count() == 0)
                {
                    throw new Exception("Can not get card blocks");
                }

                // calculate storage size of smartcard
                int totalBytes = lstBlocks.Count(block => block.BlockType == EnumBlockTypes.Data) * 16;

                // create SmartCardData object
                if (cardData == null)
                {
                    throw new Exception("SuperviseeBiodata in the card can not be null");
                }
                else
                {
                    if (cardData.HistoricalRecords == null || cardData.HistoricalRecords.Count == 0)
                    {
                        List <HistoricalRecord> historicalRecords = new List <HistoricalRecord>();
                        historicalRecords.Add(record);

                        cardData.HistoricalRecords = historicalRecords;
                    }
                    else
                    {
                        cardData.HistoricalRecords.Insert(0, record);
                    }
                }

                // convert object to json
                var jsonData = JsonConvert.SerializeObject(cardData);

                // encrypting
                var encryptedString = CommonUtil.EncryptString(jsonData, _passphrase);

                // check length of encryptContent, remove last HistoricalRecord
                while (encryptedString.Length > totalBytes)
                {
                    // remove last record
                    if (cardData.HistoricalRecords != null || cardData.HistoricalRecords.Count() > 0)
                    {
                        cardData.HistoricalRecords.Remove(cardData.HistoricalRecords.Last());
                    }

                    // convert new data to json
                    jsonData = JsonConvert.SerializeObject(cardData);

                    // encrypting
                    encryptedString = CommonUtil.EncryptString(jsonData, _passphrase);
                }

                // data to write
                byte[] byteData = Encoding.ASCII.GetBytes(jsonData);

                bool result = WriteData_MifareClassic(byteData);

                // return value
                return(result);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("WriteData_MifareClassic exception: " + ex.ToString());
                return(false);
            }
        }