//随机选8个字节既为密钥也为初始向量
private static byte[] KEY_64 = { 42, 16, 93, 156, 78, 4, 218, 32 };
private static byte[] IV_64 = { 55, 103, 246, 79, 36, 99, 167, 3 };
//标准的DES加密
public static string Encrypt(string value)
{
if (value != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
//再转换为一个字符串
return Convert.ToBase64String(ms.GetBuffer(), 0, int.Parse(ms.Length.ToString())).ToString();
}
return "";
}
//标准的DES解密
public static string Decrypt(string value)
{
if (value != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
//从字符串转换为字节组
Byte[] buffer = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buffer);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return "";
//TRIPLE DES加密
public static string EncryptTripleDES(string value)
{
if (value != "")
{
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_192, IV_192), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
//再转换为一个字符串
return Convert.ToBase64String(ms.GetBuffer(), 0, int.Parse(ms.Length.ToString()));
}
return "";
}
//TRIPLE DES解密
public static string DecryptTripleDES(string value)
{
if (value != "")
{
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
//从字符串转换为字节组
Byte[] buffer = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buffer);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_192, IV_192), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return "";
}
private static byte[] KEY_64 = { 42, 16, 93, 156, 78, 4, 218, 32 };
private static byte[] IV_64 = { 55, 103, 246, 79, 36, 99, 167, 3 };
//标准的DES加密
public static string Encrypt(string value)
{
if (value != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
//再转换为一个字符串
return Convert.ToBase64String(ms.GetBuffer(), 0, int.Parse(ms.Length.ToString())).ToString();
}
return "";
}
//标准的DES解密
public static string Decrypt(string value)
{
if (value != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
//从字符串转换为字节组
Byte[] buffer = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buffer);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return "";
}
//对TripleDES,采取24字节或192位的密钥和初始向量
private static byte[] KEY_192 = { 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 250, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 };
private static byte[] IV_192 = { 55, 103, 246, 79, 36, 99, 167, 3, 42, 5, 62, 83, 184, 7, 209, 13, 145, 23, 200, 58, 173, 10, 121, 222 };
//TRIPLE DES加密
public static string EncryptTripleDES(string value)
{
if (value != "")
{
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_192, IV_192), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
//再转换为一个字符串
return Convert.ToBase64String(ms.GetBuffer(), 0, int.Parse(ms.Length.ToString()));
}
return "";
}
//TRIPLE DES解密
public static string DecryptTripleDES(string value)
{
if (value != "")
{
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
//从字符串转换为字节组
Byte[] buffer = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buffer);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_192, IV_192), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return "";
}