[英]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.