Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C# by sad2222 ( 6 years ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Nfc.CardEmulators;
namespace B2NFC
{
[Service(Exported = true, Enabled = true, Permission = "android.permission.BIND_NFC_SERVICE"),
IntentFilter(new[] { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" }, Categories = new[] { "android.intent.category.DEFAULT" }),
MetaData("android.nfc.cardemulation.host_apdu_service", Resource = "@xml/aid_list")]
class ServicoPay : HostApduService
{
private byte[] CPPSE = new byte[] { 0x00, 0xA4, 0x04, 0x00, 0x0E };
private byte[] CSELECT = new byte[] { 0x00, 0xA4, 0x04, 0x00, 0x07 };
private byte[] CGPO = new byte[] { 0x80, 0xA8, 0x00, 0x00, 0x02 };
private byte[] CGEN = new byte[] { 0x80, 0xAE, 0x80, 0x00, 0x2B };
private byte[] CRECORD1 = new byte[] { 0x00, 0xB2, 0x01, 0x14, 0x00 };
private byte[] CRECORD2 = new byte[] { 0x00, 0xB2, 0x01, 0x1C, 0x00 };
private byte[] CRECORD3 = new byte[] { 0x00, 0xB2, 0x02, 0x1C, 0x00 };
private byte[] CRECORD4 = new byte[] { 0x00, 0xB2, 0x01, 0x24, 0x00 };
private byte[] CRECORD5 = new byte[] { 0x00, 0xB2, 0x02, 0x24, 0x00 };
private static byte[] ISO7816_UNKNOWN_ERROR_RESPONSE ={
(byte)0x6F, (byte)0x00
};
private byte[] RRECORD1 = new byte[] { 0x00 };
private byte[] RRECORD2 = new byte[] { 0x00 };
private byte[] RRECORD3 = new byte[] { 0x00 };
private byte[] RRECORD4 = new byte[] { 0x00 };
private byte[] RRECORD5 = new byte[] { 0x00 };
public byte[] CopyByte(byte[] arr, int index, int count)
{
if (count < 1)
return new byte[0];
List<byte> Res = new List<byte>();
for (int i = index; i < index + count; i++)
{
Res.Add(arr[i]);
}
return Res.ToArray();
}
public byte[] GetPPSE(byte[] apdu)
{
byte[] DF = CopyByte(apdu, 5, 14);
List<byte> Return = new List<byte>() { 0x6F, 0x23, 0x84, 0x0E };
Return.AddRange(DF);
Return.AddRange(HexStringToByteArray("A511BF0C0E610C4F07"));
Return.AddRange(HexStringToByteArray(cGlobal.getAID()));
Return.AddRange(HexStringToByteArray("870101"));
Return.AddRange(new byte[] { 0x90, 0x00 });
return HexStringToByteArray($"6F23840E325041592E5359532E4444463031A511BF0C0E610C4F07{cGlobal.getAID()}8701019000");
}
public byte[] GETSelect(byte[] apdu)
{
byte[] AID = HexStringToByteArray(cGlobal.getAID());
List<byte> Return = new List<byte>() { 0x6F, 0x14, 0x84, 0x07 };
Return.AddRange(AID);
Return.AddRange(HexStringToByteArray("A5095007"));
Return.AddRange(HexStringToByteArray(cGlobal.getIDEN()));
Return.AddRange(new byte[] { 0x90, 0x00 });
return HexStringToByteArray($"6F3E8407{AID}A533500B56495341204352454449549F38189F66049F02069F03069F1A0295055F2A029A039C019F3704BF0C089F5A0500084008409000");
}
public byte[] GETGPO(byte[] apdu)
{
return HexStringToByteArray("7716820219809410080101001001010118010200200102009000");
}
public void ReadRecConf()
{
string track2 = cGlobal.TrackSelected.getTrackParser();
string Vencimento = cGlobal.TrackSelected.getFinal().Substring(0, 4);
string diaVencimento = "31";
string num = cGlobal.TrackSelected.getNum();
if (num.Length % 2 != 0)
num += "F";
switch (Vencimento.Substring(2, 2))
{
case "02":
diaVencimento = "29";
break;
case "04":
case "06":
case "09":
case "11":
diaVencimento = "30";
break;
case "01":
case "03":
case "05":
case "07":
case "08":
case "10":
case "12":
diaVencimento = "31";
break;
default:
break;
}
Vencimento += diaVencimento;
string trackLen = (track2.Length / 2).ToString("X");
string numLen = (num.Length / 2).ToString("X");
RRECORD1 = HexStringToByteArray($"70819E57{trackLen}{track2}5A{numLen}{num}5F2403{Vencimento}5F25030401015F280200565F3401018C219F02069F03069F1A0295055F2A029A039C019F37049F35019F45029F4C089F34038D0C910A8A0295059F37049F4C088E0C000000000000000042031F039F0702FF009F080200029F0D0500000000009F0E0500000000009F0F0500000000009F420209789F4A01829000");
RRECORD2 = HexStringToByteArray("7081A08F01FA9F3201039204D35241079081908BED68CB5E81B76EF8A9F6FF38168D728F11A160F78A48562F544D0BA42826C35989B4333FAC96FF7EFB34F34FD3F815A2BC0B2B8BAFD84FD99B26C96B7B35E2B080F1664A481B980FB6C41405796A0B9D5C9CDE2EF3668EBD0557D728DB3A0ABC4F87803CF28180795CE33A377E397AA8A71251D88E2B092E287BB780865C6AB9FA891F85B268D215A43C2A77C3C1D99000");
RRECORD3 = HexStringToByteArray("70039301FF9000");
RRECORD4 = HexStringToByteArray("70219F4701039F481A00000000000000066000000000000000000000000000000000559000");
RRECORD5 = HexStringToByteArray("70739F46707005E297C2B7D0E80AC54C3014820F2FF7811EB95495E0F1D4A6BC120E0A8A739AC78DABCDB59ADEEDB43AC7652D83074D0D2C7B3EA53E0B1CC514D29D58AA7B91389289EB1DF96D00200D2E2EF4628F0646C0A9047C9390851BF04B7D17D430E51EDC7B2072D85367A172377B486BB19000");
}
public override void OnDeactivated(DeactivationReason reason)
{
}
public override void OnCreate()
{
}
public bool isArray(byte[] ar1, byte[] ar2, int len = 5)
{
return ar1[0] == ar2[0] && ar1[1] == ar2[1] && ar1[02] == ar2[02] && ar1[3] == ar2[3] && ar1[4] == ar2[4];
}
public override byte[] ProcessCommandApdu(byte[] commandApdu, Bundle extras)
{
ReadRecConf();
byte[] returnAPDU = new byte[12];
if (isArray(commandApdu, CPPSE))
returnAPDU = GetPPSE(commandApdu);
else if (isArray(commandApdu, CSELECT))
returnAPDU = GETSelect(commandApdu);
else if (isArray(commandApdu, CGPO))
returnAPDU = GETGPO(commandApdu);
else if (isArray(commandApdu, CRECORD1))
returnAPDU = RRECORD1;
else if (isArray(commandApdu, CRECORD2))
returnAPDU = RRECORD2;
else if (isArray(commandApdu, CRECORD3))
returnAPDU = RRECORD3;
else if (isArray(commandApdu, CRECORD4))
returnAPDU = RRECORD4;
else if (isArray(commandApdu, CRECORD5))
returnAPDU = RRECORD5;
else if (isArray(commandApdu, CGEN))
returnAPDU = new byte[1];
else
returnAPDU = ISO7816_UNKNOWN_ERROR_RESPONSE;
cGlobal.addHistory($"APDU:{ByteArrayToHexString(commandApdu)} RESPONSE: {ByteArrayToHexString(returnAPDU)}");
return returnAPDU;
}
public string ByteArrayToHexString(byte[] bytes)
{
String s = "";
for (int i = 0; i < bytes.Length; i++)
{
s += bytes[i].ToString("X2");
}
return s;
}
private byte[] HexStringToByteArray(string s)
{
int len = s.Length;
if (len % 2 != 0)
{
s += "0";
return HexStringToByteArray(s);
}
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;
}
public byte[] ConcatArrays(byte[] first, params byte[][] rest)
{
int totalLength = first.Length;
foreach (byte[] array in rest)
{
totalLength += array.Length;
}
byte[] result = new byte[totalLength];
first.CopyTo(result, 0);
int offset = first.Length;
foreach (byte[] array in rest)
{
array.CopyTo(result, offset);
offset += array.Length;
}
return result;
}
}
}
Revise this Paste