📄 aesclient.java
字号:
/*
* file: AesClient.java
*
* author: Franz Helmut Philip Kalchmair
* email: bier444@yahoo.de
* date: 10/03
*
* Testfile for javacard applet AesJcop.
*
*/
import java.util.*;
import opencard.core.service.*;
import opencard.core.terminal.*;
import opencard.opt.util.*;
public class AesClient {
private static final int IFD_TIMEOUT = 10; // unit: seconds
private static final int MAX_APDU_SIZE = 100; // unit: byte
private PassThruCardService ptcs;
private int n, x;
private byte[] i;
public AesClient() {
// get system properties
Properties sysProps = System.getProperties();
// set system properties for OCF, PC/SC and PassThruCardServce
sysProps.put ("OpenCard.terminals",
"com.ibm.opencard.terminal.pcsc10.Pcsc10CardTerminalFactory");
sysProps.put ("OpenCard.services","opencard.opt.util.PassThruCardServiceFactory");
}
private void showCmdAPDU(String topic, CommandAPDU command)
{
String s;
System.out.print(topic+": ");
for (n=0; n<command.getLength(); n++) {
s = Integer.toHexString(command.getByte(n)).toUpperCase();
if (s.length()== 1) s = "0" + s;
System.out.print(s + " ");
}
System.out.println();
}
private void showRspAPDU(String topic, ResponseAPDU command)
{
String s;
System.out.print(topic+": ");
for (n=0; n<command.getLength(); n++) {
s = Integer.toHexString(command.getByte(n)).toUpperCase();
if (s.length()== 1) s = "0" + s;
System.out.print(s + " ");
}
System.out.println();
}
private void showResult(byte[] res)
{
int length = res.length;
if (length==2)
{
if (res[0]==(byte)0x90 && res[1]==(byte)0x00)
System.out.println("done");
else
if (res[0]==(byte)0x67 && res[1]==(byte)0x00)
System.out.println("Wrong length");
else
showBytes(res, "\nRESULT", 0, 0);
}
else
{
showBytes(res, "RESULT", 0, 2);
}
}
private void showBytes(byte[] a, String topic, int offset, int skipAtEnd)
{
int length = a.length;
System.out.print(topic+": ");
for(int i=offset;i<(length-skipAtEnd);i++)
{
if ((a[i]&0xff)<16) System.out.print("0");
System.out.print(Integer.toHexString(a[i]&0xff));
}
System.out.println();
}
public void encrypt() {
try {
System.out.println("activate Smart Card");
SmartCard.start();
CardRequest cr = new CardRequest(CardRequest.ANYCARD, null, PassThruCardService.class);
cr.setTimeout(IFD_TIMEOUT); // set timeout for IFD
System.out.println("wait for smart card - insert smart card in terminal");
// wait for a smart card inserted into the terminal
SmartCard sc = SmartCard.waitForCard(cr);
if (sc != null) { // no error occur and a smart card is in the terminal
// install applet
// 80 E6 0C 00 29 06 11 22 33 44 55 66 06 11 22 33 44 55 66 06 11 22 33 44 55 66 01 00 10 EF 0C C6 02 00 00 C8 02 00 00 C7 02 00 00 C9 00 00 00
ptcs = (PassThruCardService) sc.getCardService(PassThruCardService.class, true);
String s = new String();
System.out.print("ATR: ");
CardID cardID = sc.getCardID ();
i = cardID.getATR();
for (n=0; n<i.length; n++) {
x = (int) (0x000000FF & i[n]); // byte to int conversion
s = Integer.toHexString(x).toUpperCase();
if (s.length()== 1) s = "0" + s;
System.out.print(s + " ");
};
System.out.println("");
// set APDU buffer size
CommandAPDU command = new CommandAPDU(MAX_APDU_SIZE);
// prepare command APDU - Select Applet
byte[] CMD_SELECT_APPLET = new byte[] {0x00,(byte)0xA4,0x04,0x00,0x06,0x11,0x22,0x33,0x44,0x55,0x66,0x00};
System.out.print("Select Applet with Aid 112233445566 .......... ");
long startTime = System.currentTimeMillis();
// show command apdu
//showCmdAPDU("Command-APDU", command);
command.append(CMD_SELECT_APPLET);
// send command and receive response
ResponseAPDU response = ptcs.sendCommandAPDU(command);
long stopTime = System.currentTimeMillis();
// show response apdu
//showRspAPDU("Response-APDU", response);
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
// prepare command APDU - initS
command.setLength(0);
byte[] CMD_INITS = new byte[] {0,0,0,0,0};
System.out.print("Init S-Boxes ................................. ");
startTime = System.currentTimeMillis();
// show command apdu
// showAPDU("Command-APDU", command);
command.append(CMD_INITS);
// send command and receive response
response = ptcs.sendCommandAPDU(command);
stopTime = System.currentTimeMillis();
// show int response apdu
// showAPDU("Response-APDU", response);
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
// prepare command APDU - initT, setKey
command.setLength(0);
byte[] CMD_INITT = new byte[] {0,0,3,0,0};
System.out.print("Init T-Boxes, set Key ........................ ");
startTime = System.currentTimeMillis();
// show command apdu
// showAPDU("Command-APDU", command);
command.append(CMD_INITT);
// send command and receive response
response = ptcs.sendCommandAPDU(command);
stopTime = System.currentTimeMillis();
// show response apdu
// showAPDU("Response-APDU", response);
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
// prepare command APDU - initT, setKey
command.setLength(0);
// /send 00020000#(000102030405060708090a0b0c0d0e0f)
byte[] CMD_ENCRYPT = new byte[] {0,2,0,0,16,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
System.out.println("Encrypt 16 bytes: 000102030405060708090a0b0c0d0e0f");
// show command apdu
// showAPDU("Command-APDU", command);
startTime = System.currentTimeMillis();
command.append(CMD_ENCRYPT);
// send command and receive response
response = ptcs.sendCommandAPDU(command);
stopTime = System.currentTimeMillis();
// show response apdu
// showAPDU("Response-APDU", response);
System.out.println("Result should be: 0A940BB5416EF045F1C39458C653EA5A");
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
command.setLength(0);
// abcdefghijklmnop
CMD_ENCRYPT = new byte[] {0,2,0,0,16,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,0};
showBytes(CMD_ENCRYPT, "INPUT ", 5, 1);
startTime = System.currentTimeMillis();
command.append(CMD_ENCRYPT);
response = ptcs.sendCommandAPDU(command);
stopTime = System.currentTimeMillis();
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
command.setLength(0);
// 0123456789012345
CMD_ENCRYPT = new byte[] {0,2,0,0,16,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,0};
showBytes(CMD_ENCRYPT, "INPUT ", 5, 1);
startTime = System.currentTimeMillis();
command.append(CMD_ENCRYPT);
response = ptcs.sendCommandAPDU(command);
stopTime = System.currentTimeMillis();
showResult(response.getBytes());
System.out.println( "Dauer: " + (stopTime - startTime) + " ms");
// destroy applet
// 80 E4 00 00 08 4F 06 11 22 33 44 55 66 00
System.out.println("deactivate Smart Card");
} // if
else
System.out.println("could not create smart card object (e.g. no ICC in IFD)");
sc.close();
SmartCard.shutdown();
} // try
catch (Exception except) {
System.err.println("exception '" + except.getClass() + "' - " + except.getMessage() );
} // catch
finally {
//System.exit(0);
} // finally
} // run example
public static void main( String [] args ){
System.out.println("---------------\nStart AesClient\n");
AesClient aesclient = new AesClient();
aesclient.encrypt();
} // main
} // class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -