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