[PYTHON] Android HCE Survey Memo (Part 2)

Purpose

Check the connection from the PC (PC / SC) to Android HSE.

environment

This time, we confirmed the operation with the following models.

Android side

--Build environment

PC side

--NFC reader

Android side

Please refer to the previous article. Android HCE Survey Memo (Part 1)

The AID is "F222222222".

HSE communication confirmation app

For a quick check, try using python.

Module addition

Add a module to operate PC / SC from python. SWING is required for pyscard, so install it in advance. SWIG

After installing SWIG, continue to add modules with the following command. pip install pyscard

pyscard documentation

The pyscard documentation can be found at the following URL:

https://pyscard.sourceforge.io/#documentation

HCE connection python scripting

Create a python script.

HCE_connect.py


from smartcard.CardType import AnyCardType
from smartcard.CardConnection import CardConnection
from smartcard.CardRequest import CardRequest
from smartcard.util import toHexString

#Card connection
cardtype = AnyCardType()
cardrequest = CardRequest( timeout=1, cardType=cardtype )
cardservice = cardrequest.waitforcard()
cardservice.connection.connect( CardConnection.T1_protocol )
print ("ATR:", toHexString( cardservice.connection.getATR() ))

#Select by specifying AID
SELECT = [0x00, 0xA4, 0x04, 0x00]
CARD_AID = [0xF2, 0x22, 0x22, 0x22, 0x22 ]
LEN = [len(CARD_AID)]
#Assembling APDU
apdu = SELECT + LEN + CARD_AID
print("send apdu:", toHexString(apdu))
#Send and receive
data, sw1, sw2 = cardservice.connection.transmit(apdu)
print("SW1SW2: %x %x" % (sw1, sw2))
print("received:", toHexString(data))

#Disconnect
cardservice.connection.disconnect()
print("...disconnected.")

python script execution

Launch the android-Card Emulation app on your Android. Place Android on the card reader and let it be recognized.

After confirming that HCE recognizes it, execute the script. python HCE_connect.py

Result output


ATR: 3B 88 80 01 00 00 00 00 80 81 71 00 79
send apdu: 00 A4 04 00 05 F2 22 22 22 22
SW1SW2: 90 0
received: 30 30 30 30 30 30 30 30
...disconnected.

I was able to read it normally.

next time

Make sure you can access it in C # as well. Android HCE Survey Memo (Part 3)

Recommended Posts

Android HCE Survey Memo (Part 2)
[For memo] Linux Part 2
List memo writing part 2
Python basic memo --Part 2
FastAPI Tutorial Memo Part 1
Python basic memo --Part 1
Linux standard textbook memo 1 part 2
Python Basic Grammar Memo (Part 1)
Linux standard textbook memo part 6
[For memo] Linux Part 1 vi editor
FastAPI Tutorial Memo Part 3 Query Parameters
FastAPI Tutorial Memo Part 2 Path Parameters
Mastodon Bot Creation Memo: Part 4 Summary