1+ package com .yale .test .ps ;
2+
3+ import java .io .IOException ;
4+ import java .io .UnsupportedEncodingException ;
5+ import java .security .InvalidKeyException ;
6+ import java .security .NoSuchAlgorithmException ;
7+
8+ import javax .crypto .KeyGenerator ;
9+ import javax .crypto .Mac ;
10+ import javax .crypto .SecretKey ;
11+ import javax .crypto .spec .SecretKeySpec ;
12+
13+ /*
14+ * Hmac算法
15+ * HMAC(Hash Message Authentication Code,散列消息鉴别码),基于密钥的Hash算法的认证协议。消息鉴别码实现鉴别的原理是,用公开函数和密钥产生一个固定长度的值作为认证标识,
16+ * 用这个标识鉴别消息的完整性。使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输。接收方利用与发送方共享的密钥进行鉴别认证等。
17+ * http://www.jfh.com/jfperiodical/article/818
18+ */
19+ public class Hmac2 {
20+ public static final String KEY_MAC = "HmacMD5" ;
21+
22+ public static void main (String [] args ) {
23+ try {
24+ String str = "简单加密" ;
25+ //使用同一密钥:对数据进行加密:查看俩次加密的结果是否一样
26+ getResult1 (str );
27+ getResult2 (str );
28+ } catch (NoSuchAlgorithmException e ) {
29+ e .printStackTrace ();
30+ } catch (InvalidKeyException e ) {
31+ e .printStackTrace ();
32+ } catch (IllegalStateException e ) {
33+ e .printStackTrace ();
34+ } catch (UnsupportedEncodingException e ) {
35+ e .printStackTrace ();
36+ } catch (IOException e ) {
37+ e .printStackTrace ();
38+ }
39+ }
40+
41+ /*
42+ * 初始化HMAC密钥
43+ */
44+ public static String initMacKey () throws NoSuchAlgorithmException {
45+ KeyGenerator keyGenerator = KeyGenerator .getInstance (KEY_MAC );
46+ SecretKey secretKey = keyGenerator .generateKey ();
47+ return Base64Test .encryptBASE64 (secretKey .getEncoded ());
48+ }
49+
50+ /*
51+ * HMAC加密:主要方法
52+ */
53+ public static String encryptHMAC (byte [] data , String key ) throws IOException , NoSuchAlgorithmException , InvalidKeyException {
54+ SecretKey secretKey = new SecretKeySpec (Base64Test .decryptBASE64 (key ), KEY_MAC );
55+ Mac mac = Mac .getInstance (secretKey .getAlgorithm ());
56+ mac .init (secretKey );
57+ return new String (mac .doFinal (data ));
58+ }
59+
60+ public static String getResult1 (String inputStr ) throws NoSuchAlgorithmException , InvalidKeyException , IOException {
61+ String path = "" ;//Tools.getClassPath()
62+ String fileSource = path + "/file/HMAC_key.txt" ;
63+ System .out .println ("==================加密前的数据:" + inputStr );
64+ byte [] inputdata = inputStr .getBytes ();
65+ String key = Hmac2 .initMacKey ();//产生密钥
66+ System .out .println ("Mac密钥:===" + key );
67+ //将密钥写入文件Tools.WriteMyFile(fileSource, key);
68+
69+ String result = Hmac2 .encryptHMAC (inputdata , key );//加密
70+ System .out .println ("HMAC加密后:======" + result );
71+ return result ;
72+ }
73+
74+ public static String getResult2 (String inputStr ) throws InvalidKeyException , NoSuchAlgorithmException , IOException {
75+ System .out .println ("加密前的数据==========" + inputStr );
76+ String path = "" ;//Tools.getClassPath();
77+ String fileSource = path + "/file/HMAC_key.txt" ;
78+ String key = null ;
79+ //将密钥从文件中读取
80+ key = "" ; //Tools.ReadMyFile(fileSource)
81+ System .out .println ("读取的密钥为:=================" + key );
82+ byte [] inputData = inputStr .getBytes ();
83+ //对数据进行加密
84+ String result = Hmac2 .encryptHMAC (inputData , key );
85+ System .out .println ("HMAC加密后:===" + result );
86+ return result ;
87+ }
88+ }
0 commit comments