Java and PHP hmac_sha1 results are different, ask the master to help

Source: Internet
Author: User
Tags hmac

Recently encountered HMAC_SHA1 cross-language encryption problem, only provided to the Java encryption file, did not provide PHP, I use PHP hmac_sha1 built-in functions, get the SIG encryption results are different, welcome the expert to help provide the corresponding PHP code, provide Java class as follows

Welcome to add 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);
}
/**
* Initialize 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);
                   String sig = new BigInteger (bytes). toString ();
                   System.out.println ("SIG:" + sig);

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
PHP I use the corresponding encryption that PHP comes with the function

$sig = Hash_hmac ("SHA1", $str, $key) The results of the sig from PHP are always different from that of Java, asking the master to help write the same code as the Java cryptographic value sig

This article is from the "Rosy Clouds Fly" blog, please be sure to keep this source http://caixia.blog.51cto.com/2266345/1631263

Java and PHP hmac_sha1 results are different, ask the master to help

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.