asp.net&C#各种加密码解密
<pre class="csharp" name="code"> #region DES加密解密
/// <summary>
/// DES加密算法必须使用Base64的Byte对象
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度必须为64位(byte[8]))</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
public string EnDES(string data, byte[] key, byte[] iv)
{
DES des = DES.Create();
//这行代码很重要,需要根据不同的字符串选择不同的转换格式
byte[] tmp = Encoding.Unicode.GetBytes(data);
byte[] encryptoData;
ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cs))
{
writer.Write(data);
writer.Flush();
writer.Dispose();
}
}
encryptoData = memoryStream.ToArray();
}
des.Dispose();
return Convert.ToBase64String(encryptoData);
}
/// <summary>
/// DES解密算法
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度必须为64位(byte[8]))</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
public string DeDES(string data, byte[] key, byte[] iv)
{
string resultData = string.Empty;
//这行代码很重要
byte[] temData = Convert.FromBase64String(data);//转换的格式挺重要
DES des = DES.Create();
ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
using (MemoryStream memoryStream = new MemoryStream(temData))
{
CryptoStream cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
StreamReader read = new StreamReader(cs);
resultData = read.ReadLine();
read.Dispose();
}
des.Dispose();
return resultData;
}
#endregion
#region TripleDES加密解密
/// <summary>
/// TripleDES加密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
public string EnTripleDES(string data, byte[] key, byte[] iv)
{
byte[] tmp;
byte[] tmpData = Encoding.Unicode.GetBytes(data);
TripleDES tripleDes = TripleDES.Create();
ICryptoTransform encryptor = tripleDes.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Flush();//这句很重要,在对流操作结束后必须用这句话强制将缓冲区中的数据全部写入到目标对象中
writer.Dispose();
}
tmp = ms.ToArray();
}
tripleDes.Dispose();
return Convert.ToBase64String(tmp);
}
/// <summary>
/// TripleDES解密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
public string DeTripleDES(string data, byte[] key, byte[] iv)
{
byte[] tmp = Convert.FromBase64String(data);
string result = string.Empty;
TripleDES tripleDES = TripleDES.Create();
ICryptoTransform decryptor = tripleDES.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(tmp))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
reader.Dispose();
}
}
tripleDES.Dispose();
return result;
}
#endregion
#region AES加密解密
/// <summary>
/// AES加密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
/// <returns>加密后的字符</returns>
public string EnAES(string data, byte[] key, byte[] iv)
{
Aes aes = Aes.Create();
byte[] tmp;
ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Flush();
writer.Dispose();
}
tmp = ms.ToArray();
}
aes.Dispose();
return Convert.ToBase64String(tmp);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
/// <returns>加密后的字符</returns>
public string DeAES(string data, byte[] key, byte[] iv)
{
byte[] tmp = Convert.FromBase64String(data);
string result = string.Empty;
Aes aes = Aes.Create();
ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(tmp))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
reader.Dispose();
}
}
aes.Dispose();
return result;
}
#endregion
#region Rijndael加密解密
/// <summary>
/// Rijndael加密
/// </summary>
/// <param name="data">需要加密的字符数据</param>
/// <param name="key">密匙,长度可以为:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度为128(byte[16])</param>
/// <returns>加密后的字符</returns>
public string EnRijndael(string data, byte[] key, byte[] iv)
{
Rijndael rijndeal = Rijndael.Create();
byte[] tmp;
ICryptoTransform encryptor = rijndeal.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Dispose();
}
tmp = ms.ToArray();
}
rijndeal.Dispose();
return Convert.ToBase64String(tmp);
}
public string DeRijndael(string data, byte[] key, byte[] iv)
{
byte[] tmp = Convert.FromBase64String(data);
string result = string.Empty;
Rijndael rijndael = Rijndael.Create();
ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(tmp))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
reader.Dispose();
}
}
return result;
}
#endregion