There are many encryption methods, the following is one of the simple signature patterns
1, first client through WEBAPI according to IP address, timestamp, random number generation signature, and pass serial number
Private Result_sign Valid ()
{
String ServerIP = "192.168.1.6";//HttpContext.Request.ServerVariables.Get ("Local_addr"). ToString (); Address
string timestamp = Datetimetostamp (DateTime.Now); //Time stamp
String nonce = ST.WEB.App_Start.Common.CreateValidateCode (6);//random number
String signstr = signaturestring (ServerIP, timestamp, nonce);//Generate signature
String appseq = configurationmanager.appsettings["Dpseq"]; Product serial Number
String Url = String. Format ("Http://www.abc.com:89/api/valid?signature={0}×tamp={1}&nonce={2}&appseq={3}", SignStr, Timestamp, nonce, appseq)//post send URL
String resstr = ST.WEB.App_Start.Common.Get_Http (URL, 12000);
Result_sign ResJson = new Result_sign ()
{
Code = "-1",
Message = ""
};
if (resstr.substring (0, 2)! = "Error")
{
ResJson = jsonconvert.deserializeobject<result_sign> (RESSTR);
}
return ResJson;
}
DateTime format conversion to UNIX timestamp format
private string Datetimetostamp (DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1));
return ((int) (time-starttime). totalseconds). ToString ();
}
Generate a signature string
private string Signaturestring (string appip, string timestamp, string nonce)
{
String[] Arrtmp = {APPIP, timestamp, nonce};
Array.Sort (ARRTMP);
String tmpstr = String. Join ("", arrtmp);
Tmpstr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpstr, "SHA1");
return Tmpstr.tolower ();
}
Generate random numbers
public static string Createvalidatecode (int length)
{
int[] randmembers = new Int[length];
int[] validatenums = new Int[length];
String validatenumberstr = "";
Generate a starting Sequence value
int seekseek = unchecked ((int) DateTime.Now.Ticks);
Random Seekrand = new Random (Seekseek);
int beginseek = (int) seekrand.next (0, Int32.maxvalue-length * 10000);
Int[] seeks = new Int[length];
for (int i = 0; i < length; i++)
{
Beginseek + = 10000;
Seeks[i] = Beginseek;
}
Generate random numbers
for (int i = 0; i < length; i++)
{
Random rand = new random (seeks[i]);
int pownum = 1 * (int) Math.pow (ten, length);
Randmembers[i] = rand. Next (Pownum, Int32.MaxValue);
}
Extracting random numbers
for (int i = 0; i < length; i++)
{
String numstr = Randmembers[i]. ToString ();
int numlength = Numstr.length;
Random rand = new Random ();
int numposition = rand. Next (0, numLength-1);
Validatenums[i] = Int32.Parse (numstr.substring (numposition, 1));
}
for (int i = 0; i < length; i++)
{
Validatenumberstr + = Validatenums[i]. ToString ();
}
return validatenumberstr;
}
<summary>
Get remote server ATN results
</summary>
<param name= "strURL" > Specify URL Path address </param>
<param name= "Timeout" > Time-out Settings </param>
<returns> Server ATN Results </returns>
public static string Get_http (string strurl, int timeout)
{
String strresult;
Try
{
HttpWebRequest Myreq = (HttpWebRequest) httpwebrequest.create (strURL);
Myreq.timeout = Timeout;
HttpWebResponse Httpwresp = (HttpWebResponse) myreq.getresponse ();
Stream MyStream = Httpwresp.getresponsestream ();
StreamReader sr = new StreamReader (MyStream, Encoding.default);
StringBuilder Strbuilder = new StringBuilder ();
while ( -1! = Sr. Peek ())
{
Strbuilder.append (Sr. ReadLine ());
}
strresult = Strbuilder.tostring ();
}
catch (Exception exp)
{
strresult = "Error:" + exp. Message;
}
return strresult;
}
2. The server side obtains the data and verifies the return result
[HttpGet]
Public result_sign sign (string signature, string timestamp, string nonce, String appseq)
{
Result_sign sign = new Result_sign ()
{
Code= "0",
Message= "Fault"
};
if (tool.validatesignature (signature, timestamp, nonce, appseq))
{
Sign.code = "1";
Sign.message = "Success";
}
return sign;
}
<summary>
Check data integrity for application access
</summary>
<param name= "signature" > Encrypt signed Content </param>
<param name= "timestamp" > Timestamp </param>
<param name= "Nonce" > Random string </param>
<param name= "Appseq" > Serial number </param>
<returns></returns>
public static bool Validatesignature (string signature, string timestamp, string nonce, String appseq)
{
BOOL result = FALSE;
Register item = CACHE.GETBYSEQ (APPSEQ);//Get Serial number related information
if (item! = NULL)
{
if (DateTime.Parse (item. Expiredt) < DateTime.Now.Date)//whether expired
{
return result;
}
#region verifying that the signature parameter is the correct source
String[] arrtmp = {item. IP, timestamp, nonce};
Array.Sort (ARRTMP);
String tmpstr = String. Join ("", arrtmp);
Tmpstr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpstr, "SHA1");
Tmpstr = Tmpstr.tolower ();
if (tmpstr = = Signature && isnumberic (timestamp))
{//Validation succeeded
DateTime dttime = stamptodatetime (timestamp);
Double minutes = DateTime.Now.Subtract (dttime). Totalminutes;
if (minutes < 5)//time cannot be greater than 5 minutes
{
result = true;
}
}
#endregion
}
return result;
}
<summary>
Time Stamp Turn time
</summary>
<param name= "TimeStamp" ></param>
<returns></returns>
private static DateTime Stamptodatetime (String timeStamp)
{
DateTime Datetimestart = TimeZone.CurrentTimeZone.ToLocalTime (New DateTime (1970, 1, 1));
Long ltime = long. Parse (TimeStamp + "0000000");
TimeSpan Tonow = new TimeSpan (Ltime);
Return Datetimestart.add (Tonow);
}
<summary>
is a number
</summary>
<param name= "Message" ></param>
<returns></returns>
protected static bool Isnumberic (String message)
{
System.Text.RegularExpressions.Regex Rex =
New System.Text.RegularExpressions.Regex (@ "^\d+$");
if (Rex. IsMatch (message))
{
return true;
}
Else
return false;
}
public class Result_sign
{
public string Code {set; get;}
public string Message {set; get;}
}
A simple way to implement. NET Signature encryption