226 lines
7.5 KiB
C#
226 lines
7.5 KiB
C#
using System;
|
||
using System.Configuration;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace HZQR.Common
|
||
{
|
||
/// <summary>
|
||
/// 商业集团会员系统相关方法
|
||
/// </summary>
|
||
public class ZJZCGCMemberHelper
|
||
{
|
||
#region 商业集团会员接口服务地址
|
||
internal const string _ApiURL = "https://hywx.zcgc.cn/api/open/do";
|
||
/// <summary>
|
||
/// 商业集团会员接口服务地址
|
||
/// </summary>
|
||
public static string ApiURL
|
||
{
|
||
get
|
||
{
|
||
if (ConfigurationManager.AppSettings["ApiURL"] == null)
|
||
{
|
||
return _ApiURL;
|
||
}
|
||
else
|
||
{
|
||
return ConfigurationManager.AppSettings["ApiURL"];
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 商业集团会员接口AppId
|
||
internal const string _AppId = "AK202209070148347162";
|
||
/// <summary>
|
||
/// 商业集团会员接口AppId
|
||
/// </summary>
|
||
public static string AppId
|
||
{
|
||
get
|
||
{
|
||
if (ConfigurationManager.AppSettings["AppId"] == null)
|
||
{
|
||
return _AppId;
|
||
}
|
||
else
|
||
{
|
||
return ConfigurationManager.AppSettings["AppId"];
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 商业集团会员接口AppSecret
|
||
internal const string _AppSecret = "0ae62dc278de2ada91808ac501e81171";
|
||
/// <summary>
|
||
/// 商业集团会员接口AppSecret
|
||
/// </summary>
|
||
public static string AppSecret
|
||
{
|
||
get
|
||
{
|
||
if (ConfigurationManager.AppSettings["AppSecret"] == null)
|
||
{
|
||
return _AppSecret;
|
||
}
|
||
else
|
||
{
|
||
return ConfigurationManager.AppSettings["AppSecret"];
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region AES加密
|
||
/// <summary>
|
||
/// AES加密
|
||
/// </summary>
|
||
/// <param name="text">明文</param>
|
||
/// <param name="key">密钥,长度为16的字符串</param>
|
||
/// <param name="iv">偏移量,长度为16的字符串</param>
|
||
/// <param name="isHex">
|
||
/// <para>True:输出Hex格式密文</para>
|
||
/// <para>False:输出Base64格式密文</para>
|
||
/// </param>
|
||
/// <returns>密文</returns>
|
||
public static string AESEncrypt(string text, string key, string iv = "", bool isHex = false)
|
||
{
|
||
if (key.Length > 16)
|
||
{
|
||
key = key.Substring(0, 16);
|
||
}
|
||
//密码转换Byte数据
|
||
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
|
||
//使用加密后的密钥对数据进行AES加密
|
||
AesManaged rijndaelCipher = new AesManaged
|
||
{
|
||
Mode = CipherMode.ECB,
|
||
Padding = PaddingMode.PKCS7,
|
||
KeySize = 128,
|
||
BlockSize = 128,
|
||
Key = pwdBytes
|
||
};
|
||
if (!string.IsNullOrWhiteSpace(iv))
|
||
{
|
||
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
|
||
rijndaelCipher.IV = ivBytes;//需要IV的启用这两句
|
||
}
|
||
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
|
||
byte[] plainText = Encoding.UTF8.GetBytes(text);
|
||
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
|
||
if (isHex)
|
||
return ToHex(cipherBytes);//输出为hex即启用此句,注释上一句
|
||
else
|
||
return Convert.ToBase64String(cipherBytes);//输出为Base64即启用此句,注释下一句
|
||
}
|
||
#endregion
|
||
|
||
#region AES解密
|
||
/// <summary>
|
||
/// AES解密
|
||
/// </summary>
|
||
/// <param name="text">密文</param>
|
||
/// <param name="key">密钥,长度为16的字符串</param>
|
||
/// <param name="iv">偏移量,长度为16的字符串</param>
|
||
/// <param name="isHex">
|
||
/// <para>True:输入Hex格式密文</para>
|
||
/// <para>False:输入Base64格式密文</para></param>
|
||
/// <returns>明文</returns>
|
||
public static string AESDecrypt(string text, string key, string iv = "", bool isHex = false)
|
||
{
|
||
if (key.Length > 16)
|
||
{
|
||
key = key.Substring(0, 16);
|
||
}
|
||
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
|
||
AesManaged rijndaelCipher = new AesManaged
|
||
{
|
||
Mode = CipherMode.ECB,
|
||
Padding = PaddingMode.PKCS7,
|
||
KeySize = 128,
|
||
BlockSize = 128,
|
||
Key = pwdBytes
|
||
};
|
||
|
||
if (!string.IsNullOrWhiteSpace(iv))
|
||
{
|
||
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
|
||
rijndaelCipher.IV = ivBytes;//需要IV的启用这两句
|
||
}
|
||
|
||
byte[] encryptedData;
|
||
if (isHex)
|
||
encryptedData = UnHex(text);//输出为hex即启用此句,注释上一句
|
||
else
|
||
encryptedData = Convert.FromBase64String(text);//输出为Base64即启用此句,注释下一句
|
||
|
||
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
|
||
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
|
||
return Encoding.UTF8.GetString(plainText);
|
||
}
|
||
#endregion
|
||
|
||
#region Hex与byte转码
|
||
/// <summary>
|
||
/// 从字符串转换到16进制表示的字符串
|
||
/// </summary>
|
||
/// <param name="bytes">需要转码的byte</param>
|
||
/// <returns>返回结果</returns>
|
||
private static string ToHex(byte[] bytes)
|
||
{
|
||
string str = string.Empty;
|
||
if (bytes != null || bytes.Length > 0)
|
||
{
|
||
for (int i = 0; i < bytes.Length; i++)
|
||
{
|
||
str += string.Format("{0:X2}", bytes[i]);
|
||
}
|
||
}
|
||
return str.ToLower();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从16进制转换成utf编码的字符串
|
||
/// </summary>
|
||
/// <param name="hex">需要转码的hex</param>
|
||
/// <returns></returns>
|
||
public static byte[] UnHex(string hex)
|
||
{
|
||
if (hex == null)
|
||
throw new ArgumentNullException("hex");
|
||
hex = hex.Replace(",", "");
|
||
hex = hex.Replace("\n", "");
|
||
hex = hex.Replace("\\", "");
|
||
hex = hex.Replace(" ", "");
|
||
if (hex.Length % 2 != 0)
|
||
{
|
||
hex += "20";//空格
|
||
throw new ArgumentException("hex is not a valid number!", "hex");
|
||
}
|
||
// 需要将 hex 转换成 byte 数组。
|
||
byte[] bytes = new byte[hex.Length / 2];
|
||
for (int i = 0; i < bytes.Length; i++)
|
||
{
|
||
try
|
||
{
|
||
// 每两个字符是一个 byte。
|
||
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
|
||
System.Globalization.NumberStyles.HexNumber);
|
||
}
|
||
catch
|
||
{
|
||
// Rethrow an exception with custom message.
|
||
throw new ArgumentException("hex is not a valid hex number!", "hex");
|
||
}
|
||
}
|
||
return bytes;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|