using System.Collections.Generic;
|
using System;
|
using System.Text;
|
using System.Security.Cryptography;
|
|
namespace HDL_ON.DAL.Net
|
{
|
|
public class RemoteRequestParameters
|
{
|
public string RequestVersion;
|
public int RequestSource;
|
public string LoginAccessToken;
|
public int RequestProtocolType;
|
|
public int HdlGatewayGatewayType = 0;
|
public bool IsRedirectSelectEmqServer = false;
|
/// <summary>
|
/// 平台类型字符串
|
/// </summary>
|
public string PlatformStr;
|
/// <summary>
|
/// 发布主题负载
|
/// </summary>
|
public string PublishPayloadJsonStr;
|
|
public string Mac = "";
|
public string GroupName = "";
|
}
|
|
public class MqttRemoteInfo
|
{
|
public List<RemoteMACInfo> pageData;
|
|
public int pageIndex = 0;
|
public int pageSize = 10;
|
public int totalCount = 3;
|
public int totalPages = 1;
|
public bool hasPreviousPage = false;
|
public bool hasNextPage = false;
|
}
|
|
public class MqttInfo
|
{
|
public string connEmqDomainPort;
|
public string connEmqClientId;
|
public string connEmqUserName;
|
public string connEmqPwd;
|
|
public List<RemoteMACInfo> AccountAllGateways;
|
|
}
|
|
public class RemoteMACInfo
|
{
|
public string mac;
|
public string macMark;
|
public string isValid;
|
public string aesKey;
|
public bool isNewBusproGateway;
|
public string groupName;
|
public string projectName;
|
public string userName;
|
|
public string clientId;
|
|
//app自定义数据
|
public string md5_mac_string;
|
public string LoginAccessToken;
|
}
|
|
namespace Shared.Securitys
|
{
|
public partial class EncryptionService
|
{
|
|
#region 加密
|
/// <summary>
|
/// 加密主题为Base64
|
/// </summary>
|
/// <param name="pToEncrypt"></param>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static string AesEncryptTopic(string pToEncrypt, string key)
|
{
|
if (string.IsNullOrEmpty(pToEncrypt)) return null;
|
if (string.IsNullOrEmpty(key)) return pToEncrypt;
|
//需要加密内容的明文流
|
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(pToEncrypt);
|
|
//配置AES加密Key(密钥、向量、模式、填充)
|
RijndaelManaged rm = new RijndaelManaged
|
{
|
Key = Encoding.UTF8.GetBytes(key),
|
IV = Encoding.UTF8.GetBytes(key),
|
Mode = CipherMode.CBC,
|
Padding = PaddingMode.PKCS7
|
};
|
|
//创建AES加密器对象
|
ICryptoTransform cTransform = rm.CreateEncryptor();
|
|
//使用AES将明文流转成密文字节数组
|
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
//将AES生成的密文字节数组转成Base64字符串
|
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
}
|
|
|
/// <summary>
|
/// 加密负载为二进制流
|
/// </summary>
|
/// <param name="toEncryptArray"></param>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static byte[] AesEncryptPayload(byte[] toEncryptArray, string key)
|
{
|
if (string.IsNullOrEmpty(key)) return toEncryptArray;
|
//配置AES加密Key(密钥、向量、模式、填充)
|
var rm = new RijndaelManaged
|
{
|
Key = Encoding.UTF8.GetBytes(key),
|
IV = Encoding.UTF8.GetBytes(key),
|
Mode = CipherMode.CBC,
|
Padding = PaddingMode.PKCS7
|
};
|
|
//创建AES加密器对象
|
var cTransform = rm.CreateEncryptor();
|
//使用AES将明文流转成密文字节数组
|
return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
}
|
#endregion
|
|
|
#region 解密
|
/// <summary>
|
/// 解密主题数据
|
/// </summary>
|
/// <param name="pToDecrypt"></param>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static string AesDecryptTopic(string pToDecrypt, string key)
|
{
|
//AES密文Base64转成字符串
|
Byte[] toEncryptArray = Convert.FromBase64String(pToDecrypt);
|
|
//配置AES加密Key(密钥、向量、模式、填充)
|
RijndaelManaged rm = new RijndaelManaged
|
{
|
Key = Encoding.UTF8.GetBytes(key),
|
IV = Encoding.UTF8.GetBytes(key),
|
Mode = CipherMode.CBC,
|
Padding = PaddingMode.PKCS7
|
};
|
|
//创建AES解密器对象
|
ICryptoTransform cTransform = rm.CreateDecryptor();
|
|
//使用AES将密文流转成明文的字节数组
|
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
//转成字符串
|
return Encoding.UTF8.GetString(resultArray);
|
}
|
|
/// <summary>
|
/// 采用Aes解密负载数据
|
/// </summary>
|
/// <param name="toEncryptArray"></param>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static byte[] AesDecryptPayload(byte[] toEncryptArray, string key)
|
{
|
//配置AES加密Key(密钥、向量、模式、填充)
|
var rm = new RijndaelManaged
|
{
|
Key = Encoding.UTF8.GetBytes(key),
|
IV = Encoding.UTF8.GetBytes(key),
|
Mode = CipherMode.CBC,
|
Padding = PaddingMode.PKCS7
|
};
|
|
//创建AES解密器对象
|
var cTransform = rm.CreateDecryptor();
|
|
//使用AES将密文流转成明文的字节数组
|
return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
}
|
#endregion
|
|
|
}
|
}
|
}
|