using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace Common.system
{
public class DataProvider
{
///3DES解密
public static string TripleDESDecrypt(string pToDecrypt, string sKey)
{
sKey = sKey.Substring(0, 24);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
{
Mode = CipherMode.ECB,
Padding = PaddingMode.Zeros
};
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = new byte[] { 0, 00, 00, 00, 00, 00, 00, 00, };
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray()).Replace("\0", "");
}
///
/// 返回一个时间戳到秒
///
///
public static long TimestampTotalSeconds()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timestr = Convert.ToInt64(ts.TotalSeconds);
return timestr;
}
///
/// 将字典类型序列化为json字符串
///
/// 字典key
/// 字典value
/// 要序列化的字典数据
/// json字符串
public static string SerializeDictionaryToJsonString(Dictionary dict)
{
if (dict.Count == 0)
{
return "";
}
string jsonStr = JsonConvert.SerializeObject(dict);
return jsonStr;
}
///
///调用Post接口
///
///
///
public static string HttpPost(string body, string Url)
{
try
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
catch (Exception ex)
{
// LogHelper.Instance.Debug($"POST接口连接失败,请求参数:{ex.Message}");
return "POST报错:" + ex.Message;
}
}
}
}