Heim  >  Artikel  >  Backend-Entwicklung  >  加密-java和php的hmac_sha1结果不同,求高手帮忙

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

WBOY
WBOYOriginal
2016-06-02 11:32:311409Durchsuche

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该如何处理

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn