java生成一次性动态时间码(1) 安全公司提供的算法:

未其公司保密:因此不提供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());
            }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码可剥落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值