java如何从奥地利ecard将ResponseADU解码为XML?
我试着从一张奥地利电子贺卡上读取信息,以获得名字和姓氏
目前有效的方法是:访问卡,发送APDU命令,并以字节数组的形式获取信息
如何将接收到的字节数组转换为XML以提取所需的数据
以下是代码:
import java.util.List;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
public class Main2 {
public static void main(String[] args) {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals;
try {
terminals = factory.terminals().list();
CardTerminal terminal = terminals.get(0);
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
// Select the MF
byte[] aid = { (byte) 0xD0, 0x40, 0x00, 0x00, 0x17, 0x01, 0x01, 0x01 };
ResponseAPDU resp = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, aid));
System.out.println("Response: " + resp.toString());
// Select the Personaladata-file
byte[] aid2 = { (byte) 0xEF, 0x01 };
resp = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x02, 0x04, aid2));
System.out.println("Response: " + resp.toString());
// Get the data from the file
resp = channel.transmit(new CommandAPDU(0x00, 0xB0, 0x00, 0x00, 0xFF));
System.out.println("Response: " + resp.toString());
System.out.println("Response String: " + new String(resp.getData()));
card.disconnect(false);
} catch (CardException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
# 1 楼答案
感谢您的提示,下面是将DER字节数组解码为字符串的代码
# 2 楼答案
我不知道如何将数据转换成XML结构(以及根据什么模式)。然而,我从SV卡收到的字节数组看起来像ASN。1 DER编码的TLV结构:
所以这似乎遵循了下面的ASN。1表示法:
其中,给定名称和姓氏的属性为
因此,为了获得给定的名称和姓氏,需要解析TLV结构,搜索这两个元素的OID,并将相关值解码为UTF8字符串
请注意,简单地假设字段位于准确的位置似乎不是一个好主意。例如,在名字字段前面有一个字段
30 xx ...
(类型为Attribute
的字段),似乎只有在卡片上打印了学术/专业头衔(如“Dr.”在我的情况下)时才会出现。同样,学术后缀还有另一个可选字段(如“M.Sc.”),只有在卡片上打印了这样的后缀时才会出现。虽然我的卡片上所有其他字段的顺序都是一样的,但我不确定这是否是必需的