/// <summary>
/// 文件加密
/// </summary>
/// <param name="sInputFilename">未加密文件路径</param>
/// <param name="sOutputFilename">加密以后文件保存路径</param>
/// <param name="sKey">密钥</param>
public void EncryptFile(string sInputFilename,string sOutputFilename)
{
FileStream fsInput = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);
FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create,FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,desencrypt,CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}
/// <summary>
/// 解密文件
/// </summary>
/// <param name="sInputFilename">被加密过的文件路径</param>
/// <param name="sOutputFilename">被解密以后的文件路径</param>
/// <param name="sKey">密钥</param>
public void DecryptFile(string sInputFilename, string sOutputFilename,string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
// <summary>
/// DES加密
/// </summary>
/// <param name="pToEncrypt">要加密的字符串。</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>以Base64格式返回的加密字符串。</returns>
public string DESEncrypt(string strToEnCrypt,string key)
{
//创建DES
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
//将明文转换成8位的整数
byte[] clearText = Encoding.UTF8.GetBytes(strToEnCrypt);
//设置密钥
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
//设置初始化向量
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
//创建内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将内存流转连接到加密的流
using (CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write))
{
cs.Write(clearText,0,clearText.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strToDeCrypt">要解密的以Base64</param>
/// <param name="key">密钥,且必须为8位。</param>
/// <returns>已解密的字符串。</returns>
public string DESDecrypt(string strToDeCrypt,string key)
{
//将密文转换成8位整数数组
byte[] cryptText = Convert.FromBase64String(strToDeCrypt);
//创建DES
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
//设置密钥
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
//设置初始化向量
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
//创建内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将内存流连接到加密的流
using (CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write))
{
cs.Write(cryptText,0,cryptText.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
Des加密与解密
最新推荐文章于 2024-10-18 15:17:19 发布