using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using LitJson;
using Model;
using Vo.Admin;
namespace Util.Logic
{
public class ApiTestHelper
{
///
/// API源请求节点
///
/// 接口数据
/// 加密方式
/// 加密参数
/// 请求参数
/// 响应参数
/// HTTP响应
public static string apiTestRequest(ApiTreeInfo apiInfoData, string requestEncrypt, string requestEncryptParam, Dictionary requestParams, Dictionary headers)
{
// 收集请求参数
Dictionary requestParamsDict = new Dictionary();
foreach (var param in requestParams)
{
string label = param.Key;
string value = param.Value;
requestParamsDict.Add(label, value);
}
string requestMethod = apiInfoData.apiMethod; // HTTP方法
string requestUrl = apiInfoData.apiPath; // HTTP请求URL
Utils.WriteLog("请求地址:" + requestUrl, "请求接口");
Utils.WriteLog("请求方式:" + requestMethod, "请求接口");
string response = ""; // HTTP响应
if (requestMethod == "GET") // GET请求
{
// 处理GET请求参数
string requestParamString = Newtonsoft.Json.JsonConvert.SerializeObject(requestParamsDict);
Utils.WriteLog("请求参数:" + requestParamString, "请求接口");
if (requestEncrypt != "NO")
{
requestParamString = Encrypt(requestParamString, requestEncrypt, requestEncryptParam);
response += requestParamString + "|";
}
Utils.WriteLog("请求参数:" + requestParamString, "请求接口");
response += Function.GetWebRequest(requestUrl + "?value=" + requestParamString, headers, true); // 发送GET请求
}
else if (requestMethod == "POST" || requestMethod == "PUT") // POST/PUT请求
{
// 处理POST/PUT请求参数
string requestBody = Newtonsoft.Json.JsonConvert.SerializeObject(requestParamsDict);
Utils.WriteLog("请求参数:" + requestBody, "请求接口");
if (requestEncrypt != "NO")
{
requestBody = Encrypt(requestBody, requestEncrypt, requestEncryptParam);
response += requestBody + "|";
}
Utils.WriteLog("请求参数:" + requestBody, "请求接口");
response += Function.PostWebRequest(requestUrl, requestBody, headers, requestMethod, "application/json", true); // 发送POST/PUT请求
}
else if (requestMethod == "DELETE") // DELETE请求
{
// 处理DELETE请求参数
string requestValue = "0";
if(requestParamsDict.Keys.Count > 0)
{
requestValue = requestParamsDict[requestParamsDict.Keys?.FirstOrDefault() ?? ""];
}
Utils.WriteLog("请求参数:" + requestValue, "请求接口");
if (requestEncrypt != "NO")
{
requestValue = Encrypt(requestValue, requestEncrypt, requestEncryptParam);
response += requestValue + "|";
}
Utils.WriteLog("请求参数:" + requestValue, "请求接口");
requestUrl = Regex.Replace(requestUrl, "\\{.*?\\}", requestValue);
Utils.WriteLog("请求地址:" + requestUrl, "请求接口");
response += Function.DeleteWithBody(requestUrl, "", headers, true); // 发送DELETE请求
}
Utils.WriteLog("返回数据:" + response, "请求接口");
// 处理POST/PUT请求响应
return response;
}
///
/// 加密
///
/// 待加密字符串
/// 加密类型
/// 加密参数
/// 加密后字符串
private static string Encrypt(string str, string encryptType, string encryptParam)
{
if (encryptType == "AES")
{
JsonData jsonData = JsonMapper.ToObject(encryptParam);
return AesEncrypt(str, jsonData["key"].ToString(), jsonData["iv"].ToString(), jsonData["mode"].ToString(), jsonData["padding"].ToString());
}
else if (encryptType == "RSA")
{
JsonData jsonData = JsonMapper.ToObject(encryptParam);
return RsaEncrypt(str, jsonData["key"].ToString(), jsonData["kind"].ToString());
}
else if (encryptType == "DES")
{
JsonData jsonData = JsonMapper.ToObject(encryptParam);
return Dbconn.DesEncrypt(str, jsonData["key"].ToString());
}
return "";
}
///
/// AES加密
///
/// 待加密字符串
/// 密钥
/// 初始向量
/// 加密模式
/// 填充方式
/// 加密后字符串
public static string AesEncrypt(string str, string key, string iv, string mode = "CBC", string padding = "PKCS7")
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
CipherMode cipherMode = CipherMode.CBC;
PaddingMode paddingMode = PaddingMode.PKCS7;
if (mode == "CBC") cipherMode = CipherMode.CBC;
if (mode == "ECB") cipherMode = CipherMode.ECB;
if (mode == "CFB") cipherMode = CipherMode.CFB;
if (mode == "CTS") cipherMode = CipherMode.CTS;
if (padding == "PKCS7") paddingMode = PaddingMode.PKCS7;
if (padding == "None") paddingMode = PaddingMode.None;
if (padding == "Zeros") paddingMode = PaddingMode.Zeros;
if (padding == "ISO10126") paddingMode = PaddingMode.ISO10126;
if (padding == "ANSIX923") paddingMode = PaddingMode.ANSIX923;
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
IV = Encoding.UTF8.GetBytes(iv),
Mode = cipherMode,
Padding = paddingMode
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
string jsonData = Convert.ToBase64String(resultArray, 0, resultArray.Length);
return Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
}
public static string RsaEncrypt(string data, string key, string kind)
{
if (kind == "privateKey")
{
var toDecryptArray = Convert.FromBase64String(data);
var toKey = Convert.FromBase64String(key);
var rsa = RSA.Create();
rsa.ImportPkcs8PrivateKey(toKey, out _);
var encryptData = rsa.Encrypt(toDecryptArray, RSAEncryptionPadding.Pkcs1);
var result = Convert.ToBase64String(encryptData);
return result;
}
else if (kind == "publicKey")
{
var toDecryptArray = Convert.FromBase64String(data);
var toKey = Convert.FromBase64String(key);
var rsa = RSA.Create();
rsa.ImportRSAPublicKey(toKey, out _);
var encryptData = rsa.Encrypt(toDecryptArray, RSAEncryptionPadding.Pkcs1);
var result = Convert.ToBase64String(encryptData);
return result;
}
if (kind == "privateKey2")
{
return new RSAHelper(RSAType.RSA, System.Text.Encoding.UTF8, key).Encrypt(data);
}
else if (kind == "publicKey2")
{
return new RSAHelper(RSAType.RSA, System.Text.Encoding.UTF8, "", key).Encrypt(data);
}
return "";
}
///
/// 解密
///
/// 待解密字符串
/// 解密类型
/// 解密参数
/// 解密后字符串
private static string Decrypt(string str, string decryptType, string decryptParam)
{
if (decryptType == "AES")
{
JsonData jsonData = JsonMapper.ToObject(decryptParam);
return AesDecrypt(str, jsonData["key"].ToString(), jsonData["iv"].ToString(), jsonData["mode"].ToString(), jsonData["padding"].ToString());
}
else if (decryptType == "RSA")
{
JsonData jsonData = JsonMapper.ToObject(decryptParam);
return RsaDecrypt(str, jsonData["key"].ToString(), jsonData["kind"].ToString());
}
else if (decryptType == "DES")
{
JsonData jsonData = JsonMapper.ToObject(decryptParam);
return Dbconn.DesDecrypt(str, jsonData["key"].ToString());
}
return "";
}
///
/// AES解密
///
/// 待解密字符串
/// 密钥
/// 初始向量
/// 解密模式
/// 填充方式
/// 解密后字符串
public static string AesDecrypt(string str, string key, string iv, string mode = "CBC", string padding = "PKCS7")
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
CipherMode cipherMode = CipherMode.CBC;
PaddingMode paddingMode = PaddingMode.PKCS7;
if (mode == "CBC") cipherMode = CipherMode.CBC;
if (mode == "ECB") cipherMode = CipherMode.ECB;
if (mode == "CFB") cipherMode = CipherMode.CFB;
if (mode == "CTS") cipherMode = CipherMode.CTS;
if (padding == "PKCS7") paddingMode = PaddingMode.PKCS7;
if (padding == "None") paddingMode = PaddingMode.None;
if (padding == "Zeros") paddingMode = PaddingMode.Zeros;
if (padding == "ISO10126") paddingMode = PaddingMode.ISO10126;
if (padding == "ANSIX923") paddingMode = PaddingMode.ANSIX923;
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
IV = Encoding.UTF8.GetBytes(iv),
Mode = cipherMode,
Padding = paddingMode
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
public static string RsaDecrypt(string data, string key, string kind)
{
if (kind == "privateKey")
{
var toDecryptArray = Convert.FromBase64String(data);
var toKey = Convert.FromBase64String(key);
var rsa = RSA.Create();
rsa.ImportPkcs8PrivateKey(toKey, out _);
var encryptData = rsa.Decrypt(toDecryptArray, RSAEncryptionPadding.Pkcs1);
var result = Convert.ToBase64String(encryptData);
return result;
}
else if (kind == "publicKey")
{
var toDecryptArray = Convert.FromBase64String(data);
var toKey = Convert.FromBase64String(key);
var rsa = RSA.Create();
rsa.ImportRSAPublicKey(toKey, out _);
var encryptData = rsa.Decrypt(toDecryptArray, RSAEncryptionPadding.Pkcs1);
var result = Convert.ToBase64String(encryptData);
return result;
}
if (kind == "privateKey2")
{
return new RSAHelper(RSAType.RSA, System.Text.Encoding.UTF8, key).Decrypt(data);
}
else if (kind == "publicKey2")
{
return new RSAHelper(RSAType.RSA, System.Text.Encoding.UTF8, "", key).Decrypt(data);
}
return "";
}
}
}