[英]Interpreting a java card HelloWorld applet
在下面,您看到一个Java卡程序,当它收到APDU Command = 8000000000
( 其源代码 )时返回“ Hello Word”
package helloWorldPackage;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
public class HelloWorldApplet extends Applet
{
private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};
private static final byte HW_CLA = (byte)0x80;
private static final byte HW_INS = (byte)0x00;
public static void install(byte[] bArray, short bOffset, byte bLength)
{
new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
public void process(APDU apdu)
{
if (selectingApplet())
{
return;
}
byte[] buffer = apdu.getBuffer();
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
if (CLA != HW_CLA)
{
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
switch ( INS )
{
case HW_INS:
getHelloWorld( apdu );
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void getHelloWorld( APDU apdu)
{
byte[] buffer = apdu.getBuffer();
short length = (short) helloWorld.length;
Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);
apdu.setOutgoingAndSend((short)0, length);
}
}
我理解了,但是我不明白为什么程序员&0XFF
中使用&0XFF
:
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
为什么他通常不使用下一行呢?
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA]);
byte INS = (byte) (buffer[ISO7816.OFFSET_INS]);
并且在行中:
ew HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
+1
是什么意思?
尽管我们看不到作者的意图,但该行:
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
是100%等于:
byte CLA = buffer[ISO7816.OFFSET_CLA];
Java确实经常将整数用作运算结果,并且由于Java Card通常不支持int
值,因此通常需要将其转换为byte
或short
。 我只能猜测& 0xFF
和cast存在,是因为过度热情地尝试了摆脱中间int
值。 让Java支持无符号字节也可能是一个错误的尝试。
register
方法需要实例AID。 该AID在INSTALL for INSTALL
的Global Platform INSTALL for INSTALL
期间给出的用户参数之内,但它前面是一个字节,其中包含AID的长度(5到15,含5和15)。 所以+ 1
是跳过该长度的字节-依次出现在register
方法的最后一个参数: bArray[bOffset]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.