Baidu AK Verification SN Generation algorithm __java

Source: Internet
Author: User
Tags md5

Reference Address:Http://developer.baidu.com/map/index.php?title=lbscloud/api/appendix

Assuming that Ak=yourak (Yourak is a string), the way to configure the AK in Http://lbsyun.baidu.com/apiconsole/key is to verify the SN signature, click the row of asterisks below to show the security key , you can see this AK corresponding SK private key, assuming Sk=yoursk.

The requested URL is: http://api.map.baidu.com/geocoder/v2/?address= Baidu mansion &output=json&ak=yourak&sn= 7de5a22212ffaa9e326444c75a58f9a0

After the SN is to be calculated, SK does not need to appear in the URL, but in the calculation of SN need SK (assuming Sk=yoursk).

The following is attached with the Java version code:

Package Com.boonya.baidu;
Import java.io.UnsupportedEncodingException;
Import Java.net.URLEncoder;
Import java.security.NoSuchAlgorithmException;
Import Java.util.LinkedHashMap;
Import Java.util.Map;
Import Java.util.Map.Entry; Java version Compute signature Signature public class Sncal {public static void main (string[] args) throws Unsupportedencodingexception, No

		suchalgorithmexception {sncal sncal = new sncal (); Computes the SN with the parameter to the order of occurrence, so save the <key,value&gt with Linkedhashmap, this method applies to get requests, and if the signature is generated for the URL that sent the POST request, make sure that the parameters are placed in the map in alphabetical order of key. Take request for example: http://api.map.baidu.com/geocoder/v2/?address= Baidu Mansion &output=json&ak=yourak,paramsmap first put address
		, then put the output, then put AK, put the order must be in the GET request of the corresponding parameters in the order of the occurrence of consistency.
		Map Paramsmap = new linkedhashmap<string, string> ();
		Paramsmap.put ("Address", "Baidu Mansion");
		Paramsmap.put ("Output", "json");

		Paramsmap.put ("AK", "Yourak"); The following toquerystring method is invoked to encode all the value in the Linkedhashmap as UTF8, and the concatenation returns the result address=%e7%99%be%e5%ba%a6%e5%a4%a7%e5%8e%a6& Output=json&ak=yourak String paramsstr = snCal.toquerystring (PARAMSMAP); On the paramsstr front stitching on the/geocoder/v2/, the back of the direct stitching Yoursk get/geocoder/v2/?address=%e7%99%be%e5%ba%a6%e5%a4%a7%e5%8e%a6&

		Output=json&ak=yourakyoursk string wholestr = new String ("/geocoder/v2/?" + paramsstr + "Yoursk");

		The above wholestr is then UTF8 encoded String tempstr = Urlencoder.encode (Wholestr, "UTF-8");
	The following MD5 method is invoked to obtain the final SN signature 7de5a22212ffaa9e326444c75a58f9a0 System.out.println (SNCAL.MD5 (TEMPSTR));
	///UTF8 code for all value in Map, stitching back results public String toquerystring (map<?,? > Data) throws Unsupportedencodingexception
		{StringBuffer querystring = new StringBuffer ();
			For (entry<?,? > Pair:data.entrySet ()) {Querystring.append (Pair.getkey () + "=");
		Querystring.append (Urlencoder.encode ((String) Pair.getvalue (), "UTF-8") + "&");
		} if (Querystring.length () > 0) {querystring.deletecharat (Querystring.length ()-1);
	return querystring.tostring (); ////The MD5 calculation method from StackOverflow, invokes the MessageDigest library function, and converts the byte array result to 16-in-system publiC String MD5 (string md5) {try {java.security.MessageDigest MD = java.security.MessageDigest.getInstance ("MD5");
			byte[] Array = md.digest (Md5.getbytes ());
			StringBuffer sb = new StringBuffer (); for (int i = 0; i < Array.Length ++i) {sb.append (integer.tohexstring (array[i) & 0xFF) | 0x100. SUBSTRING (
			1, 3));
		return sb.tostring ();
	catch (Java.security.NoSuchAlgorithmException e) {} return null; }}<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:12PX;" > </span>
In addition, this algorithm also provides php&c# version.

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.