下载 >  开发技术 >  其它 > javacard applet 开发实例 正常运行 带jar包

javacard applet 开发实例 正常运行 带jar包 评分:

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<AmbiqConstant.LEN_MIFARE_PASS; i++) { buf[i]=mifarePass8[i]; } for(i=0;i<AmbiqConstant.BYTES_PER_BLOCK;i++) { buf[AmbiqConstant.LEN_MIFARE_PASS+i]=send[0*AmbiqConstant.BYTES_PER_BLOCK+i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_0)); if(i==0) { //apdu.setOutgoingAndSend((short)8, (short) 4); //javacard.framework.Util.arrayCopy(send, (short)8, apdu.getBuffer(), (short)0, (short)16); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_S8); } for(i=0;i<AmbiqConstant.BYTES_PER_BLOCK;i++) { buf[AmbiqConstant.LEN_MIFARE_PASS+i]=send[1*AmbiqConstant.BYTES_PER_BLOCK+i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_1)); if(i!=0) { ISOException.throwIt(AmbiqConstant.SW_WRONG_S8); } for(i=0;i<AmbiqConstant.BYTES_PER_BLOCK;i++) { buf[AmbiqConstant.LEN_MIFARE_PASS+i]=send[2*AmbiqConstant.BYTES_PER_BLOCK+i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_2)); if(i!=0) { ISOException.throwIt(AmbiqConstant.SW_WRONG_S8); } for(i=0; i<AmbiqConstant.LEN_MIFARE_PASS; i++) { buf[i]=mifarePass9[i]; } for(i=0;i<4;i++) { buf[AmbiqConstant.LEN_MIFARE_PASS+i]=send[3*AmbiqConstant.BYTES_PER_BLOCK+i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_9+AmbiqConstant.BLOCK_NUM_OF_0)); if(i!=0) { ISOException.throwIt(AmbiqConstant.SW_WRONG_S9); } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_9+AmbiqConstant.BLOCK_NUM_OF_1)); if(i==0) { apdu.setOutgoingAndSend((short)0, (short) 0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_S9); } } else { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); } break; case AmbiqConstant.INS_GET_CSN: if(buf[ISO7816.OFFSET_P1]!=0 || buf[ISO7816.OFFSET_P2]!=0) { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); return; } else if(buf[ISO7816.OFFSET_LC]!=0) { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); return; } for(i=0; i<AmbiqConstant.LEN_MIFARE_PASS; i++) { buf[i]=mifarePass8[i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)0); if(i==0) { //apdu.setOutgoingAndSend((short)AmbiqConstant.LEN_MIFARE_PASS, (short) 4); apdu.setOutgoingAndSend((short)0, (short)4); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_B0); } break; case AmbiqConstant.INS_SET_PASS: if(buf[ISO7816.OFFSET_P1]==0 && buf[ISO7816.OFFSET_P2]==0 && buf[ISO7816.OFFSET_LC]==0x20)//set admin password { if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, AmbiqConstant.LEN_ADMIN_PASS)==0) { Util.arrayCopy(buf, (short)(ISO7816.OFFSET_CDATA+AmbiqConstant.LEN_ADMIN_PASS), adminPass, (short)0, AmbiqConstant.LEN_ADMIN_PASS); apdu.setOutgoingAndSend((short)0, (short) 0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN); } } else if(buf[ISO7816.OFFSET_P1]==0x01 && buf[ISO7816.OFFSET_P2]==0 && buf[ISO7816.OFFSET_LC]==0x18) //set mifare password for sector 8 { if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, AmbiqConstant.LEN_ADMIN_PASS)==0) { for(i=0;i<AmbiqConstant.LEN_MIFARE_PASS;i++) mifarePass8[i]=buf[ISO7816.OFFSET_CDATA+AmbiqConstant.LEN_ADMIN_PASS+i]; apdu.setOutgoingAndSend((short)0, (short) 0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN); } } else if(buf[ISO7816.OFFSET_P1]==0x02 && buf[ISO7816.OFFSET_P2]==0 && buf[ISO7816.OFFSET_LC]==0x18) //set mifare password for sector 9 { if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, AmbiqConstant.LEN_ADMIN_PASS)==0) { for(i=0;i<AmbiqConstant.LEN_MIFARE_PASS;i++) mifarePass9[i]=buf[ISO7816.OFFSET_CDATA+AmbiqConstant.LEN_ADMIN_PASS+i]; apdu.setOutgoingAndSend((short)0, (short) 0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN); } } else { ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } break; case AmbiqConstant.INS_SET_OTHER_PASS: /*Set Mifare password for some sector except 8,9 * Returns the content of the block; total 16bytes. Filed Length (in bytes) Data Comments CLA 1 0x80 Fixed class for the applet INS 1 0x53 Set Mifare password for other sector except 8,9 P1 1 0x00 P2 1 0x00 Lc field 1 0x18 Length Lc Data[0-15] 16 ATS Admin password Lc Data[16-23] 8 MFare password Output parameters: SW */ if(buf[ISO7816.OFFSET_P1]==0x00 && buf[ISO7816.OFFSET_P2]==0 && buf[ISO7816.OFFSET_LC]==0x18) //set mifare password for sector 8 { if(Util.arrayCompare(adminPass, (short)0, buf, ISO7816.OFFSET_CDATA, AmbiqConstant.LEN_ADMIN_PASS)==0) { for(i=0;i<AmbiqConstant.LEN_MIFARE_PASS;i++) mifarePassDefault[i]=buf[ISO7816.OFFSET_CDATA+AmbiqConstant.LEN_ADMIN_PASS+i]; apdu.setOutgoingAndSend((short)0, (short) 0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_ADMIN); } } else { ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } break; case AmbiqConstant.INS_GET_BLOCK: /*Get Mifare block data * Returns the content of the block; total 16bytes. Filed Length (in bytes) Data Comments CLA 1 0x80 Fixed class for the applet INS 1 0x50 Get Mifare block data P1 1 Sector number P2 1 Block number Lc field 1 0x10 Admin pass length Lc Data[0-15] 16 ATS Admin password Output parameters: SW+16 bytes data; */ if(buf[ISO7816.OFFSET_LC]==0x10)//Get Entire Mifare Ticket data { short blockNumber=(short)(AmbiqConstant.BLOCKS_PER_SECTOR*buf[ISO7816.OFFSET_P1]+buf[ISO7816.OFFSET_P2]); 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]=mifarePassDefault[i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, blockNumber); if(i==0) { apdu.setOutgoingAndSend((short)8, (short)16); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_S9); } } else { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); } break; case AmbiqConstant.INS_SET_BLOCK: /*set Mifare block data * Returns SW. Filed Length (in bytes) Data Comments CLA 1 0x80 Fixed class for the applet INS 1 0x51 set Mifare block data P1 1 Sector number P2 1 Block number Lc field 1 0x20 Length Lc Data[0-15] 16 ATS Admin password Lc Data[16-31] 16 Data in block Output parameters: SW; */ if(buf[ISO7816.OFFSET_LC]==0x20)//Set Block data { short blockNumber=(short)(AmbiqConstant.BLOCKS_PER_SECTOR*buf[ISO7816.OFFSET_P1]+buf[ISO7816.OFFSET_P2]); 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]=mifarePassDefault[i]; } i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_WRITE, buf, (short)0, blockNumber); if(i==0) { apdu.setOutgoingAndSend((short)0, (short)0); } else { ISOException.throwIt(AmbiqConstant.SW_WRONG_S9); } } else { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); } break; case AmbiqConstant.INS_GET_APPLET_VER: /*Get applet version * Returns the version number of applet, one byte. Filed Length (in bytes) Data Comments CLA 1 0x80 Fixed class for the applet INS 1 0x55 Get applet version P1 1 0x00 P2 1 0x00 Lc field 1 0x00 Length Output parameters: 1byte+SW */ if(buf[ISO7816.OFFSET_P1]!=0 || buf[ISO7816.OFFSET_P2]!=0) { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); return; } else if(buf[ISO7816.OFFSET_LC]!=0) { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); return; } buf[0]=AmbiqConstant.APPLET_VERSION; apdu.setOutgoingAndSend((short)0, (short)1); break; default: // good practice: If you don't know the INStruction, say so: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } } } }
...展开详情收缩
2014-05-30 上传大小:50KB
分享
收藏 (3) 举报

评论 共5条

shichengjiang 请问怎么运行
2017-08-17
回复
dai148 在学习阶段,想找个例子,下来看看
2016-11-24
回复
u010723879 对我来说并没有很大帮助
2015-11-16
回复
qq_14887587 很有用的javacard
2015-10-04
回复
hui61001 刚下载下来,还没试,也只是在学习阶段,想找个例子
2015-08-12
回复

热点文章

下载码下载
做任务获取下载码
取消 提交下载码
img

spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip

资源所需积分/C币 当前拥有积分 当前拥有C币
5 0 0
点击完成任务获取下载码
输入下载码
为了良好体验,不建议使用迅雷下载
img

javacard applet 开发实例 正常运行 带jar包

会员到期时间: 剩余下载个数: 剩余C币: 剩余积分:0
为了良好体验,不建议使用迅雷下载
VIP下载
您今日下载次数已达上限(为了良好下载体验及使用,每位用户24小时之内最多可下载20个资源)

积分不足!

资源所需积分/C币 当前拥有积分
您可以选择
开通VIP
4000万
程序员的必选
600万
绿色安全资源
现在开通
立省522元
或者
购买C币兑换积分 C币抽奖
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 4 45
为了良好体验,不建议使用迅雷下载
确认下载
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 0 0
为了良好体验,不建议使用迅雷下载
VIP和C币套餐优惠
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 4 45
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
确认下载
下载
您还未下载过该资源
无法举报自己的资源

兑换成功

你当前的下载分为234开始下载资源
你还不是VIP会员
开通VIP会员权限,免积分下载
立即开通

你下载资源过于频繁,请输入验证码

您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:webmaster@csdn.net!

举报

  • 举报人:
  • 被举报人:
  • *类型:
    • *投诉人姓名:
    • *投诉人联系方式:
    • *版权证明:
  • *详细原因: