当前位置: 首页 > 面试题库 >

如何在Android中使用智能卡读取器以编程方式读取智能卡/微处理器卡

竺捷
2023-03-14
问题内容

因此,最近我一直在使用具有一些信息的智能卡,而我想要在此处实现的目标是使用智能卡读取器通过任何Android智能手机从这些智能卡中获取此数据。我一直在使用HID
OMNIKEY 3021
USB

智能卡读取器来读取这些卡(而且我知道此读取器可通过Windows应用程序与这些卡配合使用,因为我已经对此进行了亲自测试)

现在,Android提供了USB主机,只要Android智能手机支持它,便可以读取任何USB主机。

我正在尝试使用USB主机提供的这些类来访问此卡中的数据。

我的代码来检测任何USB主机:

private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);

IntentFilter attachedFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(mUsbAttachedReceiver, attachedFilter);

private final BroadcastReceiver mUsbAttachedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Utils.writeStringToTextFile("\n1 .Get an action : " + action, FileName);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            synchronized (this) {
                device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    showToast("Plugged In");
                    mUsbManager.requestPermission(device, mPermissionIntent);
                }
            }
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                showToast("Plugged Out");
                // call your method that cleans up and closes communication with the device
            }
        }
    }
};

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (device != null) {
                        //call method to set up device communication
                        Utils.writeStringToTextFile("2 .Get an action : " + action + "\nDevice is : " + device, FileName);
                        showToast("Permission Granted for device");

                        Handler h = new Handler();
                        h.postDelayed(run, 1000);

                    }
                } else {
                    showToast("Permission denied for device" + device);
                }
            }
        }
    }
};

一切正常,正如我得到的那样UsbDevice device,它给出了设备的信息,例如:

Device is : UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1899,mProductId=12322,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=OMNIKEY AG,mProductName=Smart Card Reader USB,mVersion=2.0,mSerialNumber=null,mConfigurations=[
UsbConfiguration[mId=1,mName=CCID,mAttributes=160,mMaxPower=50,mInterfaces=[
UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=11,mSubclass=0,mProtocol=0,mEndpoints=[
UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=8,mInterval=24]
UsbEndpoint[mAddress=132,mAttributes=2,mMaxPacketSize=64,mInterval=0]
UsbEndpoint[mAddress=5,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]]

现在,我试图用它UsbDevice device来从卡中获取数据和详细信息,但是我这样做没有成功,因此我也找不到任何有用的信息。

我知道我必须使用UsbInterfaceUsbEndpointUsbDeviceConnection让我从卡想要的东西,但我不能这样做。

另外,我无法找到任何样品或类似的东西。谁能指出我正确的方向?

抱歉,很长的帖子也感谢提前:)

编辑: 感谢Michael Roland先生,当阅读器设备通过USB接口讲CCID时,我能够阅读有关CCID的信息。

所以我用下面的代码:

        UsbDeviceConnection connection = mUsbManager.openDevice(device);
        UsbEndpoint epOut = null, epIn = null;

        for (int i = 0; i < device.getInterfaceCount(); i++) {
            UsbInterface usbInterface = device.getInterface(i);
            connection.claimInterface(usbInterface, true);

            for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
                UsbEndpoint ep = usbInterface.getEndpoint(j);
                showToast("Endpoint is : " + ep.toString() + " endpoint's type : " + ep.getType() + " endpoint's direction : " + ep.getDirection());
                Log.d(" ", "EP " + i + ": " + ep.getType());
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                        epOut = ep;

                    } else if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        epIn = ep;
                    }

                }
            }

            int dataTransferred = 0;
            byte[] PC_to_RDR_IccPowerOn = hexStringToByteArray("62" + "00000000" + "00" + "00" + "00" + "0000");

            if (epOut != null) {
                //Firstly send Power in on Bulk OUT endpoint
                dataTransferred = connection.bulkTransfer(epOut, PC_to_RDR_IccPowerOn, PC_to_RDR_IccPowerOn.length, TIMEOUT);
            }

            StringBuilder result = new StringBuilder();

            if (epIn != null) {
                final byte[] RDR_to_PC_DataBlock = new byte[epIn.getMaxPacketSize()];
                result = new StringBuilder();
                //Secondly send Power out on Bulk OUT endpoint
                dataTransferred = connection.bulkTransfer(epIn, RDR_to_PC_DataBlock, RDR_to_PC_DataBlock.length, TIMEOUT);
                for (byte bb : RDR_to_PC_DataBlock) {
                    result.append(String.format(" %02X ", bb));
                }

                if (dataTransferred > 0) {
                    Utils.writeStringToTextFile("\n2nd buffer received was : " + result.toString(), "Card_communication_data.txt");
                    String s1 = Arrays.toString(RDR_to_PC_DataBlock);
                    String s2 = new String(RDR_to_PC_DataBlock);
                    showToast("received - " + s1 + " - " + s2);
                } else {
                    showToast("received length at 2nd buffer transfer was " + dataTransferred);
                }
            }
        }

我收到的80 13 00 00 00 00 00 00 00 00 3B 9A 96 C0 10 31 FE 5D 00 64 05 7B 01 02 31 80 90 00 76 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00,但仍然我不知道如何处理数据字段做:ATR或如何形成Command APDUPC_to_RDR_XfrBlock命令..

我想我应该

发送包装在PC_to_RDR_XfrBlock命令中的命令APDU

现在; 谁能帮我这个?

编辑2: 我弄清楚了ATR的含义以及如何形成命令APDU。

但是现在我应该切换协议

默认协议为T = 0。要设置T = 1协议,必须由设备将PTS(也称为PPS)发送到卡。由于T = 0和T =
1协议都是该卡的必需项,因此用于协议交换的基本PTS对于卡片。如ISO / IEC
7816-3中所示,可以使用PTS切换到比ATR中卡建议的默认波特率更高的波特率(如果有的话)(TA(1)字节)。

我不确定这意味着什么以及如何实现这一目标!


问题答案:

典型的USB智能卡读取器实现USB
CCID设备类规范
。因此,您需要在应用程序中实现该协议,以便与读取器(和卡)进行通信。



 类似资料:
  • 问题内容: 我有一个IC接触式读卡器和SLE5528智能卡。想知道如何真正开始使用这些物品。 正在读取读取器,插入智能卡后看不到任何影响。 我还从http://www.openscdp.org/安装了opensmart的智能卡外壳 但是我不能用它来读任何读卡器。我想知道它是否有兼容性问题。 请我知道我可能不恰当地提出了这个问题,但是请那里的任何人帮助我。 任何相关的链接或有用的信息都可以帮助我入门

  • 我已经实现了一个Andoid应用程序-服务器端应用程序。服务器与智能卡读卡器通信。当用户在Android应用程序中触摸按钮时,将构建一个连接到服务器以验证用户。应用和服务器之间交换的消息具有以下格式: 如果消息的类型值,则表明智能卡读卡器中存在错误 如果消息的类型值,则表明智能卡中存在错误 我使用如下代码与智能卡读卡器进行通信: 智能卡IO API具有异常的类。我的问题是,我不知道何时发送类型为或

  • 我正试图从智能卡读取证书, 这是我从http://www.developer.com/java/other/article.php/3587361/Java-Applet-for-Signing-with-a-Smart-Card.htm我将在applet中执行这段代码,问题是每个用户都必须向我指出他们的本地pkcs11。。。dll,使用java samrtcard api是否可以避免加载此dll

  • 我使用ACR122U-A9读取智能卡,通过以下APDU,我可以获取UID和ATS: 使用NXP TagInfo应用程序(android),我可以看到一个包含我所需数据的十六进制表。使用什么APDU命令可以检索这些数据? 六边形表如下所示: [0000]中是我想要检索的数据。

  • 我正在尝试使用NFC读卡器库读取Mifare超轻型卡的内容。 我对NFC完全陌生,我正在使用这个github存储库开始。 此存储库中的代码允许检测检测到哪种类型的卡(Mifare,Mifare超轻量级......)并读取卡的UID。我添加了以下代码以读取Mifare超轻型卡的内容: 我有一张卡片,上面写着“Hello world”,当我读到它时,上面的代码会打印以下字节: 所以我从我的卡片上读了一

  • 我有一个Java应用程序,可以从智能卡读取证书,并使用它们登录用户。该应用还可以使用其他登录方法(用户名和密码)跟踪失败的登录尝试。 我想知道是否有可能在没有引脚的情况下从智能卡中读取用户信息?我想将失败的pin条目标记为失败的登录尝试,但是因为我需要pin从证书中读取别名,所以我无法知道哪个用户正在尝试登录。有没有读取这些信息没有引脚在Java? 我根据这个线程的建议尝试加载KeyStore对象