You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.3 KiB
C#

3 years ago
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Common.Helpers
{
/// <summary>
/// AES加解密工具类
/// </summary>
public class AESEncrypt
{
static string KeysString = "^%*7&%1)1~1`(UNIC):&*d|`></?s&/2";
/// <summary>
/// AES加密
/// </summary>
/// <param name="toEncrypt">要加密的内容</param>
/// <param name="strKey">密钥
/// <para>16个字节长(128位)</para>
/// <para>32个字节长(256位)</para>
/// <para>注一个汉字2个字节</para>
/// <para>为空时系统默认256位进行加/解密</para>
/// </param>
/// <returns>Base64转码后的密文</returns>
public static string Encrypt(string toEncrypt, string strKey)
{
if (strKey.Length != 16 && strKey.Length != 32)
strKey = KeysString;
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
string tmp = string.Empty;
RijndaelManaged rDel = new RijndaelManaged();//using System.Security.Cryptography;
try
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography;
rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography;
ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography;
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tmp = Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch (Exception ex)
{
tmp = ex.Message;
}
return tmp;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="toDecrypt">要解密的内容</param>
/// <param name="strKey">密钥
/// <para>16个字节长(128位)</para>
/// <para>32个字节长(256位)</para>
/// <para>注一个汉字2个字节</para>
/// <para>为空时系统默认256位进行加/解密</para>
/// </param>
/// <returns>解密后的明文</returns>
public static string Decrypt(string toDecrypt, string strKey)
{
if (strKey.Length != 16 && strKey.Length != 32)
strKey = KeysString;
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey);
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}