I have to convert one line from ruby to Qt C++ - ruby

I need help from some one to convert the following line of code from ruby to qt c++:
KEY = %w(30 81 9F 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 03 81 8D 00).map{|s| s.hex}.pack('C*')

Here's what the Ruby code does:
Create an array of strings, ["30", "81", "9F", ... "8D", "00" ].
Convert each hexadecimal string in the array to an integer: [48, 129, 159, ... 141, 0]
Use Array#pack to turn the array into a single binary string of unsigned 8-bit integers.

If a simple C array will do:
unsigned char KEY[22] = { 0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06... };
If you need a QByteArray:
QByteArray KEY("\x30\x81\x9F\x30\x0D\x06...", 22);

QByteArray key = QByteArray::fromHex(QByteArray("30 81 9F 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 03 81 8D 00").replace(' ', QString()));
Quick explanation:
QByteArray("30 81 9F 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 03 81 8D 00")
creates temporary QByteArray with your key as string
.replace(' ', QString())
removes all spaces in temporary QByteArray, so it contains only characters 0..F
QByteArray::fromHex()
converts hex-encoded QByteArray into QByteArray containing unsigned 8-bit integers (i.e. C++ char type). It means that it takes every pair of hex digits from original QByteArray ( for example "41") and converts it to integer ("41" will be converted to 65 = 4*16 + 1) and appends this value into new QByteArray.
If you need key as a "const char *" you can use QByteArray::constData() method, but you have to remember that the pointer returned by this method is valid only as long as original QByteArray is valid (quote from documentation: "as long as the byte array isn't reallocated or destroyed"). So if you need to store the key data, keep it as QByteArray or make a copy of const char * returned by constData().

char KEY[] = {
48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0
};
The result is something slightly different, a flat array of byte values. It would certainly be possible to make an array of 1-character-string object references, but it would not be likely to be useful.

Related

Protobuf: ParseFromString method gives error "raise message_mod.DecodeError('Field number 0 is illegal.')" for message with oneof field

I have a kafka topic that has protobuf message of format:
message CreditTransaction {
string date = 1;
float amount = 2;
}
message DebitTransaction {
string date = 1;
float amount = 2;
}
...
.. # other message definitions
message TransactionEvent {
oneof event {
CreditTransaction credit = 1;
DebitTransaction debit = 2;
Trade trade = 3;
....
..# other fields
}
};
Using pyspark-streaming, when I am trying to use ParseFromString method to parse it, its giving me error:
File "./google.zip/google/protobuf/message.py", line 202, in ParseFromString
return self.MergeFromString(serialized)
File "./google.zip/google/protobuf/internal/python_message.py", line 1128, in MergeFromString
if self._InternalParse(serialized, 0, length) != length:
File "./google.zip/google/protobuf/internal/python_message.py", line 1178, in InternalParse
raise message_mod.DecodeError('Field number 0 is illegal.')
google.protobuf.message.DecodeError: Field number 0 is illegal.
Is it because the message TransactionEvent has only a single field and that too oneof type ?
I tried to add a dummy int64 id field also
message TransactionEvent {
int64 id = 1;
oneof event {
CreditTransaction credit = 2;
DebitTransaction debit = 3;
Trade trade = 4;
....
..# other fields
}
};
but still the same error.
Code I am using:
def parse_protobuf_from_bytes(msg_bytes):
msg = schema_pb2.MarketDataEvent()
msg.ParseFromString(msg_bytes)
eventStr = msg.WhichOneof("event")
if eventStr=="credit":
# some code
elif eventStr=="debit":
# some code
return str(concatenatedFieldsValue)
parse_protobuf = udf(lambda x: parse_protobuf_from_bytes(x), StringType())
kafka_conf = {
"kafka.bootstrap.servers": "kafka.broker.com:9092",
"checkpointLocation": "/user/aiman/checkpoint/kafka_local/transactions",
"subscribe": "TRANSACTIONS",
"startingOffsets": "earliest",
"enable.auto.commit": False,
"value.deserializer": "ByteArrayDeserializer",
"group.id": "my-group"
}
df = spark.readStream \
.format("kafka") \
.options(**kafka_conf) \
.load()
data = df.selectExpr("offset","CAST(key AS STRING)", "value") \
.withColumn("event", parse_protobuf(col("value")))
df2 = data.select(col("offset"),col("event"))
If I am just printing the bytes without parsing, I am getting this:
-------------------------------------------
Batch: 0
-------------------------------------------
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|offset |event |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|7777777|bytearray(b'\x00\x00\x00\x00\xc2\x02\x0e\x1a]\n\x11RIOT_230120P35.00\x10\x80\xa6\xae\x82\xd9\xed\xf1\xfe\x16\x18\xcd\xd9\xd9\x82\xd9\xed\xf1\xfe\x16 \xe2\xf7\xd9\x82\xd9\xed\xf1\xfe\x16(\x95\xa2\xed\xff\xd9\xed\xf1\xfe\x160\x8c\xaa\xed\xff\xd9\xed\xf1\xfe\x168\x80\xd1\xb6\xc1\x0b#\xc0\x8d\xa3\xba\x0bH\x19P\x04Z\x02Q_b\x02A_') |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
The raw data you are trying to decode is:
b'\x00\x00\x00\x00\xc2\x02\x0e\x1a]\n\x11RIOT_230120P35.00\x10\x80\xa6\xae\x82\xd9\xed\xf1\xfe\x16\x18\xcd\xd9\xd9\x82\xd9\xed\xf1\xfe\x16 \xe2\xf7\xd9\x82\xd9\xed\xf1\xfe\x16(\x95\xa2\xed\xff\xd9\xed\xf1\xfe\x160\x8c\xaa\xed\xff\xd9\xed\xf1\xfe\x168\x80\xd1\xb6\xc1\x0b#\xc0\x8d\xa3\xba\x0bH\x19P\x04Z\x02Q_b\x02A_'
A valid protobuf message never starts with 0x00, because the field number 0 is reserved. The message appears to have some extra data in the beginning.
Comparing with the protobuf encoding specification, we can try to make sense of this.
Starting from the string RIOT_230120P35.00, it is correctly prefixed by length 0x11 (17 characters). The previous byte is 0x0A, which is tag for field 1 with type string, such as in CreditTransaction message. Reading the message backwards from there, everything looks reasonable up to 0x1A byte.
After stripping the first 7 bytes and converting to hex (1a 5d 0a 11 52 49 4f 54 5f 32 33 30 31 32 30 50 33 35 2e 30 30 10 80 a6 ae 82 d9 ed f1 fe 16 18 cd d9 d9 82 d9 ed f1 fe 16 20 e2 f7 d9 82 d9 ed f1 fe 16 28 95 a2 ed ff d9 ed f1 fe 16 30 8c aa ed ff d9 ed f1 fe 16 38 80 d1 b6 c1 0b 40 c0 8d a3 ba 0b 48 19 50 04 5a 02 51 5f 62 02 41 5f), the message is accepted by online protobuf decoder.
It seems the message has 7 extra bytes in the beginning for some reason. These bytes do not conform to the protobuf format and their meaning cannot be determined without some information from the developer of the other endpoint of the communication.

MD5 implementation in Ruby

I am trying to implement MD5 in Ruby, following the pseudo code written in the wiki.
Here is the codes, not working well:
# : All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
# s specifies the per-round shift amounts
s = []
s.concat([7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22])
s.concat([5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20])
s.concat([4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23])
s.concat([6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21])
# Use binary integer part of the sines of integers (Radians) as constants:
k = 0.upto(63).map do |i|
(Math.sin(i+1).abs * 2 ** 32).floor
end
# Initialize variables:
a0 = 0x67452301 # A
b0 = 0xefcdab89 # B
c0 = 0x98badcfe # C
d0 = 0x10325476 # D
message = File.read(ARGV[0])
# Pre-processing
# with bit stream "string" (MSB)
bits = message.unpack('B*')[0]
org_len = bits.size
bits << '1' # adding a single 1 bit
bits << '0' while !((bits.size + 64) % 512 == 0) # padding with zeros
bits << (org_len % 2 ** 64).to_s(2).rjust(64, '0')
message32 = [bits].pack('B*').unpack('V*') # ?
# 1. bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')} => for test
# 2. [bits].pack('B*').unpack('N*') == bits.scan(/(.{32})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(8, '0').to_i(16) } => true
# custom operations for wrapping the results as modulo 2 ** 32
class Integer
def r
self & 0xFFFFFFFF
end
def rotate_l(count)
(self << count).r | (self >> (32 - count))
end
end
# Process the message in successive 512-bit chunks:
message32.each_slice(16).each do |m|
a = a0
b = b0
c = c0
d = d0
0.upto(63) do |i|
if i < 16
f = d ^ (b & (c ^ d))
g = i
elsif i < 32
f = c ^ (d & (b ^ c))
g = (5 * i + 1) % 16
elsif i < 48
f = b ^ c ^ d
g = (3 * i + 5) % 16
elsif i < 64
f = c ^ (b | ~d)
g = (7 * i) % 16
end
f = (f + a + k[i] + m[g]).r
a = d
d = c
c = b
b = (b + f.rotate_l(s[i])).r
end
a0 = (a0 + a).r
b0 = (b0 + b).r
c0 = (c0 + c).r
d0 = (d0 + d).r
end
puts [a0, b0, c0, d0].pack('V*').unpack('H*')
I'm testing with messages, well known for collision in just one block:
Message 1
Message 2
They are resulted in the same value, but not correct:
❯ ruby md5.rb message1.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ ruby md5.rb message2.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ md5 message*
MD5 (/Users/hansuk/Downloads/message1.bin) = 008ee33a9d58b51cfeb425b0959121c9
MD5 (/Users/hansuk/Downloads/message2.bin) = 008ee33a9d58b51cfeb425b0959121c9
I have an uncertainty about pre-processing steps.
I checked the bit stream after pre-processing, with the comments on the line 34 and 35, the original message written in same and the padding bits are right:
❯ hexdump message1.bin
0000000 4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
0000010 d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
0000020 af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
0000030 93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
0000040
(byebug) bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')}
4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00
nil
What am I missed?
The one most classical mistake in implementing MD5 is botching endianness: the padded message and the message length are to be turned to 32-bit words per little-endian convention, so that message 'abc' in ASCII (0x61 0x62 0x63) is turned to a 16-word padded message block with m[0]=0x80636261, m[14]=0x18, and m[1…13,15]=0.
I never wrote anything in Ruby, but I get the feeling the code yields m[0]=0x61626380, m[15]=0x18, and m[1…14]=0.
Also: the 4-word result is to be turned to 16 bytes per little-endian convention too.

How to explain embedded message binary wire format of protocol buffer?

I'm trying to understand protocol buffer encoding method, when translating message to binary(or hexadecimal) format, I can't understand how the embedded message is encoded.
I guess maybe it's related to memory address, but I can't find the accurate relationship.
Here is what i've done.
Step 1: I defined two messages in test.proto file,
syntax = "proto3";
package proto_test;
message Education {
string college = 1;
}
message Person {
int32 age = 1;
string name = 2;
Education edu = 3;
}
Step 2: And then I generated some go code,
protoc --go_out=. test.proto
Step 3: Then I check the encoded format of the message,
p := proto_test.Person{
Age: 666,
Name: "Tom",
Edu: &proto_test.Education{
College: "SOMEWHERE",
},
}
var b []byte
out, err := p.XXX_Marshal(b, true)
if err != nil {
log.Fatalln("fail to marshal with error: ", err)
}
fmt.Printf("hexadecimal format:% x \n", out)
fmt.Printf("binary format:% b \n", out)
which outputs,
hexadecimal format:08 9a 05 12 03 54 6f 6d 1a fd 96 d1 08 0a 09 53 4f 4d 45 57 48 45 52 45
binary format:[ 1000 10011010 101 10010 11 1010100 1101111 1101101 11010 11111101 10010110 11010001 1000 1010 1001 1010011 1001111 1001101 1000101 1010111 1001000 1000101 1010010 1000101]
what I understand is ,
08 - int32 wire type with tag number 1
9a 05 - Varints for 666
12 - string wire type with tag number 2
03 - length delimited which is 3 byte
54 6f 6d - ascii for "TOM"
1a - embedded message wire type with tag number 3
fd 96 d1 08 - ? (here is what I don't understand)
0a - string wire type with tag number 1
09 - length delimited which is 9 byte
53 4f 4d 45 57 48 45 52 45 - ascii for "SOMEWHERE"
What does fd 96 d1 08 stands for?
It seems like that d1 08 always be there, but fd 96 sometimes change, don't know why. Thanks for answering :)
Add
I debugged the marshal process and reported a bug here.
At that location I/you would expect the number of bytes in the embedded message.
I have repeated your experiment in Python.
msg = Person()
msg.age = 666
msg.name = "Tom"
msg.edu.college = "SOMEWHERE"
I got a different result, the one I would expect. A varint stating the size of the embedded message.
0x08
0x9A, 0x05
0x12
0x03
0x54 0x6F 0x6D
0x1A
0x0B <- Equals to 11 decimal.
0x0A
0x09
0x53 0x4F 0x4D 0x45 0x57 0x48 0x45 0x52 0x45
Next I deserialized your bytes:
msg2 = Person()
str = bytearray(b'\x08\x9a\x05\x12\x03\x54\x6f\x6d\x1a\xfd\x96\xd1\x08\x0a\x09\x53\x4f\x4d\x45\x57\x48\x45\x52\x45')
msg2.ParseFromString(str)
print(msg2)
The result of this is perfect:
age: 666
name: "Tom"
edu {
college: "SOMEWHERE"
}
The conclusion I come to is that there are some different ways of encoding in Protobuf. I do not know what is done in this case but I know the example of a negative 32 bit varint. A positive varint is encoded in five bytes, a negative value is cast as a 64 bit value and encoded to ten bytes.

How To Serialize A Modified Go Packet Packet Into Real IP Packet

Why
I want to write a proxy server, proxy server changes IP/port of a packet and emits modified.
Attempt
package main
import (
"encoding/hex"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"fmt"
"net"
)
func main() {
packetData := []byte{
69, 0, 0, 63, 64, 237, 64, 0, 64, 6, 74, 221, 192,
168, 1, 90, 52, 85, 184, 151, 141, 230, 0, 80,
174, 147, 86, 192, 18, 107, 243, 149, 128, 24,
0, 229, 92, 65, 0, 0, 1, 1, 8, 10, 22, 138, 85, 109,
48, 16, 32, 253, 49, 50, 51, 52, 53, 54, 55, 56,
57, 48, 10,
}
fmt.Println("Hex dump of real IP packet taken as input:\n")
fmt.Println(hex.Dump(packetData))
packet := gopacket.NewPacket(packetData, layers.LayerTypeIPv4, gopacket.Default)
if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
ip := ipLayer.(*layers.IPv4)
dst := ip.DstIP.String()
src := ip.SrcIP.String()
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
tcp := tcpLayer.(*layers.TCP)
dst = fmt.Sprintf("%s:%d", dst, tcp.DstPort)
src = fmt.Sprintf("%s:%d", src, tcp.SrcPort)
fmt.Printf("From %s to %s\n\n", src, dst)
ip.DstIP = net.ParseIP("8.8.8.8")
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
newBuffer := gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(newBuffer, options,
ip,
tcp,
)
outgoingPacket := newBuffer.Bytes()
fmt.Println("Hex dump of go packet serialization output:\n")
fmt.Println(hex.Dump(outgoingPacket))
}
}
}
Output
Hex dump of real IP packet taken as input:
00000000 45 00 00 3f 40 ed 40 00 40 06 4a dd c0 a8 01 5a |E..?#.#.#.J....Z|
00000010 34 55 b8 97 8d e6 00 50 ae 93 56 c0 12 6b f3 95 |4U.....P..V..k..|
00000020 80 18 00 e5 5c 41 00 00 01 01 08 0a 16 8a 55 6d |....\A........Um|
00000030 30 10 20 fd 31 32 33 34 35 36 37 38 39 30 0a |0. .1234567890.|
From 192.168.1.90:36326 to 52.85.184.151:80
Hex dump of go packet serialization output:
00000000 8d e6 00 50 ae 93 56 c0 12 6b f3 95 80 18 00 e5 |...P..V..k......|
00000010 00 00 00 00 01 01 08 0a 16 8a 55 6d 30 10 20 fd |..........Um0. .|
What's Wrong
Second hex dump must start with 45 (most IPv4 packets start with 45, where 4 is a version of the protocol). Second hexdump must be identical to the first in many details except one IP address that was changed, TCP checksum and size values. Second hex dump must contain payload 1234567890\n.
Similar Questions
How to use Golang to compose raw TCP packet (using gopacket) and send it via raw socket
Sending UDP packets with gopacket
Changing the IP address of a Gopacket and re-transmitting using raw sockets
First, always check returned errors — it's your feedback:
err := gopacket.SerializePacket(newBuffer, options, packet)
// OR
err := gopacket.SerializeLayers(newBuffer, options,
ip,
tcp,
)
// THEN
if err != nil {
panic(err)
}
From code above you will get: TCP/IP layer 4 checksum cannot be computed without network layer... call SetNetworkLayerForChecksum to set which layer to use
Then, the solution is to use tcp.SetNetworkLayerForChecksum(ip) and the final working code:
package main
import (
"encoding/hex"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"fmt"
"net"
)
func main() {
packetData := []byte{
69, 0, 0, 63, 64, 237, 64, 0, 64, 6, 74, 221, 192,
168, 1, 90, 52, 85, 184, 151, 141, 230, 0, 80,
174, 147, 86, 192, 18, 107, 243, 149, 128, 24,
0, 229, 92, 65, 0, 0, 1, 1, 8, 10, 22, 138, 85, 109,
48, 16, 32, 253, 49, 50, 51, 52, 53, 54, 55, 56,
57, 48, 10,
}
fmt.Println("Hex dump of real IP packet taken as input:\n")
fmt.Println(hex.Dump(packetData))
packet := gopacket.NewPacket(packetData, layers.LayerTypeIPv4, gopacket.Default)
if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
ip := ipLayer.(*layers.IPv4)
dst := ip.DstIP.String()
src := ip.SrcIP.String()
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
tcp := tcpLayer.(*layers.TCP)
dst = fmt.Sprintf("%s:%d", dst, tcp.DstPort)
src = fmt.Sprintf("%s:%d", src, tcp.SrcPort)
fmt.Printf("From %s to %s\n\n", src, dst)
ip.DstIP = net.ParseIP("8.8.8.8")
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
tcp.SetNetworkLayerForChecksum(ip)
newBuffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializePacket(newBuffer, options, packet)
if err != nil {
panic(err)
}
outgoingPacket := newBuffer.Bytes()
fmt.Println("Hex dump of go packet serialization output:\n")
fmt.Println(hex.Dump(outgoingPacket))
}
}
}
Output
Hex dump of real IP packet taken as input:
00000000 45 00 00 3f 40 ed 40 00 40 06 4a dd c0 a8 01 5a |E..?#.#.#.J....Z|
00000010 34 55 b8 97 8d e6 00 50 ae 93 56 c0 12 6b f3 95 |4U.....P..V..k..|
00000020 80 18 00 e5 5c 41 00 00 01 01 08 0a 16 8a 55 6d |....\A........Um|
00000030 30 10 20 fd 31 32 33 34 35 36 37 38 39 30 0a |0. .1234567890.|
From 192.168.1.90:36326 to 52.85.184.151:80
Hex dump of go packet serialization output:
00000000 45 00 00 3f 40 ed 40 00 40 06 27 ba c0 a8 01 5a |E..?#.#.#.'....Z|
00000010 08 08 08 08 8d e6 00 50 ae 93 56 c0 12 6b f3 95 |.......P..V..k..|
00000020 80 18 00 e5 39 1e 00 00 01 01 08 0a 16 8a 55 6d |....9.........Um|
00000030 30 10 20 fd 31 32 33 34 35 36 37 38 39 30 0a |0. .1234567890.|
As you see 08 08 08 08 is a new IP and payload 1234567890\n is also preserved, IP packet starts with 45 as usual.

How to find AID number of my local transportation card?

I have a transportation card for urban transportation. I need to know what aid(application identifier) number of the card is. According to EMV Book 1, i have to use the List of AIDs method (page 141). But how?
I also have a ACR122U card reader. I can send an APDU command to the card. All i need is the AID of the card. In addition, i always get SW=6A82 error. It means "File Not Found". I suppose, i need to know true AID number to solve this problem. I want to see SW=9000 (successful) response...
Edit: Code for creating select apdu command
private static final byte[] CLA_INS_P1_P2 = { 0x00, (byte)0xA4, 0x04, 0x00 };
private static final byte[] AID_ANDROID = { (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
private byte[] createSelectAidApdu(byte[] aid) {
byte[] result = new byte[6 + aid.length];
System.arraycopy(CLA_INS_P1_P2, 0, result, 0, CLA_INS_P1_P2.length);
result[4] = (byte)aid.length;
System.arraycopy(aid, 0, result, 5, aid.length);
result[result.length - 1] = 0;
return result;
}
Thanks..
Typically, you should lookup the card documentation, which should describe how files organized.
However, since you're reading an ISO-DEP card, you may refer to ISO/IEC CD 7816-4. The card should implement part of instructions in this standard. According to Section 5.2, the file can be chosen using its identifier, which means you are able to enumerate all files located in MF.
So a possible solution is:
Send select file by identifier instruction as
00 A4 00 00 02 id 00
Where id ranges from 0000 to FFFF.
Once you receive SW=9000, the response should contain file control information (FCI, see Section 5.6). You can then find the DF name after byte 84. For example, a card responds
6F 15 84 0D 4E 43 2E 65 43 61 72 64 2E 44 46 30 31 A5 04 9F 08 01 02 90 00,
the DF name is 4E 43 2E 65 43 61 72 64 2E 44 46 30 31. The byte 0D after 84 indicates the length of DF name is 0x0D. You may use it as AID.

Resources