I'm trying to hash an 8-byte message (maybe need to enlarge it to 128) using Java Card which supports MD5. This is my source code:
package net.sourceforge.globalplatform.jc.helloworld;
import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.Cipher;
import javax.print.attribute.standard.MediaSize;
import java.util.logging.Level;
public class HelloWorldApplet extends Applet {
final static byte APPLET_CLA = (byte)0x80;
final static byte HASH = (byte)0x05;
public static byte[] Message;
MessageDigest mDig = MessageDigest.getInstance(MessageDigest.ALG_MD5, true);
public static void install(byte[] bArray, short bOffset, byte bLength)
{
Message = new byte[256];
new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
public void process(APDU apdu)
{
if (selectingApplet())
{
return;
}
byte[] buffer = apdu.getBuffer();
if (buffer[ISO7816.OFFSET_CLA] == APPLET_CLA) {
switch (buffer[ISO7816.OFFSET_INS]) {
case HASH:
hash_message(apdu);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
} else {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
public void hash_message(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short mLen = apdu.setIncomingAndReceive();
mDig.reset();
mDig.doFinal(buffer, (short) ISO7816.OFFSET_CDATA, mLen, Message, (short) 0);
Util.arrayCopy(Message,(short)0,buffer,(short)0, mLen);
apdu.setOutgoingAndSend((short)0,mLen);
}
}
And this is also my tests using GPShell:
send_apdu -sc 1 -APDU 80050000081122334455667788
Command --> 80050000081122334455667788
Wrapped command --> 80050000081122334455667788
Response <-- DD254CDC958E53AB9000
send_APDU() returns 0x80209000 (9000: Success. No error.)
I have different questions:
I tested my data on MD5 hash using this link and I got that it is not working correctly! I don't know why this algorithm does not respond correctly while I am using a code as simple as possible! can anyone tell me the problem?
Is there any way to send/receive 256 Bytes of data to/from the card?
What is a better substitute for GPShell?
Update 1: I have seen this link and it did not solve my problem.
Update 2: Answer to question 1 returns to Hex and ASCII data difference in GPShell and the online calculator.