想了解AES的可以去看看下面这个帖子↓
AES加密算法的详细介绍与实现
封装的AES工具类
using System;
using System.Security.Cryptography;
using System.Text;
public class AESEncryptHelper
{
public enum BlockSizeMode
{
/// <summary>
/// key、iv 16位
/// </summary>
S128 = 128,
/// <summary>
/// key、iv 24位
/// </summary>
S192 = 192,
/// <summary>
/// key、iv 32位
/// </summary>
S256 = 256
}
//默认密钥
private string key = "[Nice/*YUIasee;]";
private string iv = "[Blm**^^QAQByte]";
private CipherMode cipherMode = CipherMode.ECB;
private PaddingMode paddingMode = PaddingMode.PKCS7;
private BlockSizeMode blockSizeMode = BlockSizeMode.S128;
/// <summary>
/// 使用默认数据构造
/// </summary>
public AESEncryptHelper()
{
}
/// <summary>
/// 使用自定义数据构造
/// </summary>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="cipherMode"></param>
/// <param name="paddingMode"></param>
/// <param name="blockSizeMode"></param>
public AESEncryptHelper(string key, string iv = "", CipherMode cipherMode = CipherMode.ECB, PaddingMode paddingMode = PaddingMode.PKCS7, BlockSizeMode blockSizeMode = BlockSizeMode.S128)
{
this.key = key;
this.cipherMode = cipherMode;
this.paddingMode = paddingMode;
this.blockSizeMode = blockSizeMode;
if (cipherMode != CipherMode.ECB)//ECB不用设置IV
{
if (!string.IsNullOrEmpty(iv))
{
this.iv = iv;
}
if (iv.Length != (int)blockSizeMode / 8)
{
throw new Exception("iv的长度必须和块大小的关系为 iv.length = blockSize / 8");
}
}
if (key.Length != (int)blockSizeMode / 8)
{
throw new Exception("key的长度必须和块大小的关系为 key.length = blockSize / 8");
}
}
/// <summary>
/// AES加密
/// </summary>
public string AESEncrypt(string value)
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] ivArray = Encoding.UTF8.GetBytes(iv);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(value);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.BlockSize = (int)blockSizeMode;
if (cipherMode != CipherMode.ECB)
{
rDel.IV = ivArray;
}
rDel.Mode = cipherMode;
rDel.Padding = paddingMode;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// AES解密
/// </summary>
public string AESDecrypt(string value)
{
try
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] ivArray = Encoding.UTF8.GetBytes(iv);
byte[] toEncryptArray = Convert.FromBase64String(value);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.BlockSize = (int)blockSizeMode;
if (cipherMode != CipherMode.ECB)
{
rDel.IV = ivArray;
}
rDel.Mode = cipherMode;
rDel.Padding = paddingMode;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
catch
{
return string.Empty;
}
}
}
调用
string str = "Blm";
AESEncryptHelper aesEnc = new AESEncryptHelper("BlmTest.........", "HR$2pIjHR$2pIj12");
string encStr = aesEnc.AESEncrypt(str);
Debug.Log("加密结果:" + encStr);
string resultStr = aesEnc.AESDecrypt(encStr);
Debug.Log("解密结果:" + resultStr);
效果展示
|