gpt4 book ai didi

Java 智能卡 - 读取 Scosta 智能卡

转载 作者:行者123 更新时间:2023-11-29 21:13:04 27 4
gpt4 key购买 nike

我正在尝试通过 java smartcardio 读取印度政府标准的“Scosta”智能卡我使用的代码是

package com.example.smartcardreader;

import java.util.List;

import javax.smartcardio.ATR;
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 SmartCardReader {

public static void main(String[] args) {

try{

// show the list of available terminals
TerminalFactory factory = TerminalFactory.getDefault();

List<CardTerminal> terminals = factory.terminals().list();

System.out.println("Terminals: " + terminals);

// get the first terminal
CardTerminal terminal = terminals.get(0);

// establish a connection with the card
Card card = terminal.connect("*");
System.out.println("card: " + card);

// get the ATR
ATR atr = card.getATR();
byte[] baAtr = atr.getBytes();

System.out.print("ATR = 0x");
for(int i = 0; i < baAtr.length; i++ ){
System.out.printf("%02X ",baAtr[i]);
}

CardChannel channel = card.getBasicChannel();
byte[] cmdApduGetCardUid = new byte[]{
(byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00};

ResponseAPDU respApdu = channel.transmit(
new CommandAPDU(cmdApduGetCardUid));

if(respApdu.getSW1() == 0x90 && respApdu.getSW2() == 0x00){

byte[] baCardUid = respApdu.getData();

System.out.print("Card UID = 0x");
for(int i = 0; i < baCardUid.length; i++ ){
System.out.printf("%02X ", baCardUid [i]);
}
}

card.disconnect(false);

} catch (CardException e) {
e.printStackTrace();
}
}

我在 Mac 机器上使用 eclipse IDE 进行开发。当我运行这段代码时,它给了我异常,因为它无法读取终端。我有一个 USB 读卡器,并且还插入了智能卡。你能指出我到底哪里错了吗?提前致谢。

最佳答案

这可能与您的问题无关,但包 javax.smartcardio 似乎在使用 64 位版本的 java7 的 Mac OS X 上严重损坏。您可以在此 blog post 中找到更多信息还有这个bug report .也可以看看开源项目jnasmartcardio试图解决 javax.smartcardio 包的问题。

关于Java 智能卡 - 读取 Scosta 智能卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22191549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com