/* *Utilisation des Java Card (U)SIM pour la sécurisation des communications et des données dans les applications sur téléphones mobiles Cette midlet est chargée de proposer une interface à l'utilisateur qui lui permette de déclencher l'envoi d'une information confidentielle à transmettre (chiffrée) par SMS. La midlet communique avec l'applet appropriée, récupère l'information chiffrée et l'expédie au contact indiqué Code de la midlet (listing 6) serge.chaumette at labri.fr jonathan.ouoba at labri.fr */ import javax.microedition.midlet.midlet; import javax.microedition.midlet.midletstatechangeexception; import javax.microedition.apdu.apduconnection; import javax.microedition.io.connector; import javax.wireless.messaging.*; import java.io.ioexception; import javax.microedition.io.connectionnotfoundexception; import javax.microedition.lcdui.commandlistener; import javax.microedition.lcdui.display; import javax.microedition.lcdui.displayable; import javax.microedition.lcdui.form; import javax.microedition.lcdui.item; import javax.microedition.lcdui.itemcommandlistener; import javax.microedition.lcdui.stringitem; import javax.microedition.lcdui.textfield; import javax.microedition.lcdui.command; public class Midlet_Misc extends MIDlet implements CommandListener, ItemCommandListener { private Display display; private Form form; private String smsport; private String phonenumber; private TextField stringitem; private StringItem commandreceive; private StringItem commandsend; private StringItem commandexit; private Command CMD_RECEIVE = new Command("Receive", Command.ITEM, 3); private Command CMD_SEND = new Command("Send", Command.ITEM, 2); private Command CMD_EXIT = new Command("Exit", Command.EXIT, 1); private Thread tapdu; private Send_APDU sapdu; private Thread tsms; private Send_SMS ssms; private Thread tssms; private Receive_SMS rsms; private byte[] tempbuffer = null; private byte[] encryptcommand = {(byte)0xa0, (byte)0xc1, (byte)0x00, (byte)0x00, (byte) 0x00; -1-
private byte[] decryptcommand = {(byte)0xa0, (byte)0xc3, (byte)0x00, (byte)0x00, (byte) 0x08, (byte)0x61, (byte)0x62, (byte)0x63, (byte)0x64, (byte)0x31, (byte)0x32, (byte)0x33, (byte )0x34, (byte)0x00; public Midlet_Misc() { display = Display.getDisplay(this); smsport = "50005"; phonenumber = "+5550001"; // create the main form with its component form = new Form("Demo Midlet - MISC"); stringitem = new TextField("Output", "", 100,0); stringitem.setlayout(item.layout_newline_after); form.append(stringitem); commandexit = new StringItem("", "Exit", Item.BUTTON); commandexit.setdefaultcommand(cmd_exit); commandexit.setitemcommandlistener(this); commandsend = new StringItem("", "Send", Item.BUTTON); commandsend.setlayout(item.layout_newline_before); commandsend.setdefaultcommand(cmd_send); commandsend.setitemcommandlistener(this); commandreceive = new StringItem("", "Receive", Item.BUTTON); commandreceive.setdefaultcommand(cmd_receive); commandreceive.setitemcommandlistener(this); form.append(commandsend); form.append(commandreceive); form.append(commandexit); form.setcommandlistener(this); // display the main screen public void showscreen() { display.setcurrent(form); protected void destroyapp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub protected void pauseapp() { // TODO Auto-generated method stub protected void startapp() throws MIDletStateChangeException { initapp(); showscreen(); -2-
public void initapp() { //application initialization operations //SMS Server Communication rsms = new Receive_SMS(smsPort,this); tsms = new Thread(rSMS); tsms.start(); // set the message of the stringitem public void setmessage(string text) { stringitem.setstring(text); //emission of encrypted SMS public void sendsms(string message) { ssms = new Send_SMS(smsPort,message,"",phoneNumber,this); tssms = new Thread(sSMS); tssms.start(); // fill the byte array to send as apdu command private void fill_apdu(byte[] apdu, String message) { short i; short length = (short)(message.length()-2); apdu[0] = (byte) 0xA0; apdu[1] = (byte) 0xC3; apdu[2] = (byte) 0x00; apdu[3] = (byte) 0x00; apdu[4] = (byte) length; for (i = 0; i < length; i++) apdu[i + 5] = message.getbytes()[i]; apdu[length + 5] = (byte) 0x00; //managenement of received encrypted SMS public void receivedsms(string message) { fill_apdu(tempbuffer, message); sapdu = new Send_APDU(this, tempbuffer, (byte)0xc3); tapdu = new Thread(sAPDU); tapdu.start(); // button action management public void commandaction(command c, Item i) { // exit the application if (c == CMD_EXIT) { destroyapp(false); catch (Exception ex) { -3-
setmessage("impossible to exit..."); notifydestroyed(); else if (c == CMD_SEND) { sapdu = new Send_APDU(this, encryptcommand, (byte)0xc1); tapdu = new Thread(sAPDU); tapdu.start(); else if (c == CMD_RECEIVE) { sapdu = new Send_APDU(this, decryptcommand, (byte)0xc3); tapdu = new Thread(sAPDU); tapdu.start(); public void commandaction(command com, Displayable d) { // TODO Auto-generated method stub //class to send apdu command an get apdu response (encryption and decryption operations) class Send_APDU implements Runnable { private Midlet_Misc parent; private APDUConnection apduconnectionref = null; private byte[] data = null; private byte[] response = null; private byte tag; Send_APDU(Midlet_Misc parent, byte[] data, byte tag) { this.parent = parent; this.data = data; this.tag = tag; private void openapduconnection() { //Opening SATSA APDU connection parent.setmessage("opening"); apduconnectionref = (APDUConnection) Connector.open("apdu:0;target=A0.00.00.00.18.50.00.00.00.00.00.00.52.41.44.41"); parent.setmessage("opening Done - "+String.valueOf(data.length)); catch (ConnectionNotFoundException ex) { parent.setmessage("not able to make connection "); catch (IOException ex) { //parent.display_alert(alerttype.info, "INFO","Error opening APDU 1...", 2000); parent.setmessage("error Opening..."); private void tcloseapduconnection() { -4-
if (apduconnectionref!= null) { //Closing SATSA APDU connection apduconnectionref.close(); catch (Exception ex) { parent.setmessage("error closing APDU..."); private byte[] sendapdu(byte[] messapdu) { byte[] result = null; //Sending APDU command result = apduconnectionref.exchangeapdu(messapdu); catch (IOException ioerr) { parent.setmessage("can't send APDU..."); catch (IllegalArgumentException argerr) { parent.setmessage("illegal APDU..."); return result; public static String arraytohex(byte[] data) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < data.length; i++) { String bs = Integer.toHexString(data[i] & 0xFF); if (bs.length() == 1) { sb.append(0); sb.append(bs); sb.append((i + 1) % 8 == 0? '\n' : ' '); return sb.tostring(); public void run() { openapduconnection(); response = sendapdu(data); if (tag == (byte) 0xC1) { parent.setmessage(arraytohex(response)); parent.sendsms(new String(response)); if (tag == (byte) 0xC3) { //parent.setmessage("\n" + new String(response)); parent.setmessage(arraytohex(response)); tcloseapduconnection(); -5-
//class that handles the reception of SMS class Receive_SMS implements Runnable, MessageListener{ private Midlet_Misc parent; private String smsport; private MessageConnection smsconn; private Message msg; private Thread tsms; private boolean running; Receive_SMS(String smsport,midlet_misc parent) { this.parent = parent; this.smsport = smsport; running = true; String smsconnection = "sms://:" + smsport; //Open the message connection if (smsconn == null) { smsconn = (MessageConnection)Connector.open(smsConnection); smsconn.setmessagelistener(this); parent.setmessage("#sms connection opened"); catch (IOException ioe) { ioe.printstacktrace(); System.out.println(smsConnection); smsconn.setmessagelistener(this); catch (IOException ioe) { ioe.printstacktrace(); parent.setmessage("sms connection mode error"); tsms = new Thread(this); tsms.start(); public void closeconnection() { if (smsconn!= null) { smsconn.close(); catch (IOException e) { //Ignore any errors on shutdown System.out.println("Closing intermediary SMS connection..."); public void notifyincomingmessage(messageconnection conn) { //tsms = new Thread(this); -6-
//tsms.start(); public void run() { while (running) { msg = smsconn.receive(); if (msg!= null) { String receivedmessage = ((TextMessage)msg).getPayloadText(); parent.setmessage("sms received from "+msg.getaddress()); parent.receivedsms(receivedmessage); catch (IOException e) { e.printstacktrace(); //class that handles the emission of SMS class Send_SMS implements Runnable { private String message; private String adr; private String myself; private Midlet_Misc parent; private String smsport; private String address; private String desadr; Send_SMS (String smsport,string message,string myself,string adr,midlet_misc parent) { this.smsport = smsport; this.message = message; this.parent = parent; this.adr = adr; this.myself = myself; public void run() { desadr = "sms://" + adr; address = desadr + ":" + smsport; MessageConnection smsconn = null; // open the message connection smsconn = (MessageConnection)Connector.open(address); TextMessage txtmessage =(TextMessage)smsconn.newMessage(MessageConnection. TEXT_MESSAGE); txtmessage.setaddress(address); txtmessage.setpayloadtext(message); smsconn.send(txtmessage); //parent.setmessage("#sent via sms"); -7-
catch (Throwable t) { parent.setmessage("send caught: "+smsport); t.printstacktrace(); // close the message connection if (smsconn!= null) { smsconn.close(); //parent.sendmessagesms(); catch (IOException ioe) { parent.setmessage("closing connection caught"); ioe.printstacktrace(); -8-