java 和 javascript CryptoJS 進行HmacSHA1加密


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

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

/**
 * 
 * HmacSHA1加密類
 *
 */
public class SHA1 {
    
    public static String getHmacSHA1(String password,String loginname, String algorithm){
        byte[] keyBytes = password.getBytes();
        Key key = new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm);
        Mac mac=null;
        try {
            mac = Mac.getInstance(algorithm);
            mac.init(key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return byteArrayToHex(mac.doFinal(loginname.getBytes()));
    }
    
    
    /**
     * 16進制加密
     * @param a
     * @return
     */
    protected static String byteArrayToHex(byte [] a) {
        int hn, ln, cx;
        String hexDigitChars = "0123456789abcdef";
        StringBuffer buf = new StringBuffer(a.length * 2);
        for(cx = 0; cx < a.length; cx++) {
            hn = ((int)(a[cx]) & 0x00ff) /16 ;
            ln = ((int)(a[cx]) & 0x000f);
            buf.append(hexDigitChars.charAt(hn));
            buf.append(hexDigitChars.charAt(ln));
        }
        return buf.toString();

    }
    
    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException {
        
        String loginKey= getHmacSHA1("密碼", "用戶名", "HmacSHA1");
        System.out.println(loginKey);//53b3a8413cf49e939d8711a0ba34916b2ec2db75
        String loginKey2= getHmacSHA1("123456", "admin", "HmacSHA1");
        System.out.println(loginKey2);//3c39afa93e0b12c28f1f08b18488ebd4ad2e5858
        
    }
}

html+JavaScript代碼:

<html>
    <head>
        <script src="./hmac-sha1.js"></script>
        <script type="text/javascript">
            function genkey() {
                var userName=document.getElementById("userName").value;
                var password=document.getElementById("password").value;
                var hash = CryptoJS.HmacSHA1(userName, password);
                document.getElementById("key").value=hash;
            };
        </script>
    </head>

    <body>
        用戶名:<input id="userName" value="" type="text">
        密碼:<input id="password" value="" type="text"><br>
        40位字符key:<input id="key" value="" type="text"  style="width:400px"><br>
        <input id="genKey" value="生成key" type="button" onclick="genkey()">

    </body>
</html>

java 以及 js  ,html 源碼下載:   鏈接:http://pan.baidu.com/s/1c0pTIes 密碼:j77s

 

值得注意的是: 前台頁面通過js加密后,直接進行ajax請求時會出現 ajax無法執行,原因無非是參數不對,這是 要把加密厚的 key放在一個隱藏域中(變成了字符串,而非對象)然后再取出

<input type="hidden" id="key" >

  

var key= CryptoJS.HmacSHA1(companyAccount, password);

$("#key").val(key);

  

$.ajax({
      url:urlStr,
      data:{	
	    "password":  $("#key").val(),
	   },

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM