114 lines
3.9 KiB
C#
114 lines
3.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace VRS.Util
|
||
{
|
||
public static class AESHelper
|
||
{
|
||
/// <summary>
|
||
/// AES 加密
|
||
/// </summary>
|
||
/// <param name="content">待加密的字符串</param>
|
||
/// <param name="key">密文</param>
|
||
/// <returns></returns>
|
||
public static string AesEncrypt(string str, string key)
|
||
{
|
||
if (string.IsNullOrEmpty(str)) return null;
|
||
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
|
||
|
||
RijndaelManaged rm = new RijndaelManaged
|
||
{
|
||
Key = Encoding.UTF8.GetBytes(key),
|
||
Mode = CipherMode.ECB,
|
||
Padding = PaddingMode.PKCS7
|
||
};
|
||
|
||
ICryptoTransform cTransform = rm.CreateEncryptor();
|
||
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||
return BitConverter.ToString(resultArray).Replace("-", "");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校内密码加密
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public static string EncryptPassword(string plainText)
|
||
{
|
||
// AES 密钥(必须是 16、24 或 32 字节)
|
||
var key = Encoding.UTF8.GetBytes("kGBxaThxMCMGaysp");
|
||
// 初始化向量 IV(必须是 16 字节)
|
||
var iv = Encoding.UTF8.GetBytes("VXFxgTgcvSDEXHpC");
|
||
using (Aes aesAlg = Aes.Create())
|
||
{
|
||
aesAlg.Key = key;
|
||
aesAlg.IV = iv;
|
||
aesAlg.Mode = CipherMode.CBC;
|
||
aesAlg.Padding = PaddingMode.PKCS7;
|
||
|
||
// 创建加密器对象
|
||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
||
|
||
using (MemoryStream msEncrypt = new MemoryStream())
|
||
{
|
||
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||
{
|
||
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
|
||
{
|
||
// 写入加密后的数据到内存流中
|
||
swEncrypt.Write(plainText);
|
||
}
|
||
}
|
||
// 返回加密后的数据的 Base64 字符串
|
||
return Convert.ToBase64String(msEncrypt.ToArray());
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// AES 解密
|
||
/// </summary>
|
||
/// <param name="content">待解密的字符串</param>
|
||
/// <param name="key">密文</param>
|
||
/// <returns></returns>
|
||
public static string AesDecrypt(string content, string key)
|
||
{
|
||
if (string.IsNullOrEmpty(content)) return null;
|
||
Byte[] toEncryptArray = ConvertToByteArray(content);
|
||
|
||
RijndaelManaged rm = new RijndaelManaged
|
||
{
|
||
Key = Encoding.UTF8.GetBytes(key),
|
||
Mode = CipherMode.ECB,
|
||
Padding = PaddingMode.PKCS7
|
||
};
|
||
|
||
ICryptoTransform cTransform = rm.CreateDecryptor();
|
||
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||
|
||
return Encoding.UTF8.GetString(resultArray);
|
||
}
|
||
|
||
/// <summary>
|
||
/// hex字符串转为byte[]数组
|
||
/// </summary>
|
||
/// <param name="hexString">hex字符串</param>
|
||
/// <returns></returns>
|
||
private static byte[] ConvertToByteArray(string hexString)
|
||
{
|
||
int length = hexString.Length / 2;
|
||
byte[] bytes = new byte[length];
|
||
|
||
for (int i = 0; i < length; ++i)
|
||
{
|
||
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
||
}
|
||
|
||
return bytes;
|
||
}
|
||
}
|
||
} |