Javaのカードとスマートカードにファイルを書き込む方法

tecogill:

こんにちは、私はジャバカードの使用上の初心者ですよ!私は例えば、私は638の長さの配列を持って、私は何をするのバイト(バイト[])の配列にファイルを変換することですので、私のスマートカード内のファイル(1024キロバイト未満)を書きたいです!私の書き込みにコードとデータを読み込むには、罰金(エラーなし)に動作しますが、私は、スマートカードからデータを読み込み、デフォルトの配列と比較したとき、彼らは(同じ長さが異なるコンテンツ)と同じではありません。私はここで話題をたくさん読んで、私は私の問題を解決することができるものを見つけることができませんでした!ご協力ありがとうございます

私はJCDK 2.2.2使用します

私のアプレット読み取りおよび書き込みデータをその:

.....
public void process(APDU apdu) {
    byte[] buffer = apdu.getBuffer();
    switch(buffer[ISO7816.OFFSET_INS]) {
        case INS_SET_DATA:
            // byte bufferDataLength = (byte)(apdu.setIncomingAndReceive());

            //We want to start data copy
            if (buffer[ISO7816.OFFSET_P1] == 0x00 && buffer[ISO7816.OFFSET_P2] == 0x00) {
                data = new byte[(short) 638];
            } else {
                // copying the apdu data into byte array Data
                // array copy: (src, offset, target, offset,copy size)
                Util.arrayCopyNonAtomic(buffer, (short) ISO7816.OFFSET_CDATA, data,
                            (short) ((short) buffer[ISO7816.OFFSET_P1] * 100),
                            (short) buffer[ISO7816.OFFSET_P2]
                    );
                }
            break;
        case INS_GET_DATA:
            // array copy: (src, offset, target, offset,copy size)
            short t = (short)(buffer[ISO7816.OFFSET_P2] & 0xFF);
            short o = (short)(buffer[ISO7816.OFFSET_P1] & 0xFF);

            Util.arrayCopyNonAtomic(data, o, buffer, ISO7816.OFFSET_CDATA, t);
            apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, t);
            break;
    }
}
.....

私はスマートカードにデータを送信するために使用するコード(JCDK 2.2.2のapduioパッケージ)

.....

switch(choice) {
   case 8:
       byte[] configData = fileToByteArray("D:\\jcard\\config.dat");
       byte[] chunk = new byte[100];

       System.out.println("Config Length: " + configData.length); // Got 638

       int configLength = configData.length;
       double round = Math.floor((double)configLength / 100);
       int lastPart = configLength % 100;

       System.out.println("Round : " + round); // Got 6
       System.out.println("Last Part : " + lastPart); // Got 38

       double partToSend = lastPart == 0 ? round : round + 1;

       //Initialize the array of byte in my applet to have the length of data i want to write in
       apdu.command[Apdu.INS] = MyAppletClient.INS_SET_DATA;
       apdu.command[Apdu.P1] = 0x00;
       apdu.command[Apdu.P2] = 0x00;

       cad.exchangeApdu(apdu);
       handleResponse(apdu);

       boolean allPartSend = true;
       for (int i = 0; i < round; i++) {
           //array copy: (src, offset, target, offset, copy size)
           System.arraycopy(configData, (i * 100), chunk, 0, 100);
           System.out.println("Data Length: " + chunk.length); // Got 100

           apdu.command[Apdu.P1] = (byte) i;
           apdu.command[Apdu.P2] = 100;

           apdu.setDataIn(Data);
           cad.exchangeApdu(apdu);
           if (apdu.getStatus() != 0x9000) {
               System.out.println("["+i+"] An error occurred with status: " + apdu.getStatus());
               allPartSend = false;
               break;
            }
        }

        if(allPartSend) {
            byte[] finalPart = new byte[lastPart];
            System.arraycopy(configData, (int) (round * 100), finalPart, 0, lastPart);

            apdu.command[Apdu.P1] = (byte) round;
            apdu.command[Apdu.P2] = (byte) lastPart;
            apdu.setDataIn(finalPart);
            cad.exchangeApdu(apdu);

            if (apdu.getStatus() != 0x9000) {
                System.out.println("An error occurred with status: " + apdu.getStatus());
                break;
            } else {
                System.out.println("OK");
            }
        } else {
            System.out.println("Fail to send all data");
        }
        break;
    case 9:
        int cfgLength = 638; //Because i know the array has that length
        byte[] res = new byte[cfgLength];
        double rnd = Math.floor((double)cfgLength / 100);
        int last = fpLength % 100, readLength = 0;

        boolean allSend = true;

        for(int i = 0; i < rnd; i++) {
            apdu.command[Apdu.INS] = MyAppletClient.INS_GET_DATA;
            apdu.command[Apdu.P1] = (byte) (i * 100);
            apdu.command[Apdu.P2] = 100;
            cad.exchangeApdu(apdu);

            if (apdu.getStatus() != 0x9000) {
                System.out.println("An error occurred with status: " + apdu.getStatus());
            } else {
                readLength += apdu.dataOut.length;
                // System.out.println("Datalength : " + apdu.dataOut.length); // Got 100
                //array copy: (src, offset, target, offset, copy size)
                System.arraycopy(apdu.dataOut, 0, res, (i*100), apdu.dataOut.length);
            }
         }

         if(allSend) {
             apdu.command[Apdu.INS] = MyAppletClient.INS_GET_DATA;
             apdu.command[Apdu.P1] = (byte) ((int)rnd * 100);
             apdu.command[Apdu.P2] = (byte) last;

             cad.exchangeApdu(apdu);
             if (apdu.getStatus() != 0x9000) {
                 System.out.println("An error occurred with status: " + apdu.getStatus());
                 break;
             } else {
                 readLength += apdu.dataOut.length;
                 //array copy: (src, offset, target, offset, copy size)
                 System.arraycopy(apdu.dataOut, 0, res, (int)(rnd * 100 ), apdu.dataOut.length);          
             }
         } else {
              System.out.println("Fail to get all the part");
         }

         byte[] cfgData = fileToByteArray("D:\\jcard\\config.dat");
         System.out.println("ReadLength : " + readLength); // Got 638
         System.out.println("Array equals : " + Arrays.equals(cfgData, res)); // Got false but expected true

         break;
     }
}

.....
ミハル・Iwanicki:

私の推測では、あなたがカードに書いたものから異なるデータを受信して​​いる理由は、このコード行であるということです。

switch(choice) {
...
    case 9:
    ...
        for(int i = 0; i < rnd; i++) {
        ...
            apdu.command[Apdu.P1] = (byte) (i * 100);

カードにデータを書き込むためのコードとは異なり、ここでは、P1パラメータでオフセットデータを渡しています。P1は1バイトのみであると私は3を=のために、それはP1で長い列を638バイトのエンコードオフセットすることはできませんので、それは、オーバーフローすることを、覚えておいてください。

あなたの問題の迅速な解決方法はすなわち、あなたの文章のコードと同じアプローチを使用することです。

switch(choice) {
...
    case 9:
    ...
        for(int i = 0; i < rnd; i++) {
        ...
            apdu.command[Apdu.P1] = (byte) i;

そして、Java(登録商標)Cardコードで乗算を実行します。

short o = (short) ((short) buffer[ISO7816.OFFSET_P1] * 100);

このソリューションは動作するはずですが、それはリード/ライト(P2 - コピー長)のご使用のJavaCardコードのサポート、任意の長さと完璧されていませんが、オフセットが0に固定され、100、200などそれはそれだけで限り、あなたのように動作することを意味しあなたの現在のコードがありませんカードにP2 = 100を送信します。それは1つの値に固定されている場合しかし、その後、なぜ最初の場所では、このパラメータを送りますか?

私はそれがISO / IEC 7816-4に従うと構文が指定されたコマンドを使用するのがベストだろうとマールテンBodewesに同意します。標準で説明UPDATE BINARYおよびREAD BINARYコマンドは、あなたのニーズに合わせなければなりません。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=331540&siteId=1