AES加密和解密
//AES加密
public static byte[] Encrypt(string str)
{
string key = " "; //秘钥根据自己的协议自行填写
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = strToToHexByte(str);
RijndaelManaged rm = new RijndaelManaged
{
Key = strToToHexByte(key),
Mode = CipherMode.ECB, //运算模式
Padding = PaddingMode.PKCS7 //pkcs7会填充成一个默认32个字节的数组
};
ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return resultArray;
}
//AES解密
public static byte[] Decrypt(string str)
{
string key = " "; //秘钥根据自己的协议自行填写
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = AES.strToToHexByte(str);
RijndaelManaged rm = new RijndaelManaged
{
Key = AES.strToToHexByte(key),
Mode = CipherMode.ECB, //运算模式
Padding = PaddingMode.PKCS7, //填充类型
};
ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return resultArray;
}