Gets the buffer contents and returns the buffer contents to-java card development first

Source: Internet
Author: User

Task Description:



First, the code, and then parse:

Package Test;//import Helloworld;import Javacard.framework.apdu;import javacard.framework.applet;import Javacard.framework.iso7816;import Javacard.framework.isoexception;import Javacard.framework.util;public Class HelloWorld extends Applet {public static void install (byte[] Barray, short boffset, byte blength) {//Gp-compliant Javacar D applet registrationnew HelloWorld (). Register (Barray, (short) (Boffset + 1), Barray[boffset]);} public void process (APDU APDU) {//Good Practice:return 9000 on selectif (Selectingapplet ()) {Return;} byte[] buf = Apdu.getbuffer ();//define the reference array of the buffer, that is, the buffer contents can be obtained in real time through the BUF array//The APDU command sent by the terminal into the buffer, the length of the data (that is APDU) is returned short LC = Apdu.setincomingandreceive ();//byte[] src = {0};//Gets the data part of the BUF array byte ins = buf[iso7816. Offset_ins];switch (INS) {case (byte) 0x00://gets the data from the buffer array buf and then copies it to Src//util.arraycopynonatomic (buf, ISO7816. Offset_cdata, SRC, (short) 0, (short) buf.length), Apdu.setoutgoingandsend ((short) 5, LC);//Use this command to directly return the buffer data to the terminal,// Where the first parameter represents the offset, which is the return from the first element of the buffer, followed by a parameter that represents the return dataLength,//That is, the data that is ultimately returned to the terminal is from the offset element of the buffer to the content of the offset+length element break;default://good practice:if you don ' t know the instruction, say So:ISOException.throwIt (iso7816.sw_ins_not_supported);}}

A. java file is automatically generated after the new applet has been written with some code frameworks in it. This little practice adds a few lines of code to yourself. What do you do?

(1) First of all, to understand the most basic, you this code is where to run, this is about where to accept the data and where to send the problem! Obviously, it is called Java Card applet developed, of course, it is running on one end of card cards! It happened that when I first got mixed up, I couldn't write the code. Yes, these codes are all card owners, what about the terminal (reader)? The terminal is not where you enter the command line on the Eclipse JCOP debug page! The command line that you write the command to send is sent to the card, and then the card through the applet code you write to get to the terminal sent over the command, according to the command of a series of processing (that is, the code is running).

(2) Since the code to the terminal sent over the command to get and processing, then how to get it? Jcop has helped us to encapsulate a thing, called APDU, this thing (is a class) in the store is sent from the terminal command, right, the terminal sent over the command is APDU command, what is called APDU? : APDU (Application Protocol Data Unit application protocol The data Units) command.

(3) Since the APDU command from the terminal is in the class of APDU, we can use this APDU class to get everything in the command sent by the terminal, what does this so-called command include? :



Yes, this is the terminal sent over the APDU command a few large modules, each block as its name (abbreviation), the first byte (note that the real byte is not two bits!). ) represents class instruction classes, the second byte is the INS instruction encoding, the third and fourth bytes are parameter 1 and parameter 2, and the fifth byte LC represents the length of the subsequent data (how many bytes), then the data part is the real one, the longest can be 255 bytes, The last Le byte represents the length of bytes expected to be sent back by the card, for example, 08 means I want the card you send me back 8 bytes of data (not including 9000 of these two bytes), if 00 means more and more, the card you can send back the data to me all sent.

Well, this is the APDU command that the terminal sends to the card, what is the structure of the APDU command that the card sends to the terminal? Such as:


The first block represents the data to be returned, the length is variable, here is the link to the above-mentioned terminal expects the card to send back how long the data, that is, the length of the data. The next two bytes are two state bytes, and the two add up is 9000 to indicate that the normal completion of the instruction processing, that is OK no problem!

(4) Well, the above is written before the code must understand the basis, and then say back to the code: first, the terminal sent over the APDU command read into the card buffer, there are two methods, one is a function:short Setincomingandreceive (), the second is to use the public function short receivebytes (short boff), only one time to accept the data with the previous function is enough, the latter is used to accept APDU multiple times. The detailed explanation is in the textbook "Java Smart card principle and application Development" on page 101. Yes, there are textbooks! As long as you are willing to take the time to chew the textbook, which is not afraid to knock code? So don't start from the beginning to the eclipse to how to knock code, the principle of understanding the code can be killed in seconds! So, are you getting a bit more and more agrochemical? Danger!! The caution of abstinence!!!

(5) Well, the above has put APDU stored in the buffer of the cards, and then through

byte[] buf = Apdu.getbuffer ();

This command stores the contents of the buffer in the BUF byte array. Note that this is equivalent to pointing a pointer to the buffer, so the buffer changes are updated in real time to the BUF array.

(6) then through the above BUF array, you can get to the APDU command of each module, here need to know a ghost thing called ISO7816 interface, detailed explanation in just said that the textbook 105 pages, go back to see it. In short, use


This code can read out the INS module in APDU, what module? Take a look at some of the big modules of the APDU command that you just said!

(7) then how to get to the Data module section? A special function is needed here:

Util.arraycopynonatomic (buf, ISO7816. Offset_cdata, SRC, (short) 0, LC);

This command is to copy the contents of the BUF buffer array into the SRC byte array (of course, to implement a definition of the SRC array and initialize), the second parameter represents offset, that is, from the BUF array to start copying, the last parameter represents the length of the copy, Here the LC represents the length of the data in the APDU command, so it is the first byte of the data section to copy all the data bytes.

This command is very important, because if you want to customize a byte array src, want to return the contents of it to the terminal, it is also through this command to copy the contents of SRC to the BUF buffer array, only need to change the parameters of this command or the position can be swapped.

(8) Then is the contents of the buffer to send back to the terminal, note that the terminal sent over the APDU command is first in the buffer, and then the card to send back to the APDU command is placed in the buffer and then sent to the terminal, so it will produce coverage, So you can first remove the contents of the buffer and put it in another array and overwrite it. Of course, the above problem only need to directly send the original buffer portion of the content directly back to the terminal is enough, the following code:

Apdu.setoutgoingandsend (short) 5, LC);

This code indicates that the contents of the buffer are sent back to the terminal, the first parameter represents offset offsets, which indicates the start of sending from the first byte of the buffer (note that (short) 0 means the start of sending from the beginning, that (short) 5 means the6Bytes to start sending. Why the 6th one? Look at the main structure of the CAPDU command, the front is the command head, the 6th byte start is the real data [in fact, the IP inside the computer network has such a head, it can be seen that a lot of basic knowledge or experience is common]), the back is the length, The number of bytes in the buffer to send to the terminal (which is associated with the length at which the terminal expects to send back data).

Well, for this question on this point, finally run the applet, the command line to send commands, and finally show the contents of the card returned, the problem is that the card directly sends the data sent back to the terminal intact:


The ID of the Select applet is sent at the command line first



Then you can send the content command:


See those two arrows no, yes, the right arrow indicates that the terminal is sent to the card, and the left arrow indicates that it is recycled from the card. The final card sent back the command is: the original data+9000.


Add an improved version of the code that can copy the "Hello" of the existing byte array to the buffer to return to the terminal output:

Package Test;//import Helloworld;import Javacard.framework.apdu;import javacard.framework.applet;import Javacard.framework.iso7816;import Javacard.framework.isoexception;import Javacard.framework.util;public Class HelloWorld extends Applet {public static void install (byte[] Barray, short boffset, byte blength) {//Gp-compliant Javacar D applet registrationnew HelloWorld (). Register (Barray, (short) (Boffset + 1), Barray[boffset]);} public void process (APDU APDU) {//Good Practice:return 9000 on selectif (Selectingapplet ()) {Return;} Defines a reference array of buffers, that is, the buffer contents can be obtained in real-time through the BUF array byte[] buf = Apdu.getbuffer ();//Send the terminal's APDU command into the buffer and return the length of the data (that is, in APDU) short LC = Apdu.setincomingandreceive (); byte[] src = {' h ', ' e ', ' l ', ' l ', ' o '};//get the data part of the BUF array byte ins = buf[iso7816. Offset_ins];switch (INS) {case (byte) 0x00://gets the data from the buffer array buf and then copies it to Src//util.arraycopynonatomic (buf, ISO7816. Offset_cdata, SRC, (short) 0, (short) buf.length);//Copy the array in the byte array src into the buffer array util.arraycopynonatomic (SRC, (short) 0, buf, ( Short) 0, (Src.le)Ngth); Apdu.setoutgoingandsend ((short) 0, (short) src.length);//Use this command to directly return the buffer data to the terminal,//where the first parameter represents an offset, That is, from the first element of the buffer to return, the following parameter represents the length of the returned data,//That is, the final data returned to the terminal is from the first offset element of the buffer to the content of offset+length elements// Apdu.setoutgoingandsend (short) 0, LC);//This change will return 8 bytes of databreak;default://good practice:if you don ' t know the instruction, say So:ISOException.throwIt (iso7816.sw_ins_not_supported);}}

Another piece of code, this code implements the function of returning Hello world!, similar to the above, but using new method to create an array of cache, new method does not need to like byte[] src ... Such a method must be initialized in order to be used (the use of this is mainly referred to in the Util function). And this is how the textbook defines a new cache array using new methods.

/*author:lvlangdate:2016-4-1*/package Test;//import Helloworld;import Javacard.framework.apdu;import Javacard.framework.applet;import Javacard.framework.iso7816;import Javacard.framework.isoexception;import Javacard.framework.util;public class HelloWorld extends Applet {//define a data buffer and constants for maximum data length private static final short Data_ max= 256;private byte[] src;public static void Install (byte[] Barray, short boffset, byte blength) {//Gp-compliant Javaca Rd applet Registrationnew HelloWorld (). Register (Barray, (short) (Boffset + 1), Barray[boffset]);} public void process (APDU APDU) {//Good Practice:return 9000 on selectif (Selectingapplet ()) {Return;} src = new byte[data_max];//Create the corresponding data buffer byte[] str = {' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ', '! '}; Short Str_len = (short) str.length; Util.arraycopynonatomic (str, (short) 0, SRC, (short) 0, str_len);//define a reference array for the buffer, that is, the buffer contents can be obtained in real time through the BUF array byte[] buffer = Apdu.getbuffer ();//The APDU command sent by the terminal into the buffer, the length of the returned data (that is, APDU) short LC = Apdu.setincomingandreceive ();//Get INS segmentbyte ins = buffer[iso7816. Offset_ins];switch (INS) {case (byte) 0x00://gets the data from the buffer array buf and then copies it to SRC//util.arraycopynonatomic (buffer, ISO7816. Offset_cdata, SRC, (short) 0, LC);//Copy the array in the byte array src into the buffer array util.arraycopynonatomic (SRC, (short) 0, buffer, ISO7816. Offset_cdata, Str_len) apdu.setoutgoingandsend ((short) 5, str_len);//Use this command to directly return the buffer data to the terminal,//where the first parameter represents an offset, That is, from the first element of the buffer to return, the following parameter represents the length of the returned data,//That is, the final data returned to the terminal is from the beginning of the buffer of the offset element to the content of offset+length elements break;default://good Practice:if you don ' t know the instruction, say So:ISOException.throwIt (iso7816.sw_ins_not_supported);}}


Operation Result:



Finally, say a few tips:

(1) Run the direct point to run the right triangle, select the project name in the popup window can be run directly, do not need to run as so troublesome every time


(2) Open the runtime interface with the above method to re-run the changes in real-time update code, do not need to close off the running window


Gets the buffer contents and returns the buffer contents to-java card development first

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.