3

我正在尝试使用 Galaxy S3 将一些数据写入 Mifare DesFire 卡,其中包含以下几行:

private byte[] wrapMessage (byte command, byte[] parameters) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    stream.write((byte) 0x90);
    stream.write(command);
    stream.write((byte) 0x00);
    stream.write((byte) 0x00);
    if (parameters != null) {
        stream.write((byte) parameters.length);
        stream.write(parameters);
    }
    stream.write((byte) 0x00);

    return stream.toByteArray();
}

boolean isoDepWrite(Tag tag) {
      IsoDep idTag = IsoDep.get(tag);
      idTag.setTimeout(5000);

      String info = "";
      DesfireProtocol dfp = new DesfireProtocol(idTag);
      try {
          idTag.connect();
          info += "Connected to IsoDep Tag...\n";

          int[] appList = dfp.getAppList();
          dfp.selectApp(appList[0]);
          info += "Selected app no: " + appList[0] + "..\n";

          int[] fileList = dfp.getFileList();
          info += "Selected file no: " + fileList[0] + "\n";

          byte[] params = {(byte)fileList[0], 
                           (byte)0x0, (byte)0x0, (byte)0x0, 
                           (byte)0x2, (byte)0x0, (byte)0x0,
                           (byte)0x41, (byte)0x41};
          byte[] message = wrapMessage((byte) 0x3d, params);

          byte[] result = idTag.transceive(message);
          info += "Result bytes: " + convertByteArrayToHexString(result) + "\n";

          toast(info);
          return true;
      } catch (IOException e) {
          info += "Could not connect to IsoDep Tag...\n";
      } catch (Exception e) {
          info += "Error messages: " + e.getMessage() + " -- " + e.getLocalizedMessage() + "\n";
      }

      toast(info);
      return false;
  }

沟通后得到的信息是:

Connected to IsoDep tag...
Selected app no: 1109742 // that shows I connected to an Application
Transceieve result bytes: 91 9e  // PARAMETER ERROR

我可以连接并读取该应用程序的文件,但是在我尝试写入后,该文件中的字节为 0。0x9E 是 PARAMETER_ERROR,所以我在包装/排列字节时做错了什么,有任何字节样本或想法吗?

编辑:我尝试了@nemo 推荐的字节:

{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}

现在我得到“67 00”作为结果字节,这意味着长度错误并且文件保持不变,只有 0。

最后编辑:我只是通过以下方式创建了一个新的字节数组:

wrapMessage(0x3d, rest of the bytes in the list @nemo recommended)

它终于奏效了。我用上面的工作改变了旧的。

4

1 回答 1

3

我认为你的Write命令错了,但这是在黑暗中开枪。

根据官方DESFire文档(尝试搜索M075031WriteData定义如下:

WriteData(FileNo, Offset, Length, Data)

作为字节流,它看起来像这样:

WriteCmd FileNo  Offset (3 byte)  Length (3 byte)  Data (0 to 52 byte)
[0x3D]   [0x00]  [0x00 0x00 0x00] [0x00 0x00 0x00] [0x00 ... 0x00]

甚至可以比 52 字节多写 59 字节,但这在这里并不重要。

IMO,您应该使用 WriteCmd 所需的数据创建一个新数组,如下所示:

{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}

它应该将 2 (0x2) 个字节(0x41 和 0x41)写入由fileList[0].

编辑:更新偏移量,顺序是 LSB 到 MSB。

于 2012-12-15T01:10:40.390 回答