我是智能卡技术的新手。我想从智能卡读取文件。我正在使用javax.smartcardio来读取值。我开发了一些代码来连接系统和卡(这很好)。我也有卡ATR和其他细节。但没有得到适当的APDU命令帮助与智能卡通信。卡在APDU命令中。
答案 0 :(得分:3)
首先:
"不"所有Java卡内都有MF
,DF
和EF
!这些单词依次代表主文件,专用文件和基本文件。它们是ISO7816定义的智能卡系统文件(refer to part 4 of ISO7816)的组件,因此您的卡可能或可能没有此文件系统。
典型的Java卡有一个存储空间,您可以在其中安装您的applet(在成功验证成功之后)并注册您的applet的名称(我们称之为AID,代表 Applet IDentifier 和卡片的注册表中是一个5到16字节的十六进制序列(加载/安装的applet和包的表,其中包含生命周期和特权 - read Global Platform Card Spec)。
然后:
假设您的读卡器中插有智能卡,该智能卡已连接到您的计算机。您可以使用不同的选项在计算机和卡之间进行通信。
1 - 您可以使用可用的工具,例如您的阅读器工具(几乎所有阅读器都有一个工具),PyAPDUTool等。
2 - 您可以使用Javax.smartcardio
库编写Java程序以与智能卡通信:
import java.util.List;
import java.util.Scanner;
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;
import javax.xml.bind.DatatypeConverter;
public class TestPCSC {
public static void main(String[] args) throws CardException {
TerminalFactory tf = TerminalFactory.getDefault();
List< CardTerminal> terminals = tf.terminals().list();
System.out.println("Available Readers:");
System.out.println(terminals + "\n");
Scanner scanner = new Scanner(System.in);
System.out.print("Which reader do you want to send your commands to? (0 or 1 or ...): ");
String input = scanner.nextLine();
int readerNum = Integer.parseInt(input);
CardTerminal cardTerminal = (CardTerminal) terminals.get(readerNum);
Card connection = cardTerminal.connect("DIRECT");
CardChannel cardChannel = connection.getBasicChannel();
System.out.println("Write your commands in Hex form, without '0x' or Space charaters.");
System.out.println("\n---------------------------------------------------");
System.out.println("Pseudo-APDU Mode:");
System.out.println("---------------------------------------------------");
while (true) {
System.out.println("Pseudo-APDU command: (Enter 0 to send APDU command)");
String cmd = scanner.nextLine();
if (cmd.equals("0")) {
break;
}
System.out.println("Command : " + cmd);
byte[] cmdArray = hexStringToByteArray(cmd);
byte[] resp = connection.transmitControlCommand(CONTROL_CODE(), cmdArray);
String hex = DatatypeConverter.printHexBinary(resp);
System.out.println("Response : " + hex + "\n");
}
System.out.println("\n---------------------------------------------------");
System.out.println("APDU Mode:");
System.out.println("---------------------------------------------------");
while (true) {
System.out.println("APDU command: (Enter 0 to exit)");
String cmd = scanner.nextLine();
if (cmd.equals("0")) {
break;
}
System.out.println("Command : " + cmd);
byte[] cmdArray = hexStringToByteArray(cmd);
ResponseAPDU resp = cardChannel.transmit(new CommandAPDU(cmdArray));
byte[] respB = resp.getBytes();
String hex = DatatypeConverter.printHexBinary(respB);
System.out.println("Response : " + hex + "\n");
}
connection.disconnect(true);
}
public static int CONTROL_CODE() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("windows") > -1) {
/* Value used by both MS' CCID driver and SpringCard's CCID driver */
return (0x31 << 16 | 3500 << 2);
} else {
/* Value used by PCSC-Lite */
return 0x42000000 + 1;
}
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}
3-您可以使用PySCard
库编写Python程序以与智能卡通信:
#Importing required modules.
import sys
import time
#--- You may need to change the following "line" based on your pyScard library installation path
sys.path.append("D:\\PythonX\\Lib\\site-packages")
from smartcard.scard import *
import smartcard.util
from smartcard.System import readers
#---This is the list of commands that we want to send device
cmds =[[,0xFF,0x69,0x44,0x42,0x05,0x68,0x92,0x00,0x04,0x00],]
#--- Let's to make a connection to the card reader
r=readers()
print "Available Readers :",r
print
target_reader = input("--- Select Reader (0, 1 , ...): ")
print
while(True):
try:
print "Using :",r[target_reader]
reader = r[target_reader]
connection=reader.createConnection()
connection.connect()
break
except:
print "--- Exception occured! (Wrong reader or No card present)"
ans = raw_input("--- Try again? (0:Exit/1:Again/2:Change Reader)")
if int(ans)==0:
exit()
elif int(ans)==2:
target_reader = input("Select Reader (0, 1 , ...): ")
#--- An struct for APDU responses consist of Data, SW1 and SW2
class stru:
def __init__(self):
self.data = list()
self.sw1 = 0
self.sw2 = 0
resp = stru()
def send(cmds):
for cmd in cmds:
#--- Following 5 line added to have a good format of command in the output.
temp = stru() ;
temp.data[:]=cmd[:]
temp.sw1=12
temp.sw2=32
modifyFormat(temp)
print "req: ", temp.data
resp.data,resp.sw1,resp.sw2 = connection.transmit(cmd)
modifyFormat(resp)
printResponse(resp)
def modifyFormat(resp):
resp.sw1=hex(resp.sw1)
resp.sw2=hex(resp.sw2)
if (len(resp.sw2)<4):
resp.sw2=resp.sw2[0:2]+'0'+resp.sw2[2]
for i in range(0,len(resp.data)):
resp.data[i]=hex(resp.data[i])
if (len(resp.data[i])<4):
resp.data[i]=resp.data[i][0:2]+'0'+resp.data[i][2]
def printResponse(resp):
print "res: ", resp.data,resp.sw1,resp.sw2
send(cmds)
connection.disconnect()
4-您可以使用WinSCard
库以C ++ / .Net(不确定)编写程序,以便与智能卡通信。
以上程序是将APDU命令发送到智能卡的示例程序。但是它们的命令取决于你的卡和你的[你]安装的小程序。
例如,假设您编写的AID = 01 02 03 04 05 00
的applet在收到11 22 33 44 55
作为APDU命令时返回00 00 00 00 00
。您需要做什么才能收到此回复(即11 22 33 44 55
),如下所示:
00 00 00 00 00
发送到您的小程序。 如果您的卡已经实现了ISO7816系统文件,则需要文件ID才能选择它们。但命令本身在ISO7816-P4中定义。
即使您的卡没有实现ISO7816系统文件,您也可以编写一个小程序,就像ISO7816-P系统文件实现的智能卡一样(不管怎样都不容易)。
由于MF的ID始终为3F00
,因此尝试选择此文件时,系统会显示您的卡是否实现了系统文件。
通常,当您的卡开机时,名为Card Manager的卡中的强制实体会收到您的APDU命令。通过使用SELECT APDU命令,您请求卡管理器将下一个传入命令发送到所选的APPLET
答案 1 :(得分:1)
市场上有各种各样的智能卡,每种都使用不同的结构来保存数据。文件结构在I SO 7816-4:组织,安全性和交换命令
中定义卡片人员实体的选择是否使用这种文件结构来保持芯片内的数据。印度标准SCOSTA完全符合ISO 7816标准,这意味着任何SCOSTA complient产品都将使用ISO 7816 -4(MF,DF,EF)中定义的结构。
在发送任何命令之前,您应该知道卡的结构。喜欢Select MF - &gt;选择DF - &gt;选择EF - &gt;读取记录命令。
对于java卡(智能卡),并不总是创建文件结构来保存数据,它可以使用数组[即持久存储器]并在操作期间为其赋值。此值驻留在智能卡生命周期中。想要获取值只是发送一个适当的定义命令,卡将返回数组中的值。就这些。
这意味着我们可以说所有智能卡都不遵循文件结构规则
要阅读任何智能卡,都有一些预先定义的规则,如果卡是结构明智的,我们应该知道结构,否则我们可以获得Error status word。
如果谈论命令,也可以在此处阅读Here -Smart Card Command Rules,以便增强您对智能卡命令的了解,以便您可以发送正确的命令。
But not getting proper help for APDU command for communication with Smart card. Stuck in APDU command.
在向您发送任何命令之前,您应该了解有关您正在使用的智能卡的更多信息。分享命令响应以检查确切问题是很好的。 javax.smartcardio是与智能卡通信的非常好的API,还有任何可以帮助您编写代码以访问任何智能卡的shared示例。
希望在了解您正在使用的智能卡的深层细节后,一旦您构建了适当的命令,您将不会收到任何错误。希望能帮助到你。