文件名称:javacard applet 开发实例 正常运行 带jar包
文件大小:50KB
文件格式:RAR
更新时间:2017-06-19 16:57:42
javacard applet 开发实例
javacard applet 开发实例 正常运行 带jar包
package mifare;
import com.ibm.jz.JZSystem;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
public class AmbiqApplet extends Applet {
byte[] mifarePassDefault={(byte)0x0b,(byte)0x54,(byte)0x57,(byte)0x07,(byte)0x45,(byte)0xfe,(byte)0x3a,(byte)0xe7};
byte[] mifarePass8={(byte)0x0b,(byte)0x54,(byte)0x57,(byte)0x07,(byte)0x45,(byte)0xfe,(byte)0x3a,(byte)0xe7};
byte[] mifarePass9={(byte)0x0b,(byte)0x54,(byte)0x57,(byte)0x07,(byte)0x45,(byte)0xfe,(byte)0x3a,(byte)0xe7};
byte[] adminPass={(byte)0x40,(byte)0x41,(byte)0x42,(byte)0x43,(byte)0x44,(byte)0x45,(byte)0x46,(byte)0x47,(byte)0x48,(byte)0x49,(byte)0x4a,(byte)0x4b,(byte)0x4c,(byte)0x4d,(byte)0x4e,(byte)0x4f};
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new AmbiqApplet().register(bArray, (short) (bOffset + 1),
bArray[bOffset]);
}
public void process(APDU apdu) {
short i;
byte[] send=new byte[52];
byte[] send2=new byte[24];
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
byte[] buf = apdu.getBuffer();
if(buf[ISO7816.OFFSET_CLA]==AmbiqConstant.CLS_AMBIQ)
{
switch (buf[ISO7816.OFFSET_INS]) {
case AmbiqConstant.INS_GET_MIFARE:
if(buf[ISO7816.OFFSET_P1]!=0 || buf[ISO7816.OFFSET_P2]!=0)
{
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
return;
}
/*Get Entire Mifare Ticket data
* Returns back the content of the entire sector 8 (3 block 8.0, 8.1, 8.2) and the first 4 bytes of
block 9.0; total 52bytes.
Filed Length (in bytes) Data Comments
CLA 1 0x80 Fixed class for the applet
INS 1 0x20 Read entire data
P1 1 0x00 Sector number
P2 1 0x00
Lc field 1 0x10 Admin pass length
Lc Data[0-15] 16 ATS Admin password
Output parameters:
52 bytes; 48bytes of sector 8 and the value of the electronic purse (4bytes) stored in sector 9.
*/
else if(buf[ISO7816.OFFSET_LC]==0x10)//Get Entire Mifare Ticket data
{
if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, (short)16)!=0)
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN);
}
for(i=0; i<8; i++)
{
buf[i]=mifarePass8[i];
}
i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_0));
if(i==0)
{
//javacard.framework.Util.arrayCopy(send, (short)8, apdu.getBuffer(), (short)0, (short)16);
javacard.framework.Util.arrayCopy(buf, (short)8, send, (short)0, AmbiqConstant.BYTES_PER_BLOCK);
}
else
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_S8);
}
i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_1));
if(i==0)
{
//javacard.framework.Util.arrayCopy(send, (short)8, apdu.getBuffer(), (short)16, (short)16);
javacard.framework.Util.arrayCopy(buf, (short)8, send, (short)16, AmbiqConstant.BYTES_PER_BLOCK);
}
else
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_S8);
}
i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_2));
if(i==0)
{
//javacard.framework.Util.arrayCopy(send, (short)8, apdu.getBuffer(), (short)32, (short)16);
javacard.framework.Util.arrayCopy(buf, (short)8, send, (short)32, AmbiqConstant.BYTES_PER_BLOCK);
}
else
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_S8);
}
for(i=0; i<8; i++)
{
buf[i]=mifarePass9[i];
}
i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_9+AmbiqConstant.BLOCK_NUM_OF_0));
if(i==0)
{
javacard.framework.Util.arrayCopy(buf, (short)8, send, (short)48, (short)4);
}
else
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_S9);
}
i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_9+AmbiqConstant.BLOCK_NUM_OF_1));
if(i==0)
{
if(javacard.framework.Util.arrayCompare(buf, (short)8, send, (short)48, (short)4)!=0)
{
ISOException.throwIt(AmbiqConstant.SW_CARD_COR);
}
javacard.framework.Util.arrayCopy(send, (short)0, buf, (short)0, (short)52);
apdu.setOutgoingAndSend((short)0, (short) 52);
}
else
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_S9);
}
}
else
{
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
}
break;
case AmbiqConstant.INS_SET_MIFARE:
if(buf[ISO7816.OFFSET_P1]!=0 || buf[ISO7816.OFFSET_P2]!=0)
{
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
}
/*Set Entire Mifare Ticket data
Sets the content of the entire sector 8 (3 block 8.0, 8.1, 8.2) and the first 4 bytes of block 9.0;
total 52bytes.
Filed Length (in bytes) Data Comments
CLA 1 0x80 Fixed class for the applet
INS 1 0x20 Write entire data
P1 1 0x00 Sector number
P2 1 0x00
Lc field 1 0x44(16+52) Admin pass length + 52data bytes
Lc Data[0-15] 16 ATS Admin password
Lc Data[16-43] 52 Ticket data
Output parameters:
None.
Return Value:
0x90,00 on success.*/
else if(buf[ISO7816.OFFSET_LC]==0x44)//Set Entire Mifare Ticket data
{
if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, (short)16)!=0)
{
ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN);
}
javacard.framework.Util.arrayCopy(buf, (short)21, send, (short)0, (short)52);
for(i=0; i
【文件预览】:
ambig
----bin()
--------mifare()
----.jcop(508B)
----src()
--------mifare()
----.project(565B)
----.classpath(286B)
----lib()
--------jc221.jar(43KB)
--------gp211.jar(4KB)
--------bio10.jar(4KB)
--------jz20-mifare.jar(1KB)