gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:10 24 4
gpt4 key购买 nike

我需要使用 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。

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

最佳答案

如果您的卡实际上是万事达卡(或几乎任何 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 的数据对象。参见 this answer关于如何解码 TLV 编码数据对象。您可以找到在线 TLV 解析器 here . PAN 通常编码在带有标签 0x5A 的数据对象中(参见 here )。

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

关于android - 如何通过 NFC 读取信用卡的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38998065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com