java生成一次性动态时间码(1) 安全公司提供的算法:
2013-06-14 14:30
337 查看
未其公司保密:因此不提供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());
}
}
}
// 导入包: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());
}
}
}
相关文章推荐
- 关于动态生成列的DATAGRID不能出发模板列的时间问题
- Java 语言实现的随机数生成算法
- Java 动态生成复杂 Word
- Java网络编程基础 1 建立一个提供当前时间字符串的ServerSocket
- 免费提供动态生成,读写并在线预览Office文档解决方案
- java计时器线程(自动刷新、动态时间)
- PDA动态调用JAVA提供的WebService
- 各大IT公司算法和数据结构面试题整理Java实现
- JavaPoet 动态生成Java源码(1)---Android
- 深入理解Java:SimpleDateFormat安全的时间格式化
- 使用自动生成java文件和自动编译的动态代理模式
- java安全架构____数字证书生成
- 关于动态生成WML文件的一个Java实例(3)
- 关于时间,日期,星期,月份的算法(Java中Calendar的用法)(一)
- Java动态生成复杂word
- 简化 Ajax 和 Java 开发,第 1 部分: 用 JSP 标记文件动态生成 JavaScript 代码
- 【JAVA服务端架构】分布式环境下Hilo算法生成自增编号
- 动态生成JAVA代码(二)
- TOTP算法 基于时间的一次性密码
- (基于Java)算法之最坏情况下的线性时间选择