添加项目文件。
This commit is contained in:
57
SAL_OUTSTOCK/Utils/AESHepler.cs
Normal file
57
SAL_OUTSTOCK/Utils/AESHepler.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace SAL_OUTSTOCK.Utils
|
||||
{
|
||||
internal static class AESHepler
|
||||
{
|
||||
/// <summary>
|
||||
/// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
|
||||
/// </summary>
|
||||
/// <param name="EncryptStr">明文</param>
|
||||
/// <param name="Key">密钥</param>
|
||||
/// <returns>加密后base64编码的密文</returns>
|
||||
public static string AesEncryptorBase64(string EncryptStr, string Key)
|
||||
{
|
||||
try
|
||||
{
|
||||
//byte[] keyArray = Encoding.UTF8.GetBytes(Key);
|
||||
byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);
|
||||
|
||||
RijndaelManaged rDel = new RijndaelManaged();
|
||||
//rDel.Key = keyArray;
|
||||
rDel.Key = Encoding.UTF8.GetBytes(Key);
|
||||
rDel.Mode = CipherMode.ECB;
|
||||
rDel.Padding = PaddingMode.PKCS7;
|
||||
|
||||
ICryptoTransform cTransform = rDel.CreateEncryptor();
|
||||
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
private static byte[] hexStringToByteArray(string strHex)
|
||||
{
|
||||
strHex = strHex.Replace(" ", "");
|
||||
byte[] buffer = new byte[strHex.Length / 2];
|
||||
for (int i = 0; i < strHex.Length; i += 2)
|
||||
{
|
||||
buffer[i / 2] = (byte)Convert.ToByte(strHex.Substring(i, 2), 16);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
SAL_OUTSTOCK/Utils/Base64Helper.cs
Normal file
74
SAL_OUTSTOCK/Utils/Base64Helper.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SAL_OUTSTOCK.Utils
|
||||
{
|
||||
|
||||
public static class Base64Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64加密,采用utf8编码方式加密
|
||||
/// </summary>
|
||||
/// <param name="source">待加密的明文</param>
|
||||
/// <returns>加密后的字符串</returns>
|
||||
public static string Base64Encode(string source)
|
||||
{
|
||||
return Base64Encode(Encoding.UTF8, source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64加密
|
||||
/// </summary>
|
||||
/// <param name="encodeType">加密采用的编码方式</param>
|
||||
/// <param name="source">待加密的明文</param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Encode(Encoding encodeType, string source)
|
||||
{
|
||||
string encode = string.Empty;
|
||||
byte[] bytes = encodeType.GetBytes(source);
|
||||
try
|
||||
{
|
||||
encode = Convert.ToBase64String(bytes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
encode = source;
|
||||
}
|
||||
return encode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解密,采用utf8编码方式解密
|
||||
/// </summary>
|
||||
/// <param name="result">待解密的密文</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
public static string Base64Decode(string result)
|
||||
{
|
||||
return Base64Decode(Encoding.UTF8, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解密
|
||||
/// </summary>
|
||||
/// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
|
||||
/// <param name="result">待解密的密文</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
public static string Base64Decode(Encoding encodeType, string result)
|
||||
{
|
||||
string decode = string.Empty;
|
||||
byte[] bytes = Convert.FromBase64String(result);
|
||||
try
|
||||
{
|
||||
decode = encodeType.GetString(bytes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
decode = result;
|
||||
}
|
||||
return decode;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
99
SAL_OUTSTOCK/Utils/HttpWebHelper.cs
Normal file
99
SAL_OUTSTOCK/Utils/HttpWebHelper.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Security;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace SAL_OUTSTOCK.Utils
|
||||
{
|
||||
public static class HttpWebHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// post请求
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="postData">请求数据</param>
|
||||
/// <param name="certificate2">证书</param>
|
||||
/// <returns></returns>
|
||||
public static string DoPost(string url, string postData, X509Certificate2 certificate2)
|
||||
{
|
||||
try
|
||||
{
|
||||
string result = string.Empty;
|
||||
|
||||
HttpWebRequest request = null;
|
||||
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request = WebRequest.Create(url) as HttpWebRequest;
|
||||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||||
request.ProtocolVersion = HttpVersion.Version11;
|
||||
// 这里设置了协议类型。
|
||||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
|
||||
request.KeepAlive = false;
|
||||
ServicePointManager.CheckCertificateRevocationList = true;
|
||||
ServicePointManager.DefaultConnectionLimit = 100;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
request = (HttpWebRequest)WebRequest.Create(url);
|
||||
}
|
||||
|
||||
//string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||
|
||||
////证书
|
||||
//var keystorefile = baseDirectory + @"\bin\ISSUE\testISSUE.pfx";
|
||||
//var key = "123456";
|
||||
//var cer = new X509Certificate2(keystorefile, key);
|
||||
|
||||
if (certificate2 != null)
|
||||
request.ClientCertificates.Add(certificate2);
|
||||
|
||||
request.Method = "POST"; //使用get方式发送数据
|
||||
request.ContentType = "application/json;charset=utf-8";
|
||||
|
||||
byte[] data = Encoding.UTF8.GetBytes(postData);
|
||||
Stream newStream = request.GetRequestStream();
|
||||
newStream.Write(data, 0, data.Length);
|
||||
newStream.Close();
|
||||
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
Stream stream = response.GetResponseStream();
|
||||
|
||||
using (StreamReader sr = new StreamReader(stream))
|
||||
{
|
||||
result = sr.ReadToEnd();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// post请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <returns></returns>
|
||||
public static string DoPost(string url, string postData)
|
||||
{
|
||||
return DoPost(url, postData, null);
|
||||
}
|
||||
|
||||
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||||
{
|
||||
if (errors == SslPolicyErrors.None)
|
||||
return true; //总是接受
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
SAL_OUTSTOCK/Utils/SHA256Helper.cs
Normal file
44
SAL_OUTSTOCK/Utils/SHA256Helper.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace SAL_OUTSTOCK.Utils
|
||||
{
|
||||
internal static class SHA256Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// SHA256加密
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static string SHA256EncryptString(string data)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||||
byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < hash.Length; i++)
|
||||
{
|
||||
builder.Append(hash[i].ToString("x2"));
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SHA256加密
|
||||
/// </summary>
|
||||
/// <param name="StrIn">待加密字符串</param>
|
||||
/// <returns>加密数组</returns>
|
||||
public static Byte[] SHA256EncryptByte(string StrIn)
|
||||
{
|
||||
var sha256 = new SHA256Managed();
|
||||
var Asc = new ASCIIEncoding();
|
||||
var tmpByte = Asc.GetBytes(StrIn);
|
||||
var EncryptBytes = sha256.ComputeHash(tmpByte);
|
||||
sha256.Clear();
|
||||
return EncryptBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user