package com.chenzhuo.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.ET199.ET199;
import com.ET199.ET_EXCEPTION;
public class ET199test
{
@SuppressWarnings("static-access")
public static void main(String args[])
{
try
{
System.out.println("et199 test begin...");
//(1)枚举加密锁个数
System.out.println("\n枚举加密锁个数...");
int nDevCount = ET199.Enum();
System.out.println(nDevCount);
//(2)打开索引为0的加密锁
System.out.println("\n打开加密锁1...");
ET199 et = new ET199(0);
displayDeviceInfo(et);
byte[] byDevPinDefault =
{
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
};
try
{
//(3)切换到根目录
System.out.println("\n切换到根目录...");
et.ChangeDir("\\");
//(4)验证开发商pin
System.out.println("\n验证开发商pin...");
et.VerifyPin(byDevPinDefault,ET199.ET_DEV_PIN);
//(5)删除根目录
System.out.println("\n删除根目录...");
et.EraseDir(null);
}
catch(ET_EXCEPTION e)
{
if(e.getExceptionCode() == et.ET_E_DIR_NOT_FOUND)
{
System.out.println("ET199 exception:"+e.getReason());
}
else
{
throw e;
}
}
//(6)创建根目录
System.out.println("\n创建根目录...");
et.CreateDir(null,0,ET199.ET_ROOT_DIR);
//(7)验证开发商pin
System.out.println("\n验证开发商pin...");
et.VerifyPin(byDevPinDefault,ET199.ET_DEV_PIN);
//(8)设置LED闪烁
final int nLedFreq = 5;
byte[] byLedFreq = {(byte)nLedFreq, 0, 0, 0};
System.out.println("\n设置LED灯闪烁,频率为" + nLedFreq + "...");
et.Control(ET199.ET_LED_WINK, byLedFreq, 4, null, null);
//(9)创建子目录
String strSubDirID = "5678";
System.out.println("\n创建子目录,ID为"+strSubDirID+"...");
et.CreateDir(strSubDirID, 0x2000, ET199.ET_SUB_DIR);
//(10)更改开发商pin
System.out.println("\n更改开发商pin...");
byte[] byDevPinNew =
{
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31
};
et.ChangePin(byDevPinDefault, byDevPinNew, ET199.ET_DEV_PIN, (byte)0xff);
//(11)创建一可执行文件
String strExeFileID = "1234";
System.out.println("\n创建一可执行文件,ID为"+strExeFileID+"...");
et.CreateFile(strExeFileID,0x1000,(byte)0);
//(12)写数据到可执行文件
System.out.println("\n写数据到可执行文件"+strExeFileID + "...");
writeExeFileData(et,strExeFileID,"hardware\\hardware.bin");
//(13)验证用户pin
System.out.println("\n验证用户pin...");
byte[] byUserPin =
{
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
};
et.VerifyPin(byUserPin,ET199.ET_USER_PIN);
//(14)运行可执行程序
System.out.println("\n运行可执行程序"+strExeFileID+"...");
byte[] byOutBuffer = new byte[32];
int[] nOutLen = new int[1];
et.Execute(strExeFileID,null,0,byOutBuffer,nOutLen);
//(15)取文件信息
System.out.println("\n取文件信息...");
byte[] fi = new byte[5];
fi[0] = 0x31;
fi[1] = 0x32;
fi[2] = 0x33;
fi[3] = 0x34;
int[] retlen = new int[1];
et.Control(ET199.ET_GET_EF_INFO,fi,4,fi,retlen);
//(16)验证开发商pin
System.out.println("\n验证开发商pin...");
et.VerifyPin(byDevPinNew, ET199.ET_DEV_PIN);
//(17)创建密钥对
byte[] byPubKeyData = new byte[1024];
byte[] byPriKeyData = new byte[1024];
int[] nPubKeyLen = new int[1];
int[] nPriKeyLen = new int[1];
System.out.println("\n创建密钥对...");
et.GenRsaKey((short)1024,0x10001,"1111","2222",byPubKeyData,nPubKeyLen,byPriKeyData,nPriKeyLen);
//(18)切换到上一级目录(根目录)
System.out.println("\n切换到上一级目录...");
et.ChangeDir("..");
//(19)重置设备
System.out.println("\n重置设备...");
int[] nBytesReturn = new int[1];
et.Control(ET199.ET_RESET_DEVICE, null, 0, null, nBytesReturn);
//(20)验证开发商pin
System.out.println("\n验证开发商pin...");
et.VerifyPin(byDevPinDefault, ET199.ET_DEV_PIN);
//(21)删除根目录
System.out.println("\n删除根目录...");
et.EraseDir(null);
//(22)创建根目录
System.out.println("\n创建根目录...");
et.CreateDir("\\", 0, ET199.ET_ROOT_DIR);
//(23)关闭设备
System.out.println("\n关闭设备...");
et.Close();
System.out.println("\nET199 test end");
}
catch(ET_EXCEPTION e)
{
System.out.println("ET199 exception:" + e.getReason());
}
catch(Exception e)
{
System.out.println(e.getCause()+"exception");
}
}
private static void displayDeviceInfo(ET199 et)
{
int nVersion = et.GetVersion();
System.out.print("Version:\t" + nVersion/256 + '.' );
String strtemp = "00"+ String.valueOf((byte)nVersion);
strtemp = strtemp.substring(strtemp.length()-2,strtemp.length());
System.out.println( strtemp );
System.out.println("Customer:\t" + Integer.toHexString(et.GetCustomer()));
String str = new String(et.GetAtr());
System.out.println("Atr:\t\t" + str);
byte[] byID = et.GetID();
System.out.print("ID:\t\t");
for(int i=byID.length-1;i>=0;i--)
{
//System.out.print(Integer.toHexString(byID[i]) + " ");
System.out.print((Integer.toHexString((byID[i] & 0x000000FF) | 0xFFFFFF00).substring(6)).toUpperCase());
}
System.out.println();
}
private static void writeExeFileData(ET199 et,String strFileID,String strFilePath)throws FileNotFoundException,Exception
{
File f = new File(strFilePath);
FileInputStream fs = new FileInputStream(f);
if(f.length() > 10240)
{
throw new Exception("文件太大");
}
byte[] data = new byte[(int)f.length()];
fs.read(data);
et.WriteFile(strFileID,0,data,(int)f.length());
}
}