a
This commit is contained in:
57
02.珠海市供水有限公司/SAL_OUTSTOCK/Utils/AESHepler.cs
Normal file
57
02.珠海市供水有限公司/SAL_OUTSTOCK/Utils/AESHepler.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace SAL_OUTSTOCK.Utils
|
||||
{
|
||||
internal static class AESHepler
|
||||
{
|
||||
/// <summary>
|
||||
/// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
|
||||
/// </summary>
|
||||
/// <param name="EncryptStr">明文</param>
|
||||
/// <param name="Key">密钥</param>
|
||||
/// <returns>加密后base64编码的密文</returns>
|
||||
public static string AesEncryptorBase64(string EncryptStr, string Key)
|
||||
{
|
||||
try
|
||||
{
|
||||
//byte[] keyArray = Encoding.UTF8.GetBytes(Key);
|
||||
byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);
|
||||
|
||||
RijndaelManaged rDel = new RijndaelManaged();
|
||||
//rDel.Key = keyArray;
|
||||
rDel.Key = Encoding.UTF8.GetBytes(Key);
|
||||
rDel.Mode = CipherMode.ECB;
|
||||
rDel.Padding = PaddingMode.PKCS7;
|
||||
|
||||
ICryptoTransform cTransform = rDel.CreateEncryptor();
|
||||
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
private static byte[] hexStringToByteArray(string strHex)
|
||||
{
|
||||
strHex = strHex.Replace(" ", "");
|
||||
byte[] buffer = new byte[strHex.Length / 2];
|
||||
for (int i = 0; i < strHex.Length; i += 2)
|
||||
{
|
||||
buffer[i / 2] = (byte)Convert.ToByte(strHex.Substring(i, 2), 16);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user