如何通过NFC读取信用卡的响应

时间:2016-08-17 13:29:00

标签: android xamarin nfc contactless-smartcard emv

我需要使用NFC从信用卡中获取卡号,然后将其转换为正确的字符串。

这是我到目前为止所做的:

private static readonly string MASTERCARD_AID = "A0000000041010";
// ISO-DEP command HEADER for selecting an AID.
// Format: [Class | Instruction | Parameter 1 | Parameter 2]
private static readonly string SELECT_APDU_HEADER = "00A40400";
// "OK" status word sent in response to SELECT AID command (0x9000)
private static readonly byte[] SELECT_OK_SW = { (byte)0x90, (byte)0x00 };

// Weak reference to prevent retain loop. mAccountCallback is responsible for exiting
// foreground mode before it becomes invalid (e.g. during onPause() or onStop()).
private WeakReference<AccountCallback> mAccountCallback;

public interface AccountCallback
{
    void OnAccountRecieved(string account);
}

public LoyaltyCardReader(WeakReference<AccountCallback> accountCallback)
{
    mAccountCallback = accountCallback;
}
/**
 * Callback when a new tag is discovered by the system.
 *
 * <p>Communication with the card should take place here.
 *
 * @param tag Discovered tag
 */
public void OnTagDiscovered(Tag tag)
{
    IsoDep isoDep = IsoDep.Get(tag);
    if (isoDep != 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.
            byte[] command = BuildSelectApdu(MASTERCARD_AID);
            // Send command to remote device
            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; //should be 89
            byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] };
            byte[] payload = new byte[resultLength - 2];
            Array.Copy(result, payload, resultLength - 2);
            bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length;

            for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++)
            {
                arrayEquals = (SELECT_OK_SW[i] == statusWord[i]);
                if (!arrayEquals)
                    break;
            }
            if (arrayEquals)
            {
                //takes out cardname
                //int lengthWanted = 58;
                //byte[] newRes = new byte[resultLength - lengthWanted];
                //byte[] cardname = new byte[newRes.Length - 15]; 
                //Array.Copy(payload, newRes, resultLength - lengthWanted);
                //Array.Copy(payload, 16, cardname, 0, cardname.Length);

                // The remote NFC device will immediately respond with its stored account number
                string accountNumber = Encoding.UTF8.GetString(payload);
                //string accountNumber = ByteArrayToHexString(payload);

                // Inform CardReaderFragment of received account number
                AccountCallback accountCallback;
                if (mAccountCallback.TryGetTarget(out accountCallback))
                {
                    accountCallback.OnAccountRecieved(accountNumber);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Hmm: " + e);
            throw e;
            //Toast.MakeText(ctx, "hmmm: " + e, ToastLength.Short).Show();
        }
    }
}

/**
 * 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 + (aid.Length / 2).ToString("X2") + 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)
{
    var hex = new StringBuilder(bytes.Length * 2);
    foreach (byte b in bytes)
        hex.AppendFormat("{0:x2}", b);
    return hex.ToString();
}

/**
 * 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
 */
private static byte[] HexStringToByteArray(string s)
{
    int len = s.Length;
    if (len % 2 == 1)
    {
        throw new ArgumentException("Hex string must have even number of characters");
    }
    byte[] data = new byte[len / 2]; //Allocate 1 byte per 2 hex characters
    for (int i = 0; i < len; i += 2)
    {
        ushort val, val2;
        // Convert each chatacter into an unsigned integer (base-16)
        try
        {
            val = (ushort)Convert.ToInt32(s[i].ToString() + "0", 16);
            val2 = (ushort)Convert.ToInt32("0" + s[i + 1].ToString(), 16);
        }
        catch (Exception)
        {
            continue;
        }

        data[i / 2] = (byte)(val + val2);
    }
    return data;
}

我可以提取卡片类型,但结果的其余部分是乱码,如盒装问号等。我试图阅读有关该主题的所有内容,但我根本就没有得到它! :(

我在Visual Studio 2015 Enterprise中使用Xamarin。

我不记得我从哪里得到它,但大部分代码都来自一个很好的例子,作者使用另一部手机作为模拟器。

1 个答案:

答案 0 :(得分:2)

如果您的卡实际上是万事达卡(或实际上几乎是任何EMV支付卡),则卡不会返回其卡号(实际上是:主帐号,PAN)以响应应用程序选择(SELECT)命令。相反,您需要在卡中查询其数据文件并从这些文件中提取数字。

因此,您首先要通过AID选择MasterCard应用程序:

result = isoDep.Transceive(HexStringToByteArray("00A404007A000000004101000"));

接下来,您通常会发出GET PROCESSING OPTIONS命令(请参阅Unable to identify AFL on a smart card)以发现数据记录的位置。但是,您也可以跳过此步骤并尝试通过强力方法读取记录。

用蛮力方法阅读记录可能如下所示:

for (int sfi = 1; sfi < 10; ++sfi ) {
    for (int record = 1; record < 10; ++record) {
        byte[] cmd = HexStringToByteArray("00B2000400");
        cmd[2] = (byte)(record & 0x0FF)
        cmd[3] |= (byte)((sfi << 3) & 0x0F8);
        result = isoDep.Transceive(cmd);
        if ((result != null) && (result.Length >=2)) {
            if ((result[result.Length - 2] == (byte)0x90) && (result[result.Length - 1] == (byte)0x00)) {
                // file exists and contains data
                byte[] data = Arrays.CopyOf(result, result.Length - 2);
                // TODO: parse data
            }
        }
    }
}

然后,您需要搜索为每条记录返回的数据,以便找到包含PAN的数据对象。有关如何解码TLV编码数据对象,请参阅this answer。您可以找到在线TLV解析器here。 PAN通常在标签为0x5A的数据对象中编码(参见here)。

请注意,您可以通过NFC 读取的PAN可能与卡上打印的PAN不同。