通过OmniKey读取非接触式卡片,如何获取“UID”?

9
我正在尝试使用非接触式OmniKey 5321卡读取器从智能卡上读取信息。该卡有一些信息盖在上面,我可以假设以某种方式存在于卡数据中(下方有卡和读卡器的照片)。使用OmniKey读卡器附带的诊断应用程序,我得到以下信息:智能卡名称为iCLASS 16KS。UID为EE 74 0E 00 FB FF 12 E0。ATR为3B 8F 80 01 80 4F 0C A0 00 00 03 06 0A 00 1A 00 00 00 00 78。协议为ISO 15693(第2部分)。我的问题如下:UID号码是我可以依赖的唯一号码吗? 我不需要知道447号码,我需要知道从这张卡中获取的信息将唯一地标识它,以便稍后将其与卡的所有者联系起来。如何读取UID号码?在Windows中,我可以使用WINSCARD.DLL看到“ATR”数据,每个字节都可以看到,但似乎在那部分中没有UID。如果您需要更多信息,请参见上文所示的照片。

你找到获取卡片ID的方法了吗? 我也遇到了同样的问题,读卡器返回的是十六进制序列号,与写在卡片上的卡号无关。谢谢 :) - user840575
@Abfam 抱歉,我们没有找到一种方法来查找卡上特定的编号,并不得不使用实际从卡中检索到的数据。这意味着在注册过程中需要再刷一次卡以读取其上的数据,而不是像我们希望的那样只需输入一个数字。 - Lasse V. Karlsen
ATR由PCSC制作,是有线逻辑PICC/VICC的非接触式ATR的“标准”格式。这是从初始ATR字节确定的:3B 8F 80 01(TS,T0,TD1-2)。大多数数据包的含义是“固定的”;唯一的“数据”字段位于字节偏移12(PIX.SS)和13-4(PIX.NAME),对于获取UID或任何其他所需字段没有帮助。 - escape-llc
从您的ATR数据包中,字节12-14: PIX.SS 0A = ISO 15693,第2部分 PIX.NN 00 1A = PicoPass 16Ks - escape-llc
1
就像它值得一提的那样(我知道我真的很晚才到这个聚会上),在此处的示例代码帮助我开始了这个项目。 - Onorio Catenacci
7个回答

14

最近,我花费了太多时间搜索如何从OMNIKEY近距离读卡器获取ATR的完整示例...

现在,我的代码已经可以运行,我想分享给其他人受益。

我发现的最好的代码来自SpringCard。(谢谢!)

我找到的其他示例都是浪费时间和误导的,并且由于DllImport不正确而无法工作...

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

namespace Test
{
    public delegate void VoidDelegate();
    public delegate void CardPresented(string reader, byte[] cardData);

    public class ReaderList : IDisposable, IEnumerable<string>
    {
        public ReaderList()
        { }

        public void Dispose()
        {
            StopThread();
        }

        private Thread thread;
        private void StartThread()
        {
            if (thread != null)
                StopThread();

            thread = new Thread(Run);
            thread.IsBackground = true;
            bStopThread = false;
            thread.Start();
        }

        private void StopThread()
        {
            if (thread != null)
            {
                bStopThread = true;
                Thread.Sleep(50);
            }
            if (thread != null)
                thread.Abort();
            if (thread != null)
                thread.Join();
            thread = null;
        }

        private List<string> readerNames = new List<string>();
        private Dictionary<string, string> lastCardFound = new Dictionary<string, string>();
        public int ReaderCount
        { get { return readerNames.Count; } }

        public void Refresh()
        {
            if (thread == null)
                StartThread();
        }

        public event VoidDelegate ListChanged;
        public event CardPresented CardPresented;

        private bool bStopThread = true;
        private void Run()
        {
            IntPtr hContext = IntPtr.Zero;

            try
            {
                uint result = SCARD.EstablishContext(SCARD.SCOPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, ref hContext);
                if (result != SCARD.S_SUCCESS)
                {
                    thread = null;
                    return;
                }

                uint notification_state = SCARD.STATE_UNAWARE;

                while (!bStopThread)    // loop 1 - build list, then iterate
                {
                    SCARD.ReaderState[] states = new SCARD.ReaderState[ReaderCount + 1];
                    states[0] = new SCARD.ReaderState(@"\\?PNP?\NOTIFICATION");
                    states[0].dwCurrentState = notification_state;

                    int iState = 0;
                    if (readerNames != null)
                        foreach (string s in readerNames)
                        {
                            iState++;
                            states[iState] = new SCARD.ReaderState(s);
                            states[iState].dwCurrentState = SCARD.STATE_UNAWARE;
                        }

                    while (!bStopThread)    // loop 2 - iterate over list built above
                    {
                        result = SCARD.GetStatusChange(hContext, 250, states, (uint)states.Length);
                        if (result == SCARD.E_TIMEOUT)
                            continue;
                        if (result != SCARD.S_SUCCESS)
                            break;

                        bool bReaderListChanged = false;
                        for (int i = 0; i < states.Length; i++)
                            if ((states[i].dwEventState & SCARD.STATE_CHANGED) != 0)
                                if (i == 0)
                                {
                                    // reader added or removed
                                    notification_state = states[0].dwEventState;

                                    // we want to replace the member in one step, rather than modifying it...
                                    List<string> tmp = GetReaderList(hContext, SCARD.GROUP_ALL_READERS);
                                    if (tmp == null)
                                        readerNames.Clear();
                                    else
                                        readerNames = tmp;

                                    if (ListChanged != null)
                                        ListChanged();
                                    bReaderListChanged = true;
                                }
                                else
                                {
                                    // card added or removed
                                    states[i].dwCurrentState = states[i].dwEventState;

                                    if ((states[i].dwEventState & SCARD.STATE_PRESENT) != 0)
                                    {
                                        byte[] cardData = new byte[states[i].cbATR];
                                        for (int j=0; j<cardData.Length; j++)
                                            cardData[j] = states[i].rgbATR[j];
                                        string thisCard = SCARD.ToHex(cardData, "");
                                        string lastCard;
                                        lastCardFound.TryGetValue(states[i].szReader, out lastCard);
                                        if (thisCard != lastCard)
                                        {
                                            lastCardFound[states[i].szReader] = thisCard;
                                            if (CardPresented != null)
                                                CardPresented(states[i].szReader, cardData);
                                        }
                                    }
                                    else
                                        lastCardFound[states[i].szReader] = "";                                        
                                }

                        if (bReaderListChanged)
                            break;  // break out of loop 2, and re-build our 'states' list

                    } // end loop 2
                } // end loop 1
            }
            catch (Exception ex)
            {
                //TODO: error logging
            }
            finally
            {
                if (hContext != IntPtr.Zero)
                    SCARD.ReleaseContext(hContext);
                thread = null;
            }
        }

        private List<string> GetReaderList(IntPtr hContext, string sGroup)
        {
            uint nStringLength = 0;
            uint result = SCARD.ListReaders(hContext, sGroup, null, ref nStringLength);
            if (result != SCARD.S_SUCCESS)
                return null;

            string sReaders = new string(' ', (int)nStringLength);
            result = SCARD.ListReaders(hContext, sGroup, sReaders, ref nStringLength);
            if (result != SCARD.S_SUCCESS)
                return null;
            List<string> list = new List<string> (sReaders.Split('\0'));
            for (int i = 0; i < list.Count; )
                if (list[i].Trim().Length > 0)
                    i++;
                else
                    list.RemoveAt(i);
            return list;
        }

        public IEnumerator<string> GetEnumerator()
        { return readerNames.GetEnumerator(); }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        { return readerNames.GetEnumerator(); }

    }

    public class SCARD
    {
        [DllImport("WinScard.dll", EntryPoint = "SCardEstablishContext")]
        public static extern uint EstablishContext(
            uint dwScope,
            IntPtr nNotUsed1,
            IntPtr nNotUsed2,
            ref IntPtr phContext);

        [DllImport("WinScard.dll", EntryPoint = "SCardReleaseContext")]
        public static extern uint ReleaseContext(
            IntPtr hContext);

        [DllImport("winscard.dll", EntryPoint = "SCardGetStatusChangeW", CharSet = CharSet.Unicode)]
        public static extern uint GetStatusChange(
            IntPtr hContext,
            uint dwTimeout,
            [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]
                SCARD.ReaderState[] rgReaderState,
            uint cReaders);

        [DllImport("winscard.dll", EntryPoint = "SCardListReadersW", CharSet = CharSet.Unicode)]
        public static extern uint ListReaders(
            IntPtr hContext,
            string groups,
            string readers,
            ref uint size);

        #region Error codes
        public const uint S_SUCCESS = 0x00000000;
        public const uint F_INTERNAL_ERROR = 0x80100001;
        public const uint E_CANCELLED = 0x80100002;
        public const uint E_INVALID_HANDLE = 0x80100003;
        public const uint E_INVALID_PARAMETER = 0x80100004;
        public const uint E_INVALID_TARGET = 0x80100005;
        public const uint E_NO_MEMORY = 0x80100006;
        public const uint F_WAITED_TOO_LONG = 0x80100007;
        public const uint E_INSUFFICIENT_BUFFER = 0x80100008;
        public const uint E_UNKNOWN_READER = 0x80100009;
        public const uint E_TIMEOUT = 0x8010000A;
        public const uint E_SHARING_VIOLATION = 0x8010000B;
        public const uint E_NO_SMARTCARD = 0x8010000C;
        public const uint E_UNKNOWN_CARD = 0x8010000D;
        public const uint E_CANT_DISPOSE = 0x8010000E;
        public const uint E_PROTO_MISMATCH = 0x8010000F;
        public const uint E_NOT_READY = 0x80100010;
        public const uint E_INVALID_VALUE = 0x80100011;
        public const uint E_SYSTEM_CANCELLED = 0x80100012;
        public const uint F_COMM_ERROR = 0x80100013;
        public const uint F_UNKNOWN_ERROR = 0x80100014;
        public const uint E_INVALID_ATR = 0x80100015;
        public const uint E_NOT_TRANSACTED = 0x80100016;
        public const uint E_READER_UNAVAILABLE = 0x80100017;
        public const uint P_SHUTDOWN = 0x80100018;
        public const uint E_PCI_TOO_SMALL = 0x80100019;
        public const uint E_READER_UNSUPPORTED = 0x8010001A;
        public const uint E_DUPLICATE_READER = 0x8010001B;
        public const uint E_CARD_UNSUPPORTED = 0x8010001C;
        public const uint E_NO_SERVICE = 0x8010001D;
        public const uint E_SERVICE_STOPPED = 0x8010001E;
        public const uint E_UNEXPECTED = 0x8010001F;
        public const uint E_ICC_INSTALLATION = 0x80100020;
        public const uint E_ICC_CREATEORDER = 0x80100021;
        public const uint E_UNSUPPORTED_FEATURE = 0x80100022;
        public const uint E_DIR_NOT_FOUND = 0x80100023;
        public const uint E_FILE_NOT_FOUND = 0x80100024;
        public const uint E_NO_DIR = 0x80100025;
        public const uint E_NO_FILE = 0x80100026;
        public const uint E_NO_ACCESS = 0x80100027;
        public const uint E_WRITE_TOO_MANY = 0x80100028;
        public const uint E_BAD_SEEK = 0x80100029;
        public const uint E_INVALID_CHV = 0x8010002A;
        public const uint E_UNKNOWN_RES_MNG = 0x8010002B;
        public const uint E_NO_SUCH_CERTIFICATE = 0x8010002C;
        public const uint E_CERTIFICATE_UNAVAILABLE = 0x8010002D;
        public const uint E_NO_READERS_AVAILABLE = 0x8010002E;
        public const uint E_COMM_DATA_LOST = 0x8010002F;
        public const uint E_NO_KEY_CONTAINER = 0x80100030;
        public const uint W_UNSUPPORTED_CARD = 0x80100065;
        public const uint W_UNRESPONSIVE_CARD = 0x80100066;
        public const uint W_UNPOWERED_CARD = 0x80100067;
        public const uint W_RESET_CARD = 0x80100068;
        public const uint W_REMOVED_CARD = 0x80100069;
        public const uint W_SECURITY_VIOLATION = 0x8010006A;
        public const uint W_WRONG_CHV = 0x8010006B;
        public const uint W_CHV_BLOCKED = 0x8010006C;
        public const uint W_EOF = 0x8010006D;
        public const uint W_CANCELLED_BY_USER = 0x8010006E;
        public const uint W_CARD_NOT_AUTHENTICATED = 0x8010006F;
        #endregion

        public const uint SCOPE_USER = 0;
        public const uint SCOPE_TERMINAL = 1;
        public const uint SCOPE_SYSTEM = 2;

        public const string GROUP_ALL_READERS = "SCard$AllReaders\0\0";
        public const string GROUP_DEFAULT_READERS = "SCard$DefaultReaders\0\0";
        public const string GROUP_LOCAL_READERS = "SCard$LocalReaders\0\0";
        public const string GROUP_SYSTEM_READERS = "SCard$SystemReaders\0\0";

        public const uint STATE_UNAWARE = 0x00000000;
        public const uint STATE_IGNORE = 0x00000001;
        public const uint STATE_CHANGED = 0x00000002;
        public const uint STATE_UNKNOWN = 0x00000004;
        public const uint STATE_UNAVAILABLE = 0x00000008;
        public const uint STATE_EMPTY = 0x00000010;
        public const uint STATE_PRESENT = 0x00000020;
        public const uint STATE_ATRMATCH = 0x00000040;
        public const uint STATE_EXCLUSIVE = 0x00000080;
        public const uint STATE_INUSE = 0x00000100;
        public const uint STATE_MUTE = 0x00000200;
        public const uint STATE_UNPOWERED = 0x00000400;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct ReaderState
        {
            public ReaderState(string sName)
            {
                szReader = sName;
                pvUserData = IntPtr.Zero;
                dwCurrentState = 0;
                dwEventState = 0;
                cbATR = 0;
                rgbATR = null;
            }

            internal string szReader;
            internal IntPtr pvUserData;
            internal uint dwCurrentState;
            internal uint dwEventState;
            internal uint cbATR;    // count of bytes in rgbATR
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x24, ArraySubType = UnmanagedType.U1)]
            internal byte[] rgbATR;
        }

        public static string ToHex(byte[] ab, string sDelim)
        {
            if (ab == null) return "<NULL>";
            return ToHex(ab, 0, ab.Length, sDelim);
        }

        public static string ToHex(byte[] ab, int offset, int len, string sDelim)
        {
            if (ab == null) return "<NULL>";
            StringBuilder sb = new StringBuilder();
            len = Math.Min(offset + len, ab.Length);
            for (int i = offset; i < len; i++)
                sb.Append(String.Format("{0:x02}", ab[i]).ToUpper() + sDelim);
            return sb.ToString();
        }

    }
}

1
感谢提供代码,对于其他人来说,这是使用代码的起点: var x = new ReaderList(); x.CardPresented += x_CardPresented; x.Refresh(); - StormRider01
@StormRider01,代码似乎无法运行,您能否详细解释一下? - Dragos Durlut
@StormRider01,我能够检测到ATR,但无法检测到卡片ID(CSN)。这个代码示例能检测到卡片UID吗? - dzona

5

UID可以通过针对的PC/SC 2.01兼容函数调用进行读取。

对于iCLASS卡,其长度通常为8个字节。其他卡返回4到8个字节的UID(它就像一个电话号码,用于识别场中的多张卡并最终选择一张)。

可以通过标准的SCardTransmit()交换APDU,其中CLA=0xFF表示访问非接触式存储卡。

Marc

http://www.smartcard-api.com

注意:UID不反映印在卡上的卡号。卡号是存储在该卡的应用程序1的页面0的HID PAC应用程序中的Wiegand数据的一部分。


2
您可以依赖UID,但在您的情况下似乎被截断了:
UID:EE 74 0E 00 FB FF 12 E0

Uid通常为16个字节长。

您可以阅读此独特标识符(UID):所有符合ISO标准的智能卡都配备有UID号码(类似于车辆上的VIN号码)。为了实现互操作性,卡的UID是开放和可供所有符合标准的读卡器读取的。由于此唯一数字未受密钥保护,因此读取智能卡的UID与读取近距离卡、磁条卡或其他使用开放的、未受保护数字的技术相当。

http://www.xceedid.com/pdf/XC5937_Smart_whitepaper.pdf


0

0

即使UID号码是标准的一部分,并且标准规定它应该在全球范围内是唯一的,但您必须记住,通常有制造商生产不符合规定(但可用)的卡片。

结论:只要您没有在卡片上放置独特的内容,就无法确定从卡片到卡片所有者的链接是否满足要求。


0

ATR(Answer To Reset)仅说明卡片格式、协议和数据的校验和。请参见ATR Parsing online

现在您知道了是哪种卡片,然后必须应用适当的(RS232/RS485/ZPL II/APDU - 智能卡应用协议数据单元,定义在ISO/IEC 7816-4中)命令来获取UID(取决于它是级联1、2还是3,请参见ACG HF Multi ISO RFID Reader v1.0第50页,也称为Omnikey5553 RS232)- 对于14443,它最长可达14个字节,占据第一个16字节扇区(扇区0)中的前两个块,并嵌入校验和。(请参见ISO14443协议或NXP Semiconductors MF1ICS50规格书第8页)。

对于15693,RS232/RS485命令将在选择(Omnikey5553 RS232和USB)时返回完整的16字节UID,但在APDU上只返回最后8个字节(在Omnikey5022上为0xFF 0xCA 0x00 0x00 0x00),尽管这是标准,因为对于15693,卡片每次只响应8个字节。对于14443,您可以一次读取一个扇区(32个字节的4个8字节块,在读/写S50之前登录到扇区),但对于15693,您只能一次读/写8字节块,并且返回缓冲区中有其他数据。您必须编写代码,无论是在32还是8处“阻止”数据。这是使用Windows API的标准SCardTransmit智能卡协议。由于诊断应用程序已返回16个字节,因此那就是卡片的UID。此外,一些专有的15693使用14443反转密钥来混淆和防止修改,其他人则持有执行验证的微程序 - 在PC上或在卡片本身上执行。
在所有情况下,这与卡上印刷的内容无关 - 我们使用印刷有GUID的CODE128C条形码的EV1塑料卡进行交易,并引用相应UID的数据库。其他(例如Wiegand等)打印其他数据,如区域代码、密钥集等。
当您尝试写入15693时,会出现其他问题 - 确保在4个字符接口处结束数据,否则在尝试在块中覆盖现有字符的空“0x00”时,可能会出现先前数据的问题 - 因此,在更新/写入卡后,编写针带和一根细绳的代码以确保数据符合需要。如果整个块都是'null',那么没有问题,因为数据将写入为4个字符的块。不要不必要地清空卡用户区域,因为它们是基于NAND的,具有有限的写入次数。(在写入后重新读取卡以确保所写数据正确!)
UID应在卡发行集内唯一,但它还取决于所涉及的卡数量订单 - 这就是为什么序列号已扩展2次(级联编号)的原因,并且有时您应该适度信任来自苹果手机的UID,因为它违反了UID中的U - 它可以被设置为模拟另一个UID。

NDEF是卡片的另一个需要理解的方面 - 在NFC NDEF标准中有很好的解释,但要小心14443的OTP和LOCK区域,因为它们一旦设置就是单向的。

您可以随时使用带有NFC和TagData应用程序的Android手机进行验证。


0

在花费了太多时间阅读智能卡相关的内容后,我使用.NET的PC/SC封装类成功地检索到了HID Seos iCLASS卡的UID。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接