String ASCII Ordering

Source: Internet
Author: User

When docking third-party payment channels, the third party will require the parameters to be sorted from small to large ASCII code.

The following is a sample of the Java code that the Channel party generates for the signature rule:

//Initialize the 0010merkey.private file:String Merchantprivatekey; Merchantprivatekey=paycfg.getvalue ("0010merchantprivatekey"); //the data to be signed is passed to mapTreeMap map =NewTreeMap (); StringBuffer SBF=NewStringBuffer (); Map.put (OrderNo, '0010100000000011'); Map.put (productId, '0010'); //Signature value string requiredIterator Iterator =Map.keyset (). iterator ();  while(Iterator.hasnext ()) {Object key=Iterator.next (); //and stitching the obtained valuesString value= (String) map.get (key); SYSTEM.OUT.PRINTLN ("Map:" +key+ ": =======================" +value);              Sbf.append (String) map.get (key); }                        //Signature SignDataString SignData = SIGNANDVERIFY.SIGN_MD (sbf.tostring (), "", Merchantprivatekey); }

(1) Note: Initializes the private key, obtains the corresponding parameter value through the parameter name, and encode into the UTF-8 format

(2) by instantiating the map object,TreeMap map = new TreeMap();

(3) TreeMap () sorts the data names according to the size of the ASCII code values

(4) Loop the values in the map and stitch the values together = = "SBF"

(5) The Sign_md method of signandverify in the jar package is called to encrypt SBF.

Our own. NET program docking, the first thought is to use sorteddictionary<string,string>. The docking process found that some of the interfaces generated by the signature is correct, and some interface generated by the signature after the call received response message said the verification failed.

Further investigation, found that the ordered dictionary is not based on ASCII code, with the set of the Order () method, Array.Sort is also the case. At this point, the solution is to specify a parameter string.compareordinal when using Array.Sort. String.compareordinal will convert each character Mr. Foo to a corresponding value (such as a to a value of 97), and then compare the values.

[TestMethod] Public voidAsciisort () {IDictionary<string,string> dics =Newsorteddictionary<string,string>(); Dics. ADD ("21amount","Amount"); Dics. ADD ("2callback_url","Callback_url"); Dics. ADD ("Agoodsname","Goodsname"); Dics. ADD ("Amerchno","Merchno"); Dics. ADD ("Bnotify_url","Notify_url"); Dics. ADD ("Bordno","Ordno"); Dics. ADD ("3organno","Organno"); Dics. ADD ("version","version"); Dics. ADD ("PayType","PayType"); varArray =dics.        Keys.toarray ();    Console.WriteLine (); foreach(stringSinchArray) {Console.Write (S+"\ t"); }    //Array.SortArray.Sort (Array,string.        CompareOrdinal);    Console.WriteLine (); foreach(stringSinchArray) {Console.Write (S+"\ t"); }}

Test results:

Ref:http://www.cnblogs.com/similar/p/6739293.html

String ASCII Ordering

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.