先定义一个key与iv
private string key = "GroupPower";
private string iv = "AFaneTi";
再定义一个加密对象private string iv = "AFaneTi";
private RijndaelManaged mobjCryptoService = new RijndaelManaged();
然后初始化Key与IV/// <summary>
/// 初始化密钥
/// </summary>
/// <returns>密钥字节数组</returns>
private byte[] GetLegalKey()
{
string result = "";
mobjCryptoService.GenerateKey();
byte[] buff = mobjCryptoService.Key;
if (key.Length < buff.Length)
{
result = key.PadRight(buff.Length, ' ');
}
else if (key.Length > buff.Length)
{
result = key.Substring(0, buff.Length);
}
else
{
result = key;
}
return Encoding.ASCII.GetBytes(result);
}
/// <summary>
/// 初始化向量IV
/// </summary>
/// <returns>IV向量字节数组</returns>
private byte[] GetLegalIV()
{
string result = "";
mobjCryptoService.GenerateIV();
byte[] buff = mobjCryptoService.IV;
if (iv.Length < buff.Length)
{
result = iv.PadRight(buff.Length, ' ');
}
else if (iv.Length > buff.Length)
{
result = iv.Substring(0, buff.Length);
}
else
{
result = iv;
}
return Encoding.ASCII.GetBytes(result);
}
下面是对称加密字符串/// 初始化密钥
/// </summary>
/// <returns>密钥字节数组</returns>
private byte[] GetLegalKey()
{
string result = "";
mobjCryptoService.GenerateKey();
byte[] buff = mobjCryptoService.Key;
if (key.Length < buff.Length)
{
result = key.PadRight(buff.Length, ' ');
}
else if (key.Length > buff.Length)
{
result = key.Substring(0, buff.Length);
}
else
{
result = key;
}
return Encoding.ASCII.GetBytes(result);
}
/// <summary>
/// 初始化向量IV
/// </summary>
/// <returns>IV向量字节数组</returns>
private byte[] GetLegalIV()
{
string result = "";
mobjCryptoService.GenerateIV();
byte[] buff = mobjCryptoService.IV;
if (iv.Length < buff.Length)
{
result = iv.PadRight(buff.Length, ' ');
}
else if (iv.Length > buff.Length)
{
result = iv.Substring(0, buff.Length);
}
else
{
result = iv;
}
return Encoding.ASCII.GetBytes(result);
}
/// <summary>
/// 对称加密字符串
/// </summary>
/// <param name="sourceStr">待加密的字符串</param>
/// <returns>加密后的字符串</returns>
public string EncryStr(string sourceStr)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(sourceStr);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
对字符串解密/// 对称加密字符串
/// </summary>
/// <param name="sourceStr">待加密的字符串</param>
/// <returns>加密后的字符串</returns>
public string EncryStr(string sourceStr)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(sourceStr);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// 对称解密字符串
/// </summary>
/// <param name="sourceStr">密文</param>
/// <returns>明文</returns>
public string DecryStr(string sourceStr)
{
try
{
byte[] bytIn = Convert.FromBase64String(sourceStr);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch
{
throw new Exception("源字符串格式不正确");
return null;
}
}
/// 对称解密字符串
/// </summary>
/// <param name="sourceStr">密文</param>
/// <returns>明文</returns>
public string DecryStr(string sourceStr)
{
try
{
byte[] bytIn = Convert.FromBase64String(sourceStr);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch
{
throw new Exception("源字符串格式不正确");
return null;
}
}