google 公开一次性时间加密器算法:


返得利购物, 淘宝,京东500家商城合作,包含全面的商城返利网。注册就送5元,购物就有返利。随时提现。 同学们,新一轮的返利大潮正在慢慢靠近,让购物都觉得自己在赚钱,购物,机票,游戏,酒店旅游,地方特色,娱乐,尽在www.bbuy8.com让你购物省钱,省心。【群号:335156195】


Google公开的一次性时间加密算法:在已经适用IOS和一般的安卓手机。



官方网站:https://code.google.com/p/google-authenticator/;点击打开链接



package example;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.UndeclaredThrowableException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class GoogleDydaimc {



     private static byte[] hmac_sha(String crypto, byte[] keyBytes,
             byte[] text){
         try {
             Mac hmac;
             hmac = Mac.getInstance(crypto);
             SecretKeySpec macKey =
                 new SecretKeySpec(keyBytes, "RAW");
             hmac.init(macKey);
             return hmac.doFinal(text);
         } catch (GeneralSecurityException gse) {
             throw new UndeclaredThrowableException(gse);
         }
     }


     private static byte[] hexStr2Bytes(String hex){
         // Adding one byte to get the right conversion
         // Values starting with "0" can be converted
         byte[] bArray = new BigInteger("10" + hex,16).toByteArray();

         // Copy all the REAL bytes, not the "first"
         byte[] ret = new byte[bArray.length - 1];
         for (int i = 0; i < ret.length; i++)
             ret[i] = bArray[i+1];
         return ret;
     }

     private static final int[] DIGITS_POWER
     // 0 1  2   3    4     5      6       7        8
     = {1,10,100,1000,10000,100000,1000000,10000000,100000000 };






     public static String generateTOTP(String key,
             String time,
             String returnDigits){
         return generateTOTP(key, time, returnDigits, "HmacSHA1");
     }


     public static String generateTOTP256(String key,
             String time,
             String returnDigits){
         return generateTOTP(key, time, returnDigits, "HmacSHA256");
     }

     public static String generateTOTP512(String key,
             String time,
             String returnDigits){
         return generateTOTP(key, time, returnDigits, "HmacSHA512");
     }


     
     
     //TOTP  加密
     public static String generateTOTP(String key,
             String time,
             String returnDigits,
             String crypto){
         int codeDigits = Integer.decode(returnDigits).intValue();

         String result = null;

         // Using the counter
         // First 8 bytes are for the movingFactor
         // Compliant with base RFC 4226 (HOTP)
         while (time.length() < 16 )
             time = "0" + time;

         // Get the HEX in a Byte[]
         byte[] msg = hexStr2Bytes(time);
         
       
         //原始标准
         byte[] k = hexStr2Bytes(key);    
       
         byte[] hash = hmac_sha(crypto, k, msg);

         // put selected bytes into result int
         int offset = hash[hash.length - 1] & 0xf;
     
         int binary =
             ((hash[offset] & 0x7f) << 24) |
             ((hash[offset + 1] & 0xff) << 16) |
             ((hash[offset + 2] & 0xff) << 8) |
             (hash[offset + 3] & 0xff);
         
         
         
         

         int otp = binary % DIGITS_POWER[codeDigits];
         result = Integer.toString(otp);
         while (result.length() < codeDigits) {
             result = "0" + result;
         }
         return result;
     }

     
     
     
     
     //  test
     public static void main(String[] args) {
        
         System.out.println(SHA1validation("qwerqwerqwerqw63","619666"));

     }
     
     
     
     
     
     
     
     
     
     
     //  RFC  3548     SHA1 验证
     public static boolean   SHA1validation(String base32Key,String   DynamicCode)
     {
         boolean    flag= false;
         String ReturnDynamicCode ="";
         long T0 = 0;
         long X = 30;
         long  date =System.currentTimeMillis();
         date=date/1000;
         System.out.println(date);
         long testTime[] = {date};

         String steps = "0";
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
             for (int i=0; i<testTime.length; i++) {
                
                 long T = (testTime[i] - T0)/X;
                 steps = Long.toHexString(T).toUpperCase();     
                 while (steps.length() < 16)
                  steps = "0" + steps;
                 String fmtTime = String.format("%1$-11s", testTime[i]);
                 String utcTime = df.format(new Date(testTime[i]*1000));
                 ReturnDynamicCode=generateTOTP(base32Key, steps, "6","HmacSHA1");
             }
             if(ReturnDynamicCode.equals(DynamicCode))
             {
                 flag=true;
             }
        
         return flag;    
     }
     
     //  RFC  3548     SHA256 验证
     public static  boolean   SHA256validation(String base32Key,String   DynamicCode)
     {
         boolean    flag= false;
         String ReturnDynamicCode ="";
         long T0 = 0;
         long X = 30;
         long  date =System.currentTimeMillis();
         date=date/1000;
         System.out.println(date);
         long testTime[] = {date};

         String steps = "0";
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
             for (int i=0; i<testTime.length; i++) {
                
                 long T = (testTime[i] - T0)/X;
                 steps = Long.toHexString(T).toUpperCase();     
                 while (steps.length() < 16)
                  steps = "0" + steps;
                 String fmtTime = String.format("%1$-11s", testTime[i]);
                 String utcTime = df.format(new Date(testTime[i]*1000));
                 ReturnDynamicCode=generateTOTP(base32Key, steps, "6","HmacSHA256");
             }
             if(ReturnDynamicCode.equals(DynamicCode))
             {
                 flag=true;
             }
        
         return flag;    
     }
     
     
     
     
 //  RFC  3548     SHA256 验证
     public static boolean   SHA512validation(String base32Key,String   DynamicCode)
     {
         boolean    flag= false;
         String ReturnDynamicCode ="";
         long T0 = 0;
         long X = 30;
         long  date =System.currentTimeMillis();
         date=date/1000;
         System.out.println(date);
         long testTime[] = {date};

         String steps = "0";
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
             for (int i=0; i<testTime.length; i++) {
                
                 long T = (testTime[i] - T0)/X;
                 steps = Long.toHexString(T).toUpperCase();     
                 while (steps.length() < 16)
                  steps = "0" + steps;
                 String fmtTime = String.format("%1$-11s", testTime[i]);
                 String utcTime = df.format(new Date(testTime[i]*1000));
                 ReturnDynamicCode=generateTOTP(base32Key, steps, "6","HmacSHA512");
             }
             if(ReturnDynamicCode.equals(DynamicCode))
             {
                 flag=true;
             }
        
         return flag;    
     }
     
     
     
     
     
     
     
     
     
     public  static  void   test()
     {
         // Seed for HMAC-SHA1 - 20 bytes
         String seed = "3132333435363738393031323334353637383930";
         // Seed for HMAC-SHA256 - 32 bytes
         String seed32 = "qwerqwerqwerqw63";
         // Seed for HMAC-SHA512 - 64 bytes
         String seed64 = "3132333435363738393031323334353637383930" +
         "3132333435363738393031323334353637383930" +
         "3132333435363738393031323334353637383930" +
         "31323334";
         
         long T0 = 0;
         long X = 30;
         long  date =System.currentTimeMillis();
         date=date/1000;
         System.out.println(date);
         long testTime[] = {date};

         String steps = "0";
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
         try {
             System.out.println(
                     "+---------------+-----------------------+" +
             "------------------+--------+--------+");
             System.out.println(
                     "|  Time(sec)    |   Time (UTC format)   " +
             "| Value of T(Hex)  |  TOTP  | Mode   |");
             System.out.println(
                     "+---------------+-----------------------+" +
             "------------------+--------+--------+");

             for (int i=0; i<testTime.length; i++) {
                
                
                
                
                
//                 byte[] buffer={};
//                 String secret = new String(new().encode(buffer));
                
                
                
                
                
                
                
                 long T = (testTime[i] - T0)/X;
                 steps = Long.toHexString(T).toUpperCase();
                 
                 //System.out.println(steps);
                 
                 
                 while (steps.length() < 16)
                  steps = "0" + steps;
                 String fmtTime = String.format("%1$-11s", testTime[i]);
                 String utcTime = df.format(new Date(testTime[i]*1000));
                 System.out.print("|  " + fmtTime + "  |  " + utcTime +
                         "  | " + steps + " |");
                 System.out.println(generateTOTP(seed32, steps, "6",
                 "HmacSHA1") + "| SHA1   |");
                 
                 System.out.print("|  " + fmtTime + "  |  " + utcTime +
                         "  | " + steps + " |");
                 
                 System.out.println(generateTOTP(seed32, steps, "6",
                 "HmacSHA256") + "| SHA256 |");
                 
                 System.out.print("|  " + fmtTime + "  |  " + utcTime +
                         "  | " + steps + " |");
                 
                 System.out.println(generateTOTP(seed32, steps, "6",
                 "HmacSHA512") + "| SHA512 |");


                 System.out.println(
                         "+---------------+-----------------------+" +
                 "------------------+--------+--------+");
                
                
             }
         }catch (final Exception e){
             System.out.println("Error : " + e);
         }
     }
     
     
     
     
     
     
     
 }



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码可剥落

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

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

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

打赏作者

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

抵扣说明:

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

余额充值