Encryption-the hmac_sha1 results for java and php are different.

Source: Internet
Author: User
Recently, I encountered the problem of cross-language encryption in hmac_sha1. I only provided the java encrypted file and didn't provided php. I used the phphmac_sha1 built-in function to get different sig encryption results, welcome experts to help provide the corresponding php code. the java class is as follows. welcome to QQ: 847036019 publicabstractclassCoder {publicstaticfinalStringKEY_SHA & quot; SHA & quot; publicstaticfinalStringKEY_MD5 & quot; php encrypted javahmac sha1

Recently, I encountered the problem of hmac_sha1 cross-language encryption. I only provided the java encrypted file, but not php. I used the php hmac_sha1 built-in function to get different sig encryption results, you are welcome to provide the corresponding php code. the java class is as follows:

Welcome to 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', 'e', 'F '};
Public static final String KEY_MAC = "HmacSHA1 ";
Public static byte [] decryptBASE64 (String key) throws Exception {

Return (new BASE64Decoder (). decodeBuffer (key );

}

/**
* Initialize the HMAC key.
*/

Public static String initMacKey () throws Exception {

KeyGenerator keyGenerator = KeyGenerator. getInstance (KEY_MAC );

SecretKey secretKey = keyGenerator. generateKey (); return encryptBASE64 (secretKey. getEncoded ();}/*** HMAC encryption */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); // ['-000000','-000000', '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 () ;}} the result obtained by java sig is-653068794747578802236590292838260814592085857640.

Php I use the corresponding encryption, that is, the built-in function of 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 );
}
}
The ascii array obtained by php is $ array ('-115', '-101', '97 ','-26', '-80','-109 ', '-92', '33', '-6', '71', '-122', '-64','-17', '-29 ', '-101', '-53', '88', '-93', '-22', '-104 ');
How can I obtain the following values through php?
-653068794747578802236590292838260814592085857640
Java uses new BigInteger (bytes). toString (); php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.