using System;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
namespace HZQR.Common
{
///
/// 商业集团会员系统相关方法
///
public class ZJZCGCMemberHelper
{
#region 商业集团会员接口服务地址
internal const string _ApiURL = "https://hywx.zcgc.cn/api/open/do";
///
/// 商业集团会员接口服务地址
///
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";
///
/// 商业集团会员接口AppId
///
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";
///
/// 商业集团会员接口AppSecret
///
public static string AppSecret
{
get
{
if (ConfigurationManager.AppSettings["AppSecret"] == null)
{
return _AppSecret;
}
else
{
return ConfigurationManager.AppSettings["AppSecret"];
}
}
}
#endregion
#region AES加密
///
/// AES加密
///
/// 明文
/// 密钥,长度为16的字符串
/// 偏移量,长度为16的字符串
///
/// True:输出Hex格式密文
/// False:输出Base64格式密文
///
/// 密文
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解密
///
/// AES解密
///
/// 密文
/// 密钥,长度为16的字符串
/// 偏移量,长度为16的字符串
///
/// True:输入Hex格式密文
/// False:输入Base64格式密文
/// 明文
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转码
///
/// 从字符串转换到16进制表示的字符串
///
/// 需要转码的byte
/// 返回结果
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();
}
///
/// 从16进制转换成utf编码的字符串
///
/// 需要转码的hex
///
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
}
}