优化后端框架
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
public class PublicData
|
||||
{
|
||||
/**
|
||||
* 公共报文组装
|
||||
*/
|
||||
public static string publicparam(string content,string platformCode ,
|
||||
string platformAlias,string privateKey,string password)
|
||||
{
|
||||
StringBuilder sign =new StringBuilder();
|
||||
string contentstr=EncryptDes.Encrypt3Des(content, password);
|
||||
sign.Append("content="+contentstr+"&");
|
||||
sign.Append("format=JSON&");
|
||||
sign.Append("platformCode="+platformCode+"&");
|
||||
string time = DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
string serianlNo = platformAlias + time + GenerateCheckCode(8);
|
||||
sign.Append("serialNo="+serianlNo+"&");
|
||||
sign.Append("signType=RSA&");
|
||||
string timestamp=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
sign.Append("timestamp="+timestamp+"&");
|
||||
sign.Append("version=1.0");
|
||||
string rsasign = RSA.sign(sign.ToString(), privateKey);
|
||||
Hashtable publictable=new Hashtable();
|
||||
publictable.Add("sign",rsasign);
|
||||
publictable.Add("format","JSON");
|
||||
publictable.Add("platformCode",platformCode);
|
||||
publictable.Add("serialNo",serianlNo);
|
||||
publictable.Add("signType","RSA");
|
||||
publictable.Add("timestamp",timestamp);
|
||||
publictable.Add("version","1.0");
|
||||
publictable.Add("content",contentstr);
|
||||
return ToJson.Table2Json(publictable);
|
||||
}
|
||||
|
||||
public static string disposeResponse(string str,string ptpublickey, string deskey)
|
||||
{
|
||||
|
||||
Hashtable strtable=new Hashtable();
|
||||
StringBuilder content=new StringBuilder();
|
||||
Hashtable dispose=new Hashtable();
|
||||
|
||||
string sign = "";
|
||||
string serialNo = "";
|
||||
str=str.Replace("{","").Replace("}","").Replace("\"","").Replace(",","&").Replace(":","&");
|
||||
string[] arraystr = str.Split('&');
|
||||
|
||||
for (int i = 0; i < arraystr.Length-1; i+=2)
|
||||
{
|
||||
if (arraystr[i]=="sign")
|
||||
{
|
||||
sign = arraystr[i + 1];
|
||||
}
|
||||
if (arraystr[i]=="serialNo")
|
||||
{
|
||||
serialNo=arraystr[i + 1];
|
||||
}
|
||||
strtable.Add(arraystr[i],arraystr[i+1]);
|
||||
}
|
||||
strtable.Remove("serialNo");
|
||||
strtable.Remove("sign");
|
||||
ArrayList ke = new ArrayList(strtable.Keys);
|
||||
ke.Sort();
|
||||
foreach (string tableEntry in ke)
|
||||
{
|
||||
content.Append(string.Format("{0}={1}&", tableEntry, strtable[tableEntry]));
|
||||
}
|
||||
content.Append("serialNo=" + serialNo);
|
||||
bool res=RSA.verify(content.ToString(), sign, ptpublickey, "UTF-8");
|
||||
Console.WriteLine(res);
|
||||
if (res)
|
||||
{
|
||||
for (int i = 0; i < arraystr.Length - 1; i += 2)
|
||||
{
|
||||
if (arraystr[i] == "content")
|
||||
{
|
||||
|
||||
arraystr[i+1]=EncryptDes.Decrypt3Des(arraystr[i+1],deskey);
|
||||
}
|
||||
dispose.Add(arraystr[i],arraystr[i+1]);
|
||||
|
||||
}
|
||||
return ToJson.Table2Json(dispose);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return "验签失败";
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 随机数生成
|
||||
*/
|
||||
private static string GenerateCheckCode(int codeCount)
|
||||
{
|
||||
int rep = 0;
|
||||
string str = string.Empty;
|
||||
long num2 = DateTime.Now.Ticks + rep;
|
||||
rep++;
|
||||
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
|
||||
for (int i = 0; i < codeCount; i++)
|
||||
{
|
||||
char ch;
|
||||
int num = random.Next();
|
||||
if ((num % 2) == 0)
|
||||
{
|
||||
ch = (char)(0x30 + ((ushort)(num % 10)));
|
||||
}
|
||||
else
|
||||
{
|
||||
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
|
||||
}
|
||||
str = str + ch.ToString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user