zoukankan      html  css  js  c++  java
  • 认证加密算法php hash_hmac和java hmacSha1的问题

    public class Test{
    	
    
    	public static void main(String[] args) throws Exception {
                    String postString = "abc";
                    String signature = ByteToHex(genHMAC(postString, "310A54B3C9C12920E1582E016F15DD441ACE8553769E8991")).toUpperCase();        
    	}
    	
    	/** 
         * 使用 HMAC-SHA1 签名方法对data进行签名 
         *  
         * @param data 
         *            被签名的字符串 
         * @param key 
         *            密钥      
         * @return  
                          加密后的字符串 
         */  
        public static byte[] genHMAC(String data, String key) {  
            byte[] result = null;  
            try {  
                //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称    
                SecretKeySpec signinKey = new SecretKeySpec(hexString2Bytes(key), "HmacSHA1");  
                //生成一个指定 Mac 算法 的 Mac 对象    
                Mac mac = Mac.getInstance("HmacSHA1");  
                //用给定密钥初始化 Mac 对象    
                mac.init(signinKey);  
                //完成 Mac 操作     
                byte[] rawHmac = mac.doFinal(data.getBytes());  
                result = rawHmac;  
      
            } catch (NoSuchAlgorithmException e) {  
                System.err.println(e.getMessage());  
            } catch (InvalidKeyException e) {  
                System.err.println(e.getMessage());  
            }  
            if (null != result) {  
                return result;  
            } else {  
                return null;  
            }  
        }
    	
    	public static byte[] hexString2Bytes(String src) {  
            int l = src.length() / 2;  
            byte[] ret = new byte[l];  
            for (int i = 0; i < l; i++) {  
                ret[i] = (byte) Integer  
                        .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();  
            }  
            return ret;  
        } 
    
    	// btye转换hex函数
        public static String ByteToHex(byte[] byteArray) {
            StringBuffer StrBuff = new StringBuffer();
            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                    StrBuff.append("0").append(
                            Integer.toHexString(0xFF & byteArray[i]));
                } else {
                    StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
                }
            }
            return StrBuff.toString();
        }
    
    }    
    

    方法一:

    $str = "abc";
    $key = "310A54B3C9C12920E1582E016F15DD441ACE8553769E8991";
    $decodedKey = pack("H*", $key);  //十六进制转成字符串
    $hash = bin2hex(hash_hmac("sha1", $str, $decodedKey, true));
    

      

    方法二:

    //字节数组转化为String类型的数据
    function toStr($bytes) {
        $str = '';
        foreach($bytes as $ch) {
            $str .= chr($ch);
        }
        return $str;
    }
    //字符串转十六进制
    function String2Hex($string){
        $hex='';
        for ($i=0; $i < strlen($string); $i++){
            $hex .= dechex(ord($string[$i]));
        }
        return $hex;
    }
    
    //十六进制转字符串
    function Hex2String($hex){
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2){
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }
    
    $str = "abc";
    $key = "310A54B3C9C12920E1582E016F15DD441ACE8553769E8991";
    $decodedKey = pack("H*", $key);  //十六进制转字符串
    $hash = hash_hmac("sha1", $str, $decodedKey, true);
    //字符串转字节数组,即ascii数组 $hashs = str_split($hash); foreach ($hashs as $index => $value) { if (ord($value) > 128) { $hashs[$index] = ord($value) - 128 * 2; } else { $hashs[$index] = ord($value); } } $signature = String2Hex(toStr($hashs)); echo $signature;

      

  • 相关阅读:
    centos“You don't have permission to access /index.html on this server.”
    使用yum来安装或卸载CentOS图形界面包
    PHP上IIS上显示服务器应用程序不可用
    Windows 2003 远程桌面连接数超过最大连接数终极解决方案
    perl清理电脑上重复的文件
    wordpress在IIS6上首页访问不了
    WordPress首页出现循环重定向解决办法四则(转载)
    windows server 2003 如何在远程链接时更改用户密码
    Ubuntu下安装php5gd库,支持jpg、png、gif等格式图片处理
    wordpress设置用163邮箱账号发送邮件
  • 原文地址:https://www.cnblogs.com/luojianqun/p/9375830.html
Copyright © 2011-2022 走看看