Showing posts with label java card. Show all posts
Showing posts with label java card. Show all posts

Friday, January 4, 2008

Declaration String to bytes variable in Smart Card Coding


/**

* This program is really useful for programmer who is so lazy

* to type word/ sentence in bytes when he/she codes in Smart card

*/



import java.lang.*;

/**

* @version 1.0

* @author kapulaga

*

*/

public class stringtobyte {



public stringtobyte(String variablename, String variablevalue)

{

int i;

System.out.print("byte[] "+variablename+"={");

for(i=0;i

{

if(i

System.out.print("'"+variablevalue.charAt(i)+"'"+",");

else

System.out.print("'"+variablevalue.charAt(i)+"'"+"};");

}



}





public static void main(String[] args) {

stringtobyte stb = new stringtobyte("variable","byte value");



}



}




Monday, November 19, 2007

CANNOT_PLAY_SCRIPT

If you get this error :
An internal error occurred during: "Launching baru".
Can't find resource for bundle java.util.PropertyResourceBundle, key CANNOT_PLAY_SCRIPT

Just restart your developer suit

It works for me :D

Friday, November 16, 2007

Prefix URL

We should declare prefix URL as global variable
For example :
public final static byte[] prefix = { '/', 'm', 's', 'g' };

this prefix will be called by one or more html pages
Here is example for form with post action
action="msg"
or href URL :
href="msg"

Afterwards, we should add it in SCWS container. Type it in your servlet constructor.
servletContainer.add(this, new ByteString(prefix));

Put your sensitive variable in RAM

// declare variable
byte[] requestParameterBuffer;
// allocate it in RAM , ensure the memory length
requestParameterBuffer = JCSystem.makeTransientByteArray(
(short) (queryString.length() * 2), JCSystem.CLEAR_ON_RESET);

JCSystem reset / clean memoryOptions :
  • JCSystem.CLEAR_ON_DESELECT
  • JCSystem.CLEAR_ON_RESET

Static .. Static Variable

Here are smart card programming rules :
  • It is better to use static variable then use instance variable.
  • Constants variable should be declare as static final variable
  • Local variable is accessed faster than global variable
Example Declaration Variable in Smart Card Web Server :
public static final byte[] PARAMETER = { 'P', 'a', 'r', 'a', 'm', 'e', 't','e', 'r', ' ', ':', ' ' };
public final static byte[] PROTOCOL = { 'P', 'r', 'o', 't', 'o', 'c', 'o', 'l', ':' };

No String No Integer

I have just known that there is no integer and string in Smart Card program.
So what are the data type ?

  • byte
  • ByteString
  • short
We should be aware with memory management :)

Wednesday, November 14, 2007

doGet()

I wrote my first servlet code in Smart Card Web Server.
Firstly, here is my html code :




Username:





and this following code is part of my servlet code :

public void doGet(HttpCardServletRequest req, HttpCardServletResponse resp)
throws Exception {

try {

ByteString queryString = req.getQueryString();
if (queryString != null) {

byte[] hello = { 'h', 'e', 'l', 'l','o' ,' '};


ByteString temp=queryString.substring((short)5, queryString.length());

// set header
resp.setStatus(HttpCardServletResponse.SC_OK);
resp.setContentType(stringContainer
.getConstantString(HttpStringContainer.TEXT_PLAIN_KEY));

int length = hello.length+temp.length();

resp.setContentLength(length);
// write body

HttpCardServletOutputStream out = resp.getOutputStream();
out.write(hello,(short)0,(short)hello.length);
out.write(temp);

}
} catch (Exception e) {
resp.setStatus(HttpCardServletResponse.SC_BAD_REQUEST);
}

}
This is the capture of the result :

testing by typing such username
Hey, it works now :D