130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
using System;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using WX.Interface.Security;
|
||
|
||
namespace WX.CRM.Common
|
||
{
|
||
public class AESHelper
|
||
{
|
||
public static string Decrypt(string text, string key)
|
||
{
|
||
try
|
||
{
|
||
byte[] encryptedData = strToHexByte(text);
|
||
RijndaelManaged rijndaelCipher = new RijndaelManaged();
|
||
rijndaelCipher.Key = Encoding.UTF8.GetBytes(key);
|
||
rijndaelCipher.Mode = CipherMode.ECB;
|
||
rijndaelCipher.Padding = PaddingMode.None;
|
||
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
|
||
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
|
||
string result = Encoding.Default.GetString(plainText);
|
||
return result;
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public static string Encrypt(string text, string key)
|
||
{
|
||
try
|
||
{
|
||
byte[] encryptedData = Encoding.UTF8.GetBytes(text);
|
||
encryptedData = padding(encryptedData);
|
||
RijndaelManaged rijndaelCipher = new RijndaelManaged();
|
||
rijndaelCipher.Key = Encoding.UTF8.GetBytes(key);
|
||
rijndaelCipher.Mode = CipherMode.ECB;
|
||
rijndaelCipher.Padding = PaddingMode.None;
|
||
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
|
||
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
|
||
return byteToHexStr(plainText);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
// 坐席那边的解密函数,拿过来用
|
||
public static string decyptData(string cryptograph, string clientId)
|
||
{
|
||
try
|
||
{
|
||
ClientKey client = ClientKey.GetClientKey(clientId);
|
||
if (client == null)
|
||
throw new Exception("非法客户端访问");
|
||
var accessKey = client.AccessKey;
|
||
SymmetricAlgorithm des = new DESCryptoServiceProvider();
|
||
|
||
Encoding utf = new UTF8Encoding();
|
||
byte[] key = utf.GetBytes(accessKey);
|
||
byte[] iv = { 0x75, 0x70, 0x63, 0x68, 0x69, 0x6e, 0x61, 0x31 };
|
||
ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
|
||
byte[] encData = Convert.FromBase64String(cryptograph);
|
||
byte[] data = decryptor.TransformFinalBlock(encData, 0, encData.Length);
|
||
return utf.GetString(data);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
// 坐席那边的加密函数,拿过来用
|
||
public static string encyptData(string ciphertext, string clientId)
|
||
{
|
||
ClientKey client = ClientKey.GetClientKey(clientId);
|
||
if (client == null)
|
||
throw new Exception("非法客户端访问");
|
||
var accessKey = client.AccessKey;
|
||
SymmetricAlgorithm des = new DESCryptoServiceProvider();
|
||
|
||
Encoding utf = new UTF8Encoding();
|
||
byte[] key = utf.GetBytes(accessKey);
|
||
byte[] iv = { 0x75, 0x70, 0x63, 0x68, 0x69, 0x6e, 0x61, 0x31 };
|
||
ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
|
||
byte[] data = utf.GetBytes(ciphertext);
|
||
byte[] encData = encryptor.TransformFinalBlock(data, 0, data.Length);
|
||
return Convert.ToBase64String(encData);
|
||
}
|
||
private static byte[] strToHexByte(string hexString)
|
||
{
|
||
hexString = hexString.Replace(" ", "");
|
||
if ((hexString.Length % 2) != 0)
|
||
hexString += " ";
|
||
byte[] returnBytes = new byte[hexString.Length / 2];
|
||
for (int i = 0; i < returnBytes.Length; i++)
|
||
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
||
return returnBytes;
|
||
}
|
||
|
||
|
||
private static byte[] padding(byte[] src)
|
||
{
|
||
if (src.Length % 16 == 0)
|
||
{
|
||
return src;
|
||
}
|
||
byte[] buffer = new byte[src.Length / 16 * 16 + 16];
|
||
System.Array.Copy(src, 0, buffer, 0, src.Length);
|
||
return buffer;
|
||
}
|
||
|
||
private static string byteToHexStr(byte[] bytes)
|
||
{
|
||
string hexString = string.Empty;
|
||
if (bytes != null)
|
||
{
|
||
StringBuilder strB = new StringBuilder();
|
||
for (int i = 0; i < bytes.Length; i++)
|
||
{
|
||
strB.Append(bytes[i].ToString("X2"));
|
||
}
|
||
hexString = strB.ToString();
|
||
}
|
||
return hexString.ToLower();
|
||
}
|
||
}
|
||
}
|