本文整理汇总了Java中javax.smartcardio.CommandAPDU类的典型用法代码示例。如果您正苦于以下问题:Java CommandAPDU类的具体用法?Java CommandAPDU怎么用?Java CommandAPDU使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CommandAPDU类属于javax.smartcardio包,在下文中一共展示了CommandAPDU类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReadPhoto
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
@Test
public void testReadPhoto() throws Exception {
TerminalFactory terminalFactory = TerminalFactory.getDefault();
CardTerminals cardTerminals = terminalFactory.terminals();
CardTerminal cardTerminal = cardTerminals.list().get(0);
Card card = cardTerminal.connect("T=0");
CardChannel cardChannel = card.getBasicChannel();
// select file
cardChannel.transmit(
new CommandAPDU(0x00, 0xA4, 0x08, 0x0C, new byte[] { 0x3F, 0x00, (byte) 0xDF, 0x01, 0x40, 0x35 }));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int offset = 0;
ResponseAPDU responseApdu;
do {
// read binary
responseApdu = cardChannel.transmit(new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, 0xff));
baos.write(responseApdu.getData());
offset += responseApdu.getData().length;
} while (responseApdu.getData().length == 0xff);
BufferedImage photo = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
JOptionPane.showMessageDialog(null, new ImageIcon(photo));
}
示例2: formatFailedAPDUCmd
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
static String formatFailedAPDUCmd(CommandAPDU failedCommand) {
// If command corectness verification failed, then output for easy resend during debugging later
byte[] failedcmd = failedCommand.getBytes();
String failedAPDU = String.format("\tcardMngr.transmit(new CommandAPDU(hexStringToByteArray(\"%s\")));", Util.bytesToHex(failedcmd));
failedAPDU += "\n\tstatic byte[] failedCommand = {";
for (int i = 0; i < failedcmd.length; i++) {
failedAPDU += String.format("(byte) 0x%x", failedcmd[i]);
if (i != failedcmd.length - 1) {
failedAPDU += ", ";
}
}
failedAPDU += "};";
failedAPDU += "\n\t";
return failedAPDU;
}
示例3: transmit
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
public ResponseAPDU transmit(CommandAPDU cmd)
throws CardException {
m_lastCommand = cmd;
if (m_bDebug == true) {
log(cmd);
}
long elapsed = -System.currentTimeMillis();
ResponseAPDU response = m_channel.transmit(cmd);
elapsed += System.currentTimeMillis();
m_lastTransmitTime = elapsed;
if (m_bDebug == true) {
log(response, m_lastTransmitTime);
}
return response;
}
示例4: testAPDU
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
void testAPDU(CardManager cardMngr, String input, String expectedOutput) {
try {
ResponseAPDU response = cardMngr.transmit(new CommandAPDU(hexStringToByteArray(input)));
if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
if (!expectedOutput.isEmpty()) {
byte[] data = response.getData();
String output = Util.bytesToHex(data);
assertTrue(expectedOutput.equalsIgnoreCase(output), "Result provided by card mismatch expected");
}
}
else {
assertTrue(false, String.format("Card failed with 0x%x", response.getSW()));
}
}
catch (Exception e) {
e.printStackTrace();
assertTrue(false, "Card transmit failed with execption");
}
}
示例5: transmit
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
private ResponseAPDU transmit(CardChannel channel, CommandAPDU cmd)
throws CardException {
log(cmd);
long elapsed = -System.currentTimeMillis();
ResponseAPDU response = channel.transmit(cmd);
elapsed += System.currentTimeMillis();
lastTransmitTime = elapsed;
log(response, elapsed);
return response;
}
示例6: insertTerminal
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
private void insertTerminal(Class<? extends Applet> card) throws CardException
{
if (card != null)
{
CardSimulator simulator = new CardSimulator();
AID appletAID = AIDUtil.create("D2760000015445535442");
simulator.installApplet(appletAID, card);
CardTerminal terminal = CardTerminalSimulator.terminal(simulator);
CommandAPDU selectCommand = new CommandAPDU(AIDUtil.select(appletAID));
simulator.transmitCommand(selectCommand);
terminallist.add(terminal);
terminallistCardPresent.add(terminal);
}
else
{
CardTerminal emptyTerminal = EasyMock.createNiceMock(CardTerminal.class);
EasyMock.expect(emptyTerminal.connect(EasyMock.anyString())).andThrow(
new javax.smartcardio.CardNotPresentException("no card found"));
EasyMock.expect(emptyTerminal.isCardPresent()).andReturn(false);
EasyMock.replay(emptyTerminal);
terminallist.add(emptyTerminal);
}
}
示例7: setAuth0
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Set the page from which authentication is required. The default is
* 48, which means no restriction.
*
* <p>For example, if {@code page} is 20 then authentication is
* required from page 20 until the end of the memory. Whether it is only
* write restricted or read and write restricted depends on auth1.
*
* @param page page number from which authentication is required
* @return <code>true</code> if set successfully
*/
public boolean setAuth0(int page) {
if (page < 0 || page > 48) {
return false;
}
byte[] apdu = new byte[9];
apdu[0] = (byte) 0xFF;
apdu[1] = (byte) 0xD6;
apdu[2] = 0x00;
apdu[3] = 0x2A;
apdu[4] = 0x04;
apdu[5] = (byte) page;
apdu[6] = apdu[7] = apdu[8] = 0x00;
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
feedback(command, response);
if (response.getSW() == (0x90 << 8 | 0x00)) {
return true;
}
return false;
}
示例8: setAuth1
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Set write or read+write restriction on page range set in auth1.
*
* @param allowRead {@code true} to allow read and prevent write,
* {@code false} to prevent both read and write
* @return {@code true} on success
*/
public boolean setAuth1(boolean allowRead) {
byte[] apdu = new byte[9];
apdu[0] = (byte) 0xFF;
apdu[1] = (byte) 0xD6;
apdu[2] = 0x00;
apdu[3] = 0x2B;
apdu[4] = 0x04;
apdu[6] = apdu[7] = apdu[8] = 0x00;
if (allowRead) {
apdu[5] = 0x01;
} else {
apdu[5] = 0x00;
}
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
feedback(command, response);
if (response.getSW() == (0x90 << 8 | 0x00)) {
return true;
}
return false;
}
示例9: read
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Read a 4-byte page.
*
* @param page the page number to be read (0<=page<=43)
* @return a 4-byte array with the page contents in hexadecimal, or
* {@code null} on error
*/
public byte[] read(int page) {
if (page < 0 || page > 43) {
return null;
}
byte[] apdu = {(byte) 0xFF, (byte) 0xB0, 0x00, (byte) page, 0x04};
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
feedback(command, response);
if (response.getSW() != (0x90 << 8 | 0x00)) {
return null;
}
return response.getData();
}
示例10: update
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Update a 4-byte user page.
*
* @param page the number of the page to be updated (4<=page<=39)
* @param data a 4-byte array containing the data in hexadecimal
* @return {@code true} on success
*/
public boolean update(int page, byte[] data) {
if (page < 4 || page > 39) {
// outside of user memory
return false;
}
byte[] apdu = new byte[9];
apdu[0] = (byte) 0xFF;
apdu[1] = (byte) 0xD6;
apdu[2] = 0x00;
apdu[3] = (byte) page;
apdu[4] = 0x04;
System.arraycopy(data, 0, apdu, 5, 4);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
feedback(command, response);
if (response.getSW() != (0x90 << 8 | 0x00)) {
return false;
}
return true;
}
示例11: changeKeySettings
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Change the master key settings or the application master key settings,
* depending on the selected AID.
* <p>
* Requires a preceding authentication.
*
* @param keySett the new key settings
* @return {@code true} on success, {@code false} otherwise
*/
public boolean changeKeySettings(byte keySett) {
byte[] apdu = new byte[7];
apdu[0] = (byte) 0x90;
apdu[1] = (byte) Command.CHANGE_KEY_SETTINGS.getCode();
apdu[4] = 0x01;
apdu[5] = keySett;
apdu = preprocess(apdu, CommunicationSetting.ENCIPHERED);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
code = response.getSW2();
feedback(command, response);
return postprocess(response.getBytes(), CommunicationSetting.PLAIN) != null;
}
示例12: getKeyVersion
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Read the current version of a key stored in the PICC.
* <p>
* Note that the key version should be set when changing a key
* since this command will allow to see the parity bits of the
* DES/3DES keys. It can be set to a default value.
* <p>
* To change the version of a key the {@linkplain #changeKey()} command
* should be used.
*
* @param keyNo the key number
* @return the 1-byte version of the key
*/
public byte getKeyVersion(byte keyNo) {
byte[] apdu = new byte[7];
apdu[0] = (byte) 0x90;
apdu[1] = (byte) Command.GET_KEY_VERSION.getCode();
apdu[4] = 0x01;
apdu[5] = keyNo;
preprocess(apdu, CommunicationSetting.PLAIN);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
code = response.getSW2();
feedback(command, response);
byte[] ret = postprocess(response.getBytes(), CommunicationSetting.PLAIN);
if (ret.length != 1)
return FAKE_NO;
else
return ret[0];
}
示例13: createApplication
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Create a new application.
* Requires the PICC-level AID to be selected (00 00 00).
*
* @param aid 3-byte AID
* @param amks application master key settings
* @param nok number of keys (concatenated with 0x40 or 0x80 for 3K3DES and AES respectively)
* @return <code>true</code> on success, <code>false</code> otherwise
*/
public boolean createApplication(byte[] aid, byte amks, byte nok) {
byte[] apdu = new byte[11];
apdu[0] = (byte) 0x90;
apdu[1] = (byte) Command.CREATE_APPLICATION.getCode();
apdu[4] = 0x05;
System.arraycopy(aid, 0, apdu, 5, 3);
apdu[8] = amks;
apdu[9] = nok;
preprocess(apdu, CommunicationSetting.PLAIN);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
code = response.getSW2();
feedback(command, response);
return postprocess(response.getBytes(), CommunicationSetting.PLAIN) != null;
}
示例14: deleteApplication
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Delete an application.
* Depending on the PICC master key settings, an authentication is required
* either with the PICC master key or with the application master key.
*
* @param aid the 3-byte AID of the application to delete
* @return {@code true} on success, {@code false} otherwise
*/
public boolean deleteApplication(byte[] aid) {
byte[] apdu = new byte[9];
apdu[0] = (byte) 0x90;
apdu[1] = (byte) Command.DELETE_APPLICATION.getCode();
apdu[4] = 0x03;
System.arraycopy(aid, 0, apdu, 5, 3);
preprocess(apdu, CommunicationSetting.PLAIN);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
code = response.getSW2();
feedback(command, response);
byte[] ret = postprocess(response.getBytes(), CommunicationSetting.PLAIN);
if (ret != null) {
System.out.println("ret is null");
if (this.aid == aid)
reset();
return true;
}
System.out.println("ret is NOT null");
return false;
}
示例15: selectApplication
import javax.smartcardio.CommandAPDU; //导入依赖的package包/类
/**
* Select the PICC or a specific application for further access.
* The authentication state is lost.
*
* @param aid the 3-byte AID
* @return {@code true} on success, {@code false} otherwise
*/
public boolean selectApplication(byte[] aid) {
byte[] apdu = new byte[9];
apdu[0] = (byte) 0x90;
apdu[1] = (byte) Command.SELECT_APPLICATION.getCode();
apdu[4] = 0x03;
System.arraycopy(aid, 0, apdu, 5, 3);
CommandAPDU command = new CommandAPDU(apdu);
ResponseAPDU response = transmit(command);
code = response.getSW2();
feedback(command, response);
reset();
if (code != 0x00)
return false;
this.aid = aid;
return true;
}