具体请百度,下面为封装好的类(代码细节请参考msdn):
/// <summary>
///DES加密算法
/// </summary>
public class DES
{
public DES()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 对字符进行加密
/// </summary>
/// <param name="pToEncrypt">输入字符串</param>
/// <param name="sKey">密钥,要求为8个字节共64位</param>
/// <returns>返回加密结果</returns>
public static string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2} ", b);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// 对已加密字符密文解密
/// </summary>
/// <param name="pToDecrypt">已加密字符</param>
/// <param name="sKey">密钥,要求为8个字节共64位</param>
/// <returns>解密结果</returns>
public static string Decrypt(string pToDecrypt, string sKey)
{
pToDecrypt = pToDecrypt.Replace(" ", "");//加密后的密文两位一组,组与组之间以空格隔开
//这里要先去除所有空格
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
/// <summary>
/// 加密文件
/// </summary>
/// <param name="sInputFile">源文件</param>
/// <param name="sOutputFile">加密过后的文件</param>
/// <param name="sKey">密钥,要求为8个字节共64位</param>
/// <param name="sMsg">返回信息</param>
/// <returns>是否成功</returns>
public static bool EncryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
{
bool IsSuccess = true;
FileStream fsInput = null;
FileStream fsEncrypted = null;
CryptoStream cryptostream = null;
try
{
fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
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 = 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();
sMsg = "成功";
return true;
}
catch (Exception ex)
{
sMsg = ex.Message;
IsSuccess = true;
}
finally
{
try
{
if (cryptostream != null) cryptostream.Close();
if (fsInput != null) fsInput.Close();
if (fsEncrypted != null) fsEncrypted.Close();
}
catch
{
}
}
return IsSuccess;
}
/// <summary>
/// 解密文件
/// </summary>
/// <param name="sInputFilename">输入文件(已加密文件)路径</param>
/// <param name="sOutputFilename">解密后文件路径</param>
/// <param name="sKey">密钥,要求为8个字节共64位</param>
/// <param name="sMsg">返回信息</param>
/// <returns>是否成功</returns>
public static bool DecryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
{
bool IsSuccess = true;
FileStream fsInput = null;
FileStream fsDecrypted = null;
CryptoStream cryptostream = null;
try
{
fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
fsDecrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desdecrypt = des.CreateDecryptor();
cryptostream = new CryptoStream(fsDecrypted,
desdecrypt,
CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsDecrypted.Close();
sMsg = "成功";
return true;
}
catch (Exception ex)
{
sMsg = ex.Message;
IsSuccess = true;
}
finally
{
try
{
if (cryptostream != null) cryptostream.Close();
if (fsInput != null) fsInput.Close();
if (fsDecrypted != null) fsDecrypted.Close();
}
catch
{
}
}
return IsSuccess;
}
}