Home > Net >  A c # code for the great god turn to Python, especially CreateToken this method, thank you
A c # code for the great god turn to Python, especially CreateToken this method, thank you

Time:09-23

I tried in python encryption, the length of the results and the c # method of encryption is different
Great god to help me see,




using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Using System. Diagnostics;
using System.IO;
using System.Linq;
Using System.Net;
Using System. Security. Cryptography.
using System.Text;
Using System. The Threading;
using System.Threading.Tasks;

The namespace Test2
{
Public class Program
{
Public static void Main (string [] args)
{
Var app_key="XXXXXXX";
Var app_sec="XXXXXX";
Var app_id="XXXXXXX";
Var ts=(DateTime. Now. ToUniversalTime (). The Ticks - 621355968000000000)/10000000;
Var url=$/create/accesstoken?" T={ts} ";
"Var body=${{" app_id \ ": " {app_id} \ ", \ "app_key " : \ "\ {app_key}}}";
Var hashKey=${app_sec}} {ts ";
Var sign=CreateToken (${url} {body} ", hashKey);
Var reqUrl=$" https://openapi/create/accesstoken? T={ts} ";
Var header=new Dictionary (a);
The header. The Add (" X-ray tg - token ", ts. The ToString ());
The header. The Add (" x - tg - sign, "sign);
Var response=Post (reqUrl, body, the header);
Console. WriteLine ($" CreateTokenResponse: {response} ");
Var tokenResponse=JsonConvert. DeserializeObject (response);
Url=$/FlightChange/Add?" T={ts} ";
Body="{" AddType \ ": 0, " ChangeReason \ ": 0, " ChangeType \ ": " 1 \ ", \ "NewArrTime " : \ "the 2019-11-04 22:30:00 ", \ "NewDepTime " : \ "the 2019-11-04 18:55:00 ", \ "NewEndPort " : \ "LHW ", \ "NewFlightNo " : \ "DR6593 ", \ "NewStartPort " : \ "WNZ ", \ "OrgArrTime " : \ "the 2019-11-04 14:40:00 ", \ "OrgDepTime " : \ "the 2019-11-04 14:40:00 ", \ "OrgEndPort " : \ "LHW ", \ "OrgFlightNo " : \ "DR6593 ", \ "OrgStartPort " : \ "WNZ ", \ "RequestGuid " : \ "eefdfaee0ac047fda68d048183bda7c4 "} ";
HashKey=${app_sec} {tokenResponse. Access_token} ";
Sign=CreateToken (${url} {body} ", hashKey);
Console. WriteLine ($" AppSign: {signs} ");
The header=new Dictionary (a);
The header. The Add (" X-ray tg - token ", tokenResponse access_token);
The header. The Add (" x - tg - sign, "sign);
The header. The Add (" x - appkey ", app_key);
ReqUrl=$" https://openapi.air.ly.com/FlightChange/Add? T={ts} ";
The response=Post (reqUrl, body, the header);
Console. WriteLine ($" AppApplyResponse: {response} ");

The Console. ReadKey ();
}

///
///HTTP request POTS
///

///
///
///
///
Public static string Post (string requestUrl, string requestData, Dictionary Headers)
{
The string result=string. The Empty;

Try
{
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (requestUrl);
Byte [] data=https://bbs.csdn.net/topics/Encoding.GetEncoding (" utf-8 "). GetBytes (requestData);
Request. The Method="Post";
Request. ContentType="application/json";
If (headers!=null & amp; & Headers. Any ())
{
The foreach (var header in headers)
{
Request. The Headers [header. Key]=header. The Value;
}
}
Request. ContentLength=data. Length;
Request. The Timeout=100000;
Request. The KeepAlive=false;
Using (var requestStream=request. GetRequestStream ())
{
RequestStream. Write (data, 0, the data Length);
//response flow
HttpWebResponse m_Response=(HttpWebResponse) request. The method GetResponse ();
Var responseStream=m_Response. GetResponseStream ();
Var streamReader=new streamReader (responseStream, Encoding GetEncoding (" utf-8 "));
//return information
Result=streamReader. ReadToEnd ();
}
}
The catch (Exception ex)
{
Result=ex. The Message;
}

return result;
}


///
///hamcSHA256 encryption implementation
///

/// The token. & lt;/returns>
///
///
Public static string CreateToken (string message, string secret)
{
Secret=secret?? "";
Var encoding=new System. Text. UTF8Encoding (); and
Byte [] keyByte=encoding. GetBytes (secret);
Byte [] messageBytes=encoding. GetBytes (message);
Using (var hmacsha256=new hmacsha256 (keyByte))
{
Byte [] hashmessage=hmacsha256.Com puteHash (messageBytes);
Return BitConverter. ToString (hashmessage). Replace (" - ", ""). ToLower ();
}
}
}

Public class CreateToken
{
Public string access_token {get; The set; }
}
}

CodePudding user response:




# # # # # # # # # # # # #
The import hmac
The import base64

Def CreateToken (message, secret) :
nullnullnull
  •  Tags:  
  • C #
  • Related