In many places request parameters need to be handled such as:
Steps
1. Parameter dictionary ordering.
2. Stitching characters.
/// <summary> ///Generate Signature/// </summary> /// <param name= "Paramlst" >parameter list</param> /// <param name= "Istoupper" >whether to turn uppercase</param> /// <param name= "Isdirect" >whether to add the signature key directly</param> /// <returns></returns> Public Static stringGeneratesign (list<keyvaluepair<string,string>> LST,stringPrivatekey) {Comparison<KeyValuePair<string,string>> comparer =Newcomparison<keyvaluepair<string,string>>(Comparekeyvaluepair); Lst. Sort (comparer); varParamlst = lst. Where (p =!)string. IsNullOrEmpty (P.value)). ToList (); stringSignstr =string. Join ("&", Paramlst. Select (p = p.key +"="+ p.value)) +Privatekey; returnSign (SIGNSTR,"UTF-8"); }
/// <summary> /// Compare sort positions by key /// </summary> Private Static int Comparekeyvaluepair (keyvaluepair<stringstring> P1, keyvaluepair<string string> p2) { returnstring. Compare (P1. Key, p2. Key); }
3.MD5 encryption
/// <summary> ///Signature String/// </summary> /// <param name= "Prestr" >a string that needs to be signed</param> /// <param name= "key" >secret key</param> /// <param name= "Inputcharset" >encoding Format</param> /// <returns>Signature Results</returns> Public Static stringSign (stringPRESTR,stringInputcharset) {StringBuilder SB=NewStringBuilder (); MD5 MD5=NewMD5CryptoServiceProvider (); byte[] t =Md5.computehash (encoding.getencoding (Inputcharset). GetBytes (PRESTR)); foreach(byteT1incht) {sb. Append (t1. ToString ("X2")); } returnsb. ToString (); }
Signature: Implements the parameter dictionary sort, then stitching as url Parameter form