最近遇到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);
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
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該如何處理
自己已經搞定,解決了哇!