如何从HID omnikey获取智能卡的ATTR

时间:2014-12-31 07:19:05

标签: python-2.7 smartcard-reader contactless-smartcard pyscard

我想获得智能卡的ATR。我正在使用HID omnikey 5321.我正在关注此链接" http://pyscard.sourceforge.net/user-guide.html#requesting-any-card"

到目前为止,我已经尝试过:

>>>from smartcard.CardType import AnyCardType
>>>from smartcard.CardRequest import CardRequest
>>>from smartcard.util import toHexString
>>>
>>> cardtype = AnyCardType()
>>> cardrequest = CardRequest( timeout=1, cardType=cardtype )
>>> cardservice = cardrequest.waitforcard() 
>>>
>>>>>> cardservice.connection.connect()

我在

收到错误
cardservice.connection.connect() 

错误如:

raise CardConnectionException('Unable to connect with ptotocol[pcscprotocol] + . '+ScardGetErrorMessage(hresult) 
CardConnectionException: Unable to conenct the card with T0 or T1 . Card is not responding to reset.

2 个答案:

答案 0 :(得分:1)

在python中,你可以使用pyscard库与智能卡进行交互,有一个例子可以帮助你在http://pyscard.sourceforge.net/pyscard-framework.html#framework-samples显示ATR

答案 1 :(得分:0)

因为您没有指定阅读器进行连接:

r=readers()
#r[Number of reader list].
cardservice.connection = r[0].createConnection()
cardservice.connection.connect()

一个简单的例子:

from __future__ import print_function
from smartcard.Exceptions import NoCardException
from smartcard.System import readers
from smartcard.util import toHexString

for reader in readers():
    try:
         connection = reader.createConnection()
         connection.connect()
         print(reader, toHexString(connection.getATR()))
    except NoCardException:
         print(reader, 'no card inserted')

import sys
if 'win32' == sys.platform:
    print('press Enter to continue')
    sys.stdin.read(1)

-另一个选择阅读器:

from __future__ import print_function
from smartcard.Exceptions import NoCardException
from smartcard.System import readers
from smartcard.util import toHexString
from smartcard.CardType import AnyCardType
from smartcard.CardRequest import CardRequest

cardtype = AnyCardType()
r=readers()
cardrequest = CardRequest( timeout=10, cardType=cardtype )
cardservice = cardrequest.waitforcard()
print('Available Readers:')
for i in range(len(readers())):
    print('[',i+1,']',r[i])
if(len(readers()) < 1):
    print("\nNO AVAILABLE READERS!\n")
else:
    print("Select you Reader: (Ctrl+C to Exit)")
    my_input = input()
    selectReader = clamp(int(my_input)-1,0,len(readers()))
    print('Selected: ',r[selectReader])
    cardservice.connection = r[selectReader].createConnection()
    cardservice.connection.connect()
    try:
        print('Card ATR:',toHexString(cardservice.connection.getATR()),file=f)
    except:
        print("Cant not Get ATR")

完整信息:

https://pyscard.sourceforge.io/pyscard-framework.html#framework-samples

https://github.com/LudovicRousseau/pyscard

https://pyscard.sourceforge.io/user-guide.html