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