加密-java和php的hmac_sha1结果不同,求高手帮忙

WBOY
发布: 2016-06-02 11:32:31
原创
1436 人浏览过

php加密javahmac sha1

最近遇到hmac_sha1跨语言加密的问题,只提供给了java加密文件,没提供php的,我用php hmac_sha1内置函数,得到的sig加密结果不同,欢迎高手帮忙提供相对应的php代码,提供的java类如下

欢迎加QQ: 847036019

public abstract class Coder {

public static final String KEY_SHA = "SHA";

public static final String KEY_MD5 = "MD5";

public static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
public static final String KEY_MAC = "HmacSHA1";
public static byte[] decryptBASE64(String key) throws Exception {

return (new BASE64Decoder()).decodeBuffer(key);

}

/**
* 初始化HMAC密钥
*/

public static String initMacKey() throws Exception {

KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);

<code>    SecretKey secretKey = keyGenerator.generateKey();      return encryptBASE64(secretKey.getEncoded());  }      /**     * HMAC加密     */      public static byte[] encryptHMAC(byte[] data, String key) throws Exception {          SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);          Mac mac = Mac.getInstance(secretKey.getAlgorithm());          mac.init(secretKey);          return mac.doFinal(data);      }      public static void main(String[] args) {             try {               String param = '';               String appkey = '';                byte[] bytes = Coder.encryptHMAC((param).getBytes("utf-8"), appkey);//['-115','-101','97','-26','-80','-109','-92','33','-6','71','-122','-64','-17','-29','-101','-53','88','-93','-22','-104']               String sig = new BigInteger(bytes).toString();//-653068794747578802236590292838260814592085857640               System.out.println("sig:" + sig);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }    }}    java sig得出来的结果为    -653068794747578802236590292838260814592085857640</code>
登录后复制

php我用相对应的加密即php自带函数
$signature = mhash(MHASH_SHA1,$sigstr,base64_decode($appkey));
$hash = str_split($signature);
foreach ($hash as $index=>$value) {
if (ord($value)>128) {
$hash[$index] = ord($value)-128*2;
} else {
$hash[$index] = ord($value);
}
}
php得到的ascii数组为$array('-115','-101','97','-26','-80','-109','-92','33','-6','71','-122','-64','-17','-29','-101','-53','88','-93','-22','-104');
请问该php如何处理得到下面的值
-653068794747578802236590292838260814592085857640
java用的是new BigInteger(bytes).toString();php该如何处理

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板