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