VS2015,MVC开发环境下椭圆算法解密(ECC2)

入口API函数

public ActionResult PortalLogin(string token)
{
#if(DEBUG)
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJjY2lfY29kZSI6Ijk5OTkiLCJ1dWlkIjoiOTllMzVjMDctOGU4NC00NGMwLWI2OTUtMGFiMTk2NTBiODY4IiwiZW1haWwiOiJrYW5la29Ac21pbGUtd29ya3MuY28uanAiLCJwc2V1ZG9ueW0iOiJhYzIwM2I2MDhlNWJiMDc4Iiwicm9sZXMiOlsiY2NpIl0sImlzcyI6InBvcnRhbC5jY2ktamlneW8uanAiLCJpYXQiOjE0NzczNzU2MjB9.FQmHp5Z4_F-g79Njt8BgoxYt7WSabs0zbCqBpprNy8f8ANLzcuBE56El0pJLwCC6x28jeW8ZLqZq3WU_6mCKog";
#endif

try
{
string json = PortalEncode.GetJsonString(token);

if (!string.IsNullOrEmpty(json))
{
PortalInfo portInfo = JsonConvert.DeserializeObject<PortalInfo>(json);

if (_repository.IsValid(portInfo))
{

User userInfo = _repository.GetUserInfo(portInfo.CCI_CODE);

ActionResult rtnAction = Login(userInfo);

ReturnInfo jsonRtn = new ReturnInfo { user = userInfo.UserName, cookie = "CCI_HANBAISHI", redirect_to = "/retailmarketing/Home/Main" };

string strJsonRtn = JsonConvert.SerializeObject(jsonRtn);

return Content(strJsonRtn); }

}
}
catch (Exception ex)
{

return Content(string.Format(format, token));
}
return View();
}

public static class PortalEncode
{
static log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static string _token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJjY2lfY29kZSI6Ijk5OTkiLCJ1dWlkIjoiOTllMzVjMDctOGU4NC00NGMwLWI2OTUtMGFiMTk2NTBiODY4IiwiZW1haWwiOiJrYW5la29Ac21pbGUtd29ya3MuY28uanAiLCJwc2V1ZG9ueW0iOiJhYzIwM2I2MDhlNWJiMDc4Iiwicm9sZXMiOlsiY2NpIl0sImlzcyI6InBvcnRhbC5jY2ktamlneW8uanAiLCJpYXQiOjE0NzcyOTY5OTZ9.Bryo17_lDpyTLZHhLzRzbglhll4nfLmFcuLuVkQ2lUufsGPzfONWT14VzZQDmh6_V3ZyiREChq7d3xg7tccDLQ";

static string _x = "29:6d:bc: 92:d3: 41:84:24:b3: 68:3c: cd: 48:cc: 1a: ae: 3f:26:99:74:9f:f0: 6d:70:3a: be: 5f:5c: be: a3: f0: a6";
static string _y = "44:2a: cc: e4: 22:37:7e:cd: c5: 95:ab: 74:a1: 95:85:5e:4c: 21:5c: 56:f0: 50:22:fc: ca: 77:55:fb: 7a: 5c: 34:1a";

public static string GetJsonString(string token)
{
logger.Info("●●●●●●" + token);
if (string.IsNullOrEmpty(token)) token = _token;
try
{
CngKey cng = Ecc256Public();

string json = JWT.Decode(token, cng);

return json;
}
catch (Exception ex)
{
logger.Info(token + "|||" + ex);
throw ex;
}
}

private static CngKey Ecc256Public(CngKeyUsages usage = CngKeyUsages.Signing)
{
var listX = _x.Split(':');
var listY = _y.Split(':');
string keyA = string.Empty;
string keyB = string.Empty;

listX.ToList().ForEach(m => { keyA = keyA + m.Trim(); });
listY.ToList().ForEach(m => { keyB = keyB + m.Trim(); });

byte[] x = StringToByteArray(keyA);
byte[] y = StringToByteArray(keyB);

return EccKey.New(x, y, usage: usage);
}
private static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}

Love it, and you live without it
原文地址:https://www.cnblogs.com/tomclock/p/6006387.html