未其公司保密:因此不提供jar包,抱歉。
// 导入包:dKeySDK.jar
package example;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import cn.com.ndkey.dkeyserver.cipher.T6H60OSCipher;
import cn.com.ndkey.dkeyserver.cipher.TokenCipher;
import cn.com.ndkey.dkeyserver.cipher.TokenData;
import cn.com.ndkey.dkeyserver.token.exception.PasswordException;
import cn.com.ndkey.dkeyserver.utils.Arrays;
public class Main
{
public static final String serial = "DKEY01015071"; //令牌序列好
// 字符串形式的种子
public static final String rawSeed = "8A0901346E1A9F3DB17453ABCD3F018724F29517";
// 令牌种子,二进制串形式,
// 示例种子的16进制字符串形式为(7BE22085B1AFBB73D411D19338A485FC33D0734E)
//public static final byte[] seed = {(byte) 0x7B, (byte) 0xE2, (byte) 0x20, (byte) 0x85, (byte) 0xB1, (byte) 0xAF, (byte) 0xBB, (byte) 0x73, (byte) 0xD4, (byte) 0x11, (byte) 0xD1, (byte) 0x93, (byte) 0x38, (byte) 0xA4, (byte) 0x85, (byte) 0xFC, (byte) 0x33, (byte) 0xD0, (byte) 0x73, (byte) 0x4E};
public static final byte[] seed =decodeByte(rawSeed);
public static void main(String[] args) {
try {
System.out.println(rawSeed.length());
//testGetPasswd();
testAuth("469609");
//testSync();
// 将字符串型式的种子转换为二进制格式
byte[] binSeed = decodeByte(rawSeed);
//System.out.println(Arrays.equals(seed, binSeed));
} catch (PasswordException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
//验证输入的动态密码 是否正确
public static void testAuth(String passwd) throws PasswordException {
TokenCipher tokenCipher = new T6H60OSCipher();
TokenData token = new TokenData();
token.setSerial(serial);
token.setSeed(seed);
// 执行authenticate之后如果成功将返回新的drift,
// 将返回的drift保存到数据库,下次认证的时候传入
token.setDrift(0);
boolean result = tokenCipher.authenticate(token, // 令牌信息
passwd, // 用户输入的动态密码
5, // 前向搜索窗口,如:5为允许误差最大快5分钟(5 x 60)
5 // 后向搜索窗口,如:5为允许误差最大慢5分钟(5 x 60)
);
System.out.println("认证结果:" + result);
if (result) {
System.out.println("新的时间偏移:" + token.getDrift());
}
}
// 生成当前动态密码
public static String testGetPasswd() throws PasswordException {
TokenCipher tokenCipher = new T6H60OSCipher();
Calendar currentTime = Calendar.getInstance();
// System.out.println(currentTime.getTime());
String passwd = tokenCipher.getPasswd(serial, seed, currentTime);
System.out.println(passwd);
return passwd;
}
// 将字符串型式的种子转换为二进制格式
public static byte[] decodeByte(String in) {
byte[] out = new byte[in.length() / 2];
for (int i = 0; i < out.length; i++) {
String str = "0x" + in.substring(i * 2, i * 2 + 2);
out[i] = Short.decode(str).byteValue();
}
return out;
}
//同步令牌时间
public static void testSync() throws PasswordException {
TokenCipher tokenCipher = new T6H60OSCipher();
TokenData token = new TokenData();
token.setSerial(serial);
token.setSeed(seed);
// 执行authenticate或者sync之后如果成功将返回新的drift,
// 将返回的drift保存到数据库,下次认证的时候传入
token.setDrift(0);
boolean result = tokenCipher.sync(token, // 令牌信息
"273365", // 第一个动态密码
"393926", // 下一个动态密码
5 // 校时搜索窗口,如:30为允许最大误差30分钟(30 x 60)
);
System.out.println("校时结果:" + result);
if (result) {
System.out.println("新的时间偏移:" + token.getDrift());
}
}
}