java 和 javascript CryptoJS 進行HmacSHA1加密

來源:互聯網
上載者:User

標籤:

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

java 和 javascript CryptoJS 進行HmacSHA1加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.