標籤:java php hmac_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);
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);
String sig = new BigInteger(bytes).toString();
System.out.println("sig:" + sig);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
php我用相對應的加密即php內建函數
$sig = hash_hmac("sha1", $str, $key) php得出的sig結果總是和java不一樣,求高手幫忙寫一個與java加密值sig相同的代碼
本文出自 “彩霞飛飛” 部落格,請務必保留此出處http://caixia.blog.51cto.com/2266345/1631263
java和php的hmac_sha1結果不同,求高手幫忙