Skip to content

Instantly share code, notes, and snippets.

@jkingsman
Last active April 24, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkingsman/7645ca166fe0ca752413295b645cbe99 to your computer and use it in GitHub Desktop.
Save jkingsman/7645ca166fe0ca752413295b645cbe99 to your computer and use it in GitHub Desktop.
/**
*
*/
package me.jackkingsman.demoapplet;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.APDU;
import javacard.framework.Util;
/**
* @author Robert
*
*/
public class HelloWorld extends Applet {
private final static byte[] hello=
{(byte)'H', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)':', (byte)')'} ;
public static void install(byte[] buffer, short offset, byte length)
{
// GP-compliant JavaCard applet registration
new HelloWorld().register();
}
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x40:
Util.arrayCopy(hello, (short)0, buf, ISO7816.OFFSET_CDATA, (short) hello.length);
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short) hello.length);
break;
default:
// good practice: If you don't know the INStruction, say so:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
package me.jackkingsman.myapplication
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
private var nfcAdapter: NfcAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
public override fun onResume() {
super.onResume()
nfcAdapter?.enableReaderMode(this, this,
NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
null)
}
public override fun onPause() {
super.onPause()
nfcAdapter?.disableReaderMode(this)
}
fun ByteArray.toHexString() = asUByteArray().joinToString("") { it.toString(16).padStart(2, '0') }
override fun onTagDiscovered(tag: Tag?) {
val textView: TextView = findViewById(R.id.theTextView) as TextView
val tag = IsoDep.get(tag)
tag.connect()
val select: ByteArray = byteArrayOf(
0x00, 0xA4.toByte(), 0x04, 0x00, 0x06, 0xA0.toByte(), 0x00, 0x42, 0x06, 0x94.toByte(), 0x21
)
var select_result: ByteArray = tag.transceive(select)
runOnUiThread {textView.append("\nSelect applet got back " + select_result.toHexString())}
val hallo_fetch: ByteArray = byteArrayOf(
0x01, 0x40.toByte(), 0x00, 0x00, 0x00
)
var hallo_result: ByteArray = tag.transceive(hallo_fetch)
runOnUiThread {textView.append("\nInteract got back " + hallo_result.size + " bytes:" + hallo_result.toHexString())}
tag.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment