DES加密解密帮助类

    public class DESCrypto
{
/// <summary>
/// 初始化des实例秘钥及向量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static DESCryptoServiceProvider InitDESInstance(string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] salt = new byte[] { 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xF1 };
byte[] keyByte = Encoding.UTF8.GetBytes(key);
Rfc2898DeriveBytes rfcKey = new Rfc2898DeriveBytes(keyByte, salt, );
Rfc2898DeriveBytes rfcIv = new Rfc2898DeriveBytes("", salt, );
des.Key = rfcKey.GetBytes();
des.IV = rfcIv.GetBytes();
return des;
} #region 基于Base64的加密解密(不其他格式的加解密方法配合使用)
/// <summary>
/// 基于Base64
/// </summary>
/// <param name="plaintext"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESEncryptBase64(string plaintext,string key)
{
DESCryptoServiceProvider des = InitDESInstance(key);
byte[] dataByteArray = Encoding.UTF8.GetBytes(plaintext);
string ciphertext = "";
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
ciphertext = Convert.ToBase64String(ms.ToArray());
}
return ciphertext;
} /// <summary>
/// 基于Base64
/// </summary>
/// <param name="encrypt"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecryptBase64(string encrypt, string key)
{
DESCryptoServiceProvider des = InitDESInstance(key);
byte[] dataByteArray = Convert.FromBase64String(encrypt);
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
#endregion #region 基于2位十六进制格式的加密解密(不其他格式的加解密方法配合使用)
/// <summary>
/// 加密
/// </summary>
/// <param name="plaintext">明文</param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESEncrypt(string plaintext, string key)
{
DESCryptoServiceProvider des = InitDESInstance(key);
StringBuilder sb = new StringBuilder();
byte[] dataByteArray = Encoding.UTF8.GetBytes(plaintext);
string ciphertext = "";
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
//输出加密文本
foreach (byte b in ms.ToArray())
{
sb.AppendFormat("{0:X2}", b);
}
ciphertext = sb.ToString();
}
return ciphertext; } /// <summary>
/// 解密
/// </summary>
/// <param name="ciphertext">密文</param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecrypt(string ciphertext, string key)
{
DESCryptoServiceProvider des = InitDESInstance(key);
byte[] dataByteArray = new byte[ciphertext.Length / ];
for (int x = ; x < ciphertext.Length / ; x++)
{
int i = (Convert.ToInt32(ciphertext.Substring(x * , ), ));
dataByteArray[x] = (byte)i;
}
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
#endregion #region 文件加密解密(不其他格式的加解密方法配合使用)
/// <summary>
/// 加密文件
/// </summary>
/// <param name="sourceFile">文件的物理路径</param>
/// <param name="encryptFile">文件的物理路径</param>
/// <param name="key"></param>
public static void DESEncryptFile(string sourceFile, string encryptFile, string key)
{
if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(encryptFile)||!File.Exists(sourceFile)) return; DESCryptoServiceProvider des = InitDESInstance(key); using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
using (FileStream encryptStream = new FileStream(encryptFile, FileMode.Create, FileAccess.Write))
{
byte[] dataByteArray = new byte[sourceStream.Length];
sourceStream.Read(dataByteArray, , dataByteArray.Length);
using (CryptoStream cs = new CryptoStream(encryptStream, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
}
}
} /// <summary>
/// 解密文件
/// </summary>
/// <param name="encryptFile">文件的物理路径</param>
/// <param name="decryptFile">文件的物理路径</param>
/// <param name="key"></param>
public static void DESDecryptFile(string encryptFile, string decryptFile, string key)
{
if (string.IsNullOrEmpty(encryptFile) || string.IsNullOrEmpty(decryptFile) || !File.Exists(encryptFile))return; DESCryptoServiceProvider des = InitDESInstance(key); using (FileStream encryptStream = new FileStream(encryptFile, FileMode.Open, FileAccess.Read))
using (FileStream decryptStream = new FileStream(decryptFile, FileMode.Create, FileAccess.Write))
{
byte[] dataByteArray = new byte[encryptStream.Length];
encryptStream.Read(dataByteArray, , dataByteArray.Length);
using (CryptoStream cs = new CryptoStream(decryptStream, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, , dataByteArray.Length);
cs.FlushFinalBlock();
}
}
} /// <summary>
/// 加密文本并输出到文件
/// </summary>
/// <param name="plaintext"></param>
/// <param name="FileName">文件的物理路径</param>
/// <param name="Key"></param>
public static void DESEncryptTextToFile(string plaintext, string FileName, string key)
{
if (string.IsNullOrEmpty(FileName)) return; DESCryptoServiceProvider des = InitDESInstance(key);
byte[] dataByteArray = Encoding.UTF8.GetBytes(plaintext); using (FileStream fStream = File.Open(FileName, FileMode.Create))
using (CryptoStream cStream = new CryptoStream(fStream, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cStream.Write(dataByteArray, , dataByteArray.Length);
cStream.FlushFinalBlock();
}
} /// <summary>
/// 解密文件中的密文并返回
/// </summary>
/// <param name="FileName"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string DESDecryptTextFromFile(string FileName, string key)
{
if (string.IsNullOrEmpty(FileName)||!File.Exists(FileName)) return ""; DESCryptoServiceProvider des = InitDESInstance(key); using (FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate))
using (CryptoStream cStream = new CryptoStream(fStream, des.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sReader = new StreamReader(cStream))
{
string val = sReader.ReadToEnd();
return val;
}
}
#endregion
}

Tips:参考MSDN整理编写,比较全面。

上一篇:修改CKFinder上传路径


下一篇:BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对