Esempio n. 1
0
        public static int LoadKey(IntPtr obj, int sector, int keyTypeInt, bool nonvolatileMemory, int keyIndex, IntPtr key)
        {
            string text =
                $"!!! LoadKey !!!\tobj:{obj}\tSector:{sector}\tKeyType:{keyTypeInt}\tNonvolatileMemory:{nonvolatileMemory}\tKeyIndex:{keyIndex}\tKey:{key}\t";

            WriteToLog(text);
            try
            {
                if (_keys != null && _card != null)
                {
                    byte[] keyR = UnMemory <byte> .ReadInMemArr(key, 6);

                    _keys[new Tuple <int, int>(sector, keyTypeInt)] = new Tuple <int, bool, byte[]>(keyIndex, nonvolatileMemory, keyR);

                    text =
                        $"!!! LoadKey over !!!\tSector:{sector}\tKeyType:{keyTypeInt}\tKeyIndex:{keyIndex}\tkey:{BitConverter.ToString(keyR ?? new byte[] {})}\t";
                    //WriteToLog(text);
                }
                return((int)ErrorCodes.E_SUCCESS);
            }
            catch (Exception e)
            {
                WriteToLog($"LoadKey ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_POS_KEYS_LOAD);
            }
        }
Esempio n. 2
0
        public static int WriteBlock(IntPtr obj, int block, IntPtr data)
        {
            string text =
                $"!!! WriteBlock !!!\tobj:{obj}\tBlock:{block}\tData:{data}\t";

            WriteToLog(text);
            try
            {
                if (_card != null)
                {
                    int blockInSector;
                    int controlSector;
                    BlockToSectorBlock(block, out controlSector, out blockInSector);

                    byte[] dat = UnMemory <byte> .ReadInMemArr(data, 16);

                    var keyA = GetKeyFromCollection(controlSector, 0);
                    var keyB = GetKeyFromCollection(controlSector, 1);

                    text =
                        $"!!! WriteBlock internal !!!\tcurrentSector: {controlSector}\tblockInSector: {blockInSector}\tkeyA: {BitConverter.ToString(keyA ?? new byte[] { })}\tkeyB: {BitConverter.ToString(keyB ?? new byte[] { })}\tData:{BitConverter.ToString(dat ?? new byte[] { })}\t";
                    //WriteToLog(text);

                    //TODO убрать
                    //if (_cardBadSectors?.Contains(new Tuple<int, int>(controlSector, blockInSector)) ?? false)
                    //{
                    //    WriteOrReplaceToFileSector(controlSector, blockInSector, dat);
                    //    text = "!!! WriteBlock to file!";
                    //    //WriteToLog(text);
                    //    return (int)ErrorCodes.E_SUCCESS;
                    //}

                    //WriteDataToLog(controlSector, blockInSector, data.ByteArrayToString(), true);

                    var result = SetData(dat, controlSector, blockInSector, keyA, keyB).Result;
                    if (result == (int)ErrorCodes.E_SUCCESS)
                    {
                        result = Transfer(obj, block);
                    }

                    return(result);
                }
                WriteToLog($"WriteBlock ERROR !!! reader:{_reader} card: {_card}\r\n");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
            catch (Exception e)
            {
                WriteToLog($"WriteBlock ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_GENERIC);
            }
        }
Esempio n. 3
0
        public static int Init([MarshalAs(UnmanagedType.LPWStr)] string /*IntPtr*/ initStr, IntPtr caps, ref IntPtr obj)
        {
            string text =
                $"!!! Init !!!\tobj:{obj}\tCaps:{caps}\tInitStr:{initStr/*Marshal.PtrToStringBSTR(initStr)*/ ?? "null"}\t";

            WriteToLog(text);
            try
            {
                UnMemory <int> .SaveInMemArr(new[] { 12, 1, 0 }, ref caps);

                var capsR = UnMemory <int> .ReadInMemArr(caps, 3);

                text =
                    $"!!! Init internal Caps !!!\tsize:{capsR[0]}\tVolatileKeySlotCount:{capsR[1]}\tNonvolatileKeySlotCount:{capsR[2]}\t";
                //WriteToLog(text);

                var res = InitializeReader().Result;

                // выделение  памяти под obj
                int pInt = 0;
                UnMemory <int> .SaveInMem(pInt, ref obj);

                _obj = obj;

                if (_keys == null)
                {
                    _keys = new Dictionary <Tuple <int, int>, Tuple <int, bool, byte[]> >();
                }
                else
                {
                    _keys.Clear();
                }

                return(res);
            }
            catch (Exception e)
            {
                WriteToLog($"Init ERROR !!! reader:{_reader} card: {_card}\r\n {e}");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
        }
Esempio n. 4
0
        public static int SelectCard(IntPtr obj, IntPtr serialNumber, int serialNumberSize)
        {
            string text =
                $"!!! SelectCard !!!\tobj:{obj}\tSerialNumber:{serialNumber}\tSerialNumberSize:{serialNumberSize}\t";

            WriteToLog(text);
            try
            {
                byte[] uid = UnMemory <byte> .ReadInMemArr(serialNumber, serialNumberSize);

                text =
                    $"!!! SelectCard over !!!\tobj:{obj}\tSerialNumber:{serialNumber}\tSerialNumberSize:{serialNumberSize}\tuid:{BitConverter.ToString(uid ?? new byte[] {})}\t";
                WriteToLog(text);
                return((int)ErrorCodes.E_SUCCESS);
            }
            catch (Exception e)
            {
                WriteToLog($"SelectCard ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_GENERIC);
            }
        }
Esempio n. 5
0
        public static int Anticollision(IntPtr obj, IntPtr serialNumberBuf, int bufSize, IntPtr serialNumberSize)
        {
            string text =
                $"!!! Anticollision !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tSerialNumberSize:{serialNumberSize}\t";

            WriteToLog(text);

            try
            {
                var uid = _card?.GetUid().Result;
                if (uid != null)
                {
                    byte[] truncUid = new byte[Math.Min(bufSize, uid.Length)];
                    Array.Copy(uid, truncUid, truncUid.Length);
                    UnMemory <int> .SaveInMem(truncUid.Length, ref serialNumberSize);

                    UnMemory <byte> .SaveInMemArr(uid, ref serialNumberBuf);

                    //Marshal.StructureToPtr(memory_object, SerialNumberBuf, true);

                    var uidWrited = UnMemory <byte> .ReadInMemArr(serialNumberBuf, truncUid.Length);

                    var serialNumberSizeWrited = UnMemory <int> .ReadInMem(serialNumberSize);

                    text =
                        $"!!! Anticollision over !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tuid:{BitConverter.ToString(uid)}\tuid_w:{BitConverter.ToString(uidWrited ?? new byte[] {})}\tSerialNumberSize:{serialNumberSize}\tSerialNumberSize_writed:{serialNumberSizeWrited}\t";
                    WriteToLog(text);
                    return((int)ErrorCodes.E_SUCCESS);
                }

                WriteToLog($"Anticollision ERROR !!! _card:{_card == null}\r\n uid:{uid == null}");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
            catch (Exception e)
            {
                WriteToLog($"Anticollision ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_GENERIC);
            }
        }