• 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="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(),
    	   },
    

      

      

  • 相关阅读:
    PCL:描述三维离散点的ROPS特征(Code)
    veket智能机器人
    三维重建:SLAM的粒度和工程化问题
    DNN:逻辑回归与 SoftMax 回归方法
    人工智能:一种现代方法 第四版 翻译序言
    编程低级错误记录
    apache服务器配置防盗链(centos7)
    Linux下命令行中的复制和粘贴
    rm: cannot remove `libtoolT’: No such file or directory
    switch范围判断
  • 原文地址:https://www.cnblogs.com/qq-757617012/p/4558808.html
Copyright © 2020-2023  润新知