乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java 加解密技術(shù)系列之 HMAC

       nikybook 2016-01-08
      1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.hmac;  

      2. import com.google.common.base.Strings;  

      3. import sun.misc.BASE64Decoder;  

      4. import sun.misc.BASE64Encoder;  

      5. import javax.crypto.KeyGenerator;  

      6. import javax.crypto.Mac;  

      7. import javax.crypto.SecretKey;  

      8. import javax.crypto.spec.SecretKeySpec;  

      9. import java.security.NoSuchAlgorithmException;  

      10. /** 

      11.  * Created by xiang.li on 2015/2/27. 

      12.  */  

      13. public class HMAC {  

      14.     /** 

      15.      * 定義加密方式 

      16.      * MAC算法可選以下多種算法 

      17.      * <pre> 

      18.      * HmacMD5 

      19.      * HmacSHA1 

      20.      * HmacSHA256 

      21.      * HmacSHA384 

      22.      * HmacSHA512 

      23.      * </pre> 

      24.      */  

      25.     private final static String KEY_MAC = "HmacMD5";  

      26.     /** 

      27.      * 全局?jǐn)?shù)組 

      28.      */  

      29.     private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",  

      30.             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };  

      31.     /** 

      32.      * 構(gòu)造函數(shù) 

      33.      */  

      34.     public HMAC() {  

      35.     }  

      36.     /** 

      37.      * BASE64 加密 

      38.      * @param key 需要加密的字節(jié)數(shù)組 

      39.      * @return 字符串 

      40.      * @throws Exception 

      41.      */  

      42.     public static String encryptBase64(byte[] key) throws Exception {  

      43.         return (new BASE64Encoder()).encodeBuffer(key);  

      44.     }  

      45.     /** 

      46.      * BASE64 解密 

      47.      * @param key 需要解密的字符串 

      48.      * @return 字節(jié)數(shù)組 

      49.      * @throws Exception 

      50.      */  

      51.     public static byte[] decryptBase64(String key) throws Exception {  

      52.         return (new BASE64Decoder()).decodeBuffer(key);  

      53.     }  

      54.     /** 

      55.      * 初始化HMAC密鑰 

      56.      * @return 

      57.      */  

      58.     public static String init() {  

      59.         SecretKey key;  

      60.         String str = "";  

      61.         try {  

      62.             KeyGenerator generator = KeyGenerator.getInstance(KEY_MAC);  

      63.             key = generator.generateKey();  

      64.             str = encryptBase64(key.getEncoded());  

      65.         } catch (NoSuchAlgorithmException e) {  

      66.             e.printStackTrace();  

      67.         } catch (Exception e) {  

      68.             e.printStackTrace();  

      69.         }  

      70.         return str;  

      71.     }  

      72.     /** 

      73.      * HMAC加密 

      74.      * @param data 需要加密的字節(jié)數(shù)組 

      75.      * @param key 密鑰 

      76.      * @return 字節(jié)數(shù)組 

      77.      */  

      78.     public static byte[] encryptHMAC(byte[] data, String key) {  

      79.         SecretKey secretKey;  

      80.         byte[] bytes = null;  

      81.         try {  

      82.             secretKey = new SecretKeySpec(decryptBase64(key), KEY_MAC);  

      83.             Mac mac = Mac.getInstance(secretKey.getAlgorithm());  

      84.             mac.init(secretKey);  

      85.             bytes = mac.doFinal(data);  

      86.         } catch (Exception e) {  

      87.             e.printStackTrace();  

      88.         }  

      89.         return bytes;  

      90.     }  

      91.     /** 

      92.      * HMAC加密 

      93.      * @param data 需要加密的字符串 

      94.      * @param key 密鑰 

      95.      * @return 字符串 

      96.      */  

      97.     public static String encryptHMAC(String data, String key) {  

      98.         if (Strings.isNullOrEmpty(data)) {  

      99.             return null;  

      100.         }  

      101.         byte[] bytes = encryptHMAC(data.getBytes(), key);  

      102.         return byteArrayToHexString(bytes);  

      103.     }  

      104.     /** 

      105.      * 將一個字節(jié)轉(zhuǎn)化成十六進(jìn)制形式的字符串 

      106.      * @param b 字節(jié)數(shù)組 

      107.      * @return 字符串 

      108.      */  

      109.     private static String byteToHexString(byte b) {  

      110.         int ret = b;  

      111.         //System.out.println("ret = " + ret);  

      112.         if (ret < 0) {  

      113.             ret += 256;  

      114.         }  

      115.         int m = ret / 16;  

      116.         int n = ret % 16;  

      117.         return hexDigits[m] + hexDigits[n];  

      118.     }  

      119.     /** 

      120.      * 轉(zhuǎn)換字節(jié)數(shù)組為十六進(jìn)制字符串 

      121.      * @param bytes 字節(jié)數(shù)組 

      122.      * @return 十六進(jìn)制字符串 

      123.      */  

      124.     private static String byteArrayToHexString(byte[] bytes) {  

      125.         StringBuffer sb = new StringBuffer();  

      126.         for (int i = 0; i < bytes.length; i++) {  

      127.             sb.append(byteToHexString(bytes[i]));  

      128.         }  

      129.         return sb.toString();  

      130.     }  

      131.     /** 

      132.      * 測試方法 

      133.      * @param args 

      134.      */  

      135.     public static void main(String[] args) throws Exception {  

      136.         String key = HMAC.init();  

      137.         System.out.println("Mac密鑰:\n" + key);  

      138.         String word = "123";  

      139.         System.out.println(encryptHMAC(word, key));  

      140.     }  

      141. }</span>  

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多