Android HCE的基本使用

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

最近NFC支付挺火的,趁国庆宅在家,学习下Android 卡模拟(Host-based Card Emulation)。HCE的特点是模拟智能IC卡(ISO 7816-4),可用于金融和行业应用,相应地,CardReader例子中使用IsoDep。

智能IC卡本身是一个微型计算机,常见为Java Card平台,特别是多功能集于一身的卡(如联名卡),Java Card比J2ME更加硬件受限。Java Card可以运行一到多个Java Applet,这些Applet也就是卡应用,例如一张能刷公交的银行卡可能就包含了2个Applet。每个Applet都有一个AID,受理终端(刷卡设备)通过AID来找到对应的卡应用(受理终端向卡发送SELECT命令),受理终端找到对应的卡应用后就可以进行数据交互,交互的数据一般是密文,不联机解密的话,用对称算法,联机解密的话,用非对称和对称算法都行。

HCE是软件模拟的智能IC卡,所以也会有AID。本文CardEmulation只注册一个AID。本文的代码改自Android 4.4 Sample,没改动CardEmulation,简化CardReader并支持Android 2.3系统,适应低版本的NFC手机做虚拟卡的卡读取了。使用努比亚Z7 MAX做CardEmulation,小米 2A作CardReader。本文的代码可以到http://pan.baidu.com/share/link?shareid=3865907609&uk=1765623201下载。个人觉得阅读CardReader核心代码更能了解2者交互的过程:

public final class CardReader {
    private static final String TAG = "LoyaltyCardReader";
    // AID for our loyalty card service.
    private static final String SAMPLE_LOYALTY_CARD_AID = "F222222222";
    // ISO-DEP command HEADER for selecting an AID.
    // Format: [Class | Instruction | Parameter 1 | Parameter 2]
    private static final String SELECT_APDU_HEADER = "00A40400";
    // "OK" status word sent in response to SELECT AID command (0x9000)
    private static final byte[] SELECT_OK_SW = {(byte) 0x90, (byte) 0x00};

	public static String[][] TECHLISTS;
	public static IntentFilter[] FILTERS;

	static {
		try {
			//the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED intent
			TECHLISTS = new String[][] { { IsoDep.class.getName() },
					{ NfcV.class.getName() }, { NfcF.class.getName() }, };

			FILTERS = new IntentFilter[] { new IntentFilter(
					NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") };
		} catch (Exception e) {
		}
	}

	public static String load(Parcelable parcelable, Resources res) {
		//从Parcelable筛选出各类NFC标准数据
		final Tag tag = (Tag) parcelable;

		final IsoDep isoDep = IsoDep.get(tag);
		Log.e("NFCTAG ID", Util.toHexString(tag.getId(), 0, tag.getId().length));//isodep.transceive("45".getBytes()).toString());
		if (isoDep == null) {
			return null;
		}

        try {
            // Connect to the remote NFC device
            isoDep.connect();
            // Build SELECT AID command for our loyalty card service.
            // This command tells the remote device which service we wish to communicate with.
            Log.e(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);
            byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
            // Send command to remote device
            Log.e(TAG, "Sending: " + ByteArrayToHexString(command));
            byte[] result = isoDep.transceive(command);
            // If AID is successfully selected, 0x9000 is returned as the status word (last 2
            // bytes of the result) by convention. Everything before the status word is
            // optional payload, which is used here to hold the account number.
            int resultLength = result.length;
            byte[] statusWord = {result[resultLength-2], result[resultLength-1]};
            byte[] payload = Arrays.copyOf(result, resultLength-2);
            if (Arrays.equals(SELECT_OK_SW, statusWord)) {
                // The remote NFC device will immediately respond with its stored account number
                String accountNumber = new String(payload, "UTF-8");
                Log.e(TAG, "Received: " + accountNumber);
                return accountNumber;
            }
        } catch (IOException e) {
            Log.e(TAG, "Error communicating with card: " + e.toString());
        }

        return null;
	}

    /**
     * Build APDU for SELECT AID command. This command indicates which service a reader is
     * interested in communicating with. See ISO 7816-4.
     *
     * @param aid Application ID (AID) to select
     * @return APDU for SELECT AID command
     */
    public static byte[] BuildSelectApdu(String aid) {
        // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]
        return HexStringToByteArray(SELECT_APDU_HEADER + String.format("%02X", aid.length() / 2) + aid);
    }

    /**
     * Utility class to convert a byte array to a hexadecimal string.
     *
     * @param bytes Bytes to convert
     * @return String, containing hexadecimal representation.
     */
    public static String ByteArrayToHexString(byte[] bytes) {
        final char[] hexArray = {‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for ( int j = 0; j < bytes.length; j++ ) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * Utility class to convert a hexadecimal string to a byte string.
     *
     * <p>Behavior with input strings containing non-hexadecimal characters is undefined.
     *
     * @param s String containing hexadecimal characters to convert
     * @return Byte array generated from input
     */
    public static byte[] HexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}

简单地说:
1。CardReader使用SAMPLE_LOYALTY_CARD_AID + SELECT_APDU_HEADER 生成 SELECT APDU;

2。CardReader发送SELECT APDU到CardEmulation后,CardEmulation返回SELECT_OK_SW + accountNumber。

其中SAMPLE_LOYALTY_CARD_AID ,SELECT_APDU_HEADER ,SELECT_OK_SW 都必须是CardReader与CardEmulation双方定义好的。

时间: 2024-08-01 18:40:57

Android HCE的基本使用的相关文章

Android HCE开发配置AID响应关系总结

开发HCE功能必须使用到HostApduService,在使用HostApduService时需要配置HostApduService的对应AID,同一个手机可能安装多个HCE APP,或者同一个APP包含多个HostApduService,这时候就需要搞清楚配置的AID和对应的HostApduService之间的响应向后问题. 首先介绍一下关于HostApduService的AID配置问题,AID有两种模式:other和payment 第一种 <host-apdu-service xmlns:a

Android API Guides---Host-based Card Emulation

Host-based Card Emulation 许多提供NFC功能的Andr??oid手机已经支持NFC卡模拟.在大多数情况下,该卡是由在该装置的单独芯片仿真,称为安全元件.无线运营商提供了很多的SIM卡还包含一个安全元件. Android 4.4系统的介绍卡仿真的一个额外的方法,不涉及安全的元素,称为基于主机的卡仿真.这允许任何Android应用程序来模拟卡,并直接交谈的NFC读取器.本文介绍了基于主机的卡模拟(HCE)如何适用于Android以及如何开发模拟使用这种技术的NFC卡的应用程

基于HCE移动支付研究报告

1. 概念 HCE(host-based card emulation),即基于主机的卡模拟.在一部配备NFC功能的手机实现卡模拟,目前有两种方式:一种是基于硬件的,称为虚拟卡模式(Virtual Card Mode);一种是基于软件的,被称为主机卡模式(Host Card Mode),即本文要讨论的方式. 在虚拟卡模式下,需要提供安全模块SE(Secure Elemen),SE提供对敏感信息的安全存储和对交易事务提供一个安全的执行环境.NFC芯片作为非接触通讯前端,将从外部读写器接收到的命令转

10.1-10.31推荐文章汇总

10.1-10.31推荐文章汇总 [移动开发] Android ViewGroup拦截触摸事件详解        Mr-Simp1e Android 实现形态各异的双向侧滑菜单 自定义控件来袭        鸿洋_ Android应用开发-小巫CSDN博客客户端之嵌入有米广告        IT_xiao小巫 [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上)        秦元培 Android应用开发-小巫CSDN博客客户端Jsoup篇        IT_xiao小巫

HCE对金融、交通、通讯、智能卡行业的巨大影响 电子虚拟智能卡 Android version4.4 NFC手机

HCE模式,可以使得软件模拟智能卡,产生电子虚拟智能卡. HCE(Host-based Card Emulation)是一个模拟智能卡的软件,在Android中以软件形式存在(例如:apk).HCE的特点是模拟智能IC卡(ISO 7816-4),可用于金融.交通.通讯等行业应用,HCE是软件模拟的智能IC卡,所以也会有AID,可以模拟现有实体卡片的功能. 对金融.交通.通讯.智能卡等行业产生巨大影响,就如同电子信息和互联网技术的发展对传统媒体(纸质报纸.实体唱片)等的影响一样. HCE是基于NF

支持HCE功能的NFC手机(Android)、 支持NFC功能的手机大全(安卓、微软、塞班、苹果)

支持HCE功能的Android手机需要满足如下条件: 1.支持NFC功能 2.操作系统是Android 3.Android版本4.4 如果满足条件1.2,但是通过刷机满足条件3也成立. 说明:对于NFC发烧友,如果囊中羞涩,不妨购买二手手机(刷机满足要求的二手手机更实惠)发烧.淘宝上价廉物美的很多! HCE(Host-based Card Emulation)是一个模拟智能卡的软件,在Android中就是一个apk.HCE的特点是模拟智能IC卡(ISO 7816-4),可用于金融和行业应用,HC

HCE:Host-based Card Emulation基于Android设备的卡片模拟器

HCE技术支持提供了一个软实现SE的通路,Service实现的方式很多,可以使用文件,使用网络,甚至连接真正的SE.支持HCE的测试手机:目前可以确定使用了NXP PN547作为CLF的NFC手机已经打通了HCE.市面上可见的目前有Sony Xperia Z2 和 Samsung Galaxy S5.一份参考资料:http://developer.android.com/guide/topics/connectivity/nfc/hce.html 里面写的非常细致:1. HCE工作在ISO 78

HCE安全问题

最近的项目中每每都会被问到:HCE安全吗? 我的答复是:相对安全. 听到我这样的回答,很多人可能会开始说了,某某银行都上HCE应用了,怎么不安全了? 其实,HCE应用的场景有两种:在线模式与离线模式. 在线模式: 相关密钥以及运算在后台完成,即便出现安全问题,也属于网络安全范畴.但,不会发生大规模密钥泄露问题.目前,银行上线的HCE应用都是属于在线模式. 离线模式: 相关密钥.敏感数据.金额等信息都会存储在手机内部.这样就麻烦了,因为Android手机很容易被root,所有的数据会被读取.复制.

移动支付--银联,支付宝,微信(android)

在这个移动互联网高速发展的时代,手机已经成为人们生活或者出行之中不可缺少的设备了,现在很多城市的商户都可以采用支付宝,微信支付了,人们出门只需要随身携带带手机,不用带大量现金就可以放心购物了.现在的很多移动互联网产品都应用到移动支付功能,特别像电商平台,每天下单,支付就是他们处理的最复杂的业务逻辑.作为一位Android开发人员也必须应该懂得怎样在自己所开发的应用中接入当今互联网非常流行的移动支付(银联,支付宝,微信)功能.所以此篇文章就来记录一下Android对于移动支付的一些开发步骤. 银联