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

來源:互聯網
上載者:User
關鍵字 php 加密 java hmac sha1
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);

    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該如何處理

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.