Need help to write apdu to file

Help
2009-02-23
2013-04-24
  • Hello,
    i'm writing an littel script with pscard to read the european drivercard.

    i can read the hole card, but i cant write it to an file.
    the response of an apdu is un tuple, when i understand it right ? when i transform it with toHexbin ta an Hex code i got an string like "01 4C 41 42 4F 20 42 65 72 6C 69 6E 20" an this string i want to write as binary to an file.

    When i open an file with   

    z = toHexString(response)
        datei = open("test.ddd", "wb")
        datei.write(z)
        datei.close()
    i got exact the string obove in the file. what's wrong ?

    Sorry i'm only an Hobby coder and my english is verry rusty :)

    Stefan

     
    • Hi Nobody!

      the APDUs are treated as lists of bytes. When sending APDUs with connection.transmit(), the transmit parameter is a list of bytes and the returned value is a tuple with three elements, the response as a list of bytes, and the status words sw1 and sw2 as bytes.

      As I understand, you want to store the APDUs inside a file, maybe to read them back to replay a script.

      Here is a code snippet:

      ----------------------------
      from smartcard.util import toHexString, toBytes

      # define the apdus used in this script
      SAMPLE_APDU = [0XA0, 0XC0, 00, 00, 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 ]

      # write APDU to file
      f = open( "test.dat", "w" )
      f.write( toHexString( SAMPLE_APDU ) )
      f.close()

      # read APDU from file
      f = open( "test.dat", "r" )
      APDU_STRING = f.readline()
      APDU = toBytes( APDU_STRING )
      print APDU
      ----------------------------

      alternatively, you can use python pickle module to do so:

      ----------------------------
      import pickle

      # define the apdus used in this script
      SAMPLE_APDU = [0XA0, 0XC0, 00, 00, 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 ]

      # write APDU to file
      output = open('test.dat', 'wb')
      pickle.dump( SAMPLE_APDU, output )
      output.close()

      # read APDU from file
      pkl_file = open('test.dat', 'rb')
      APDU = pickle.load(pkl_file)
      print APDU
      ----------------------------

      hope this helps

      best regards,

      -jda