using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Common
![](http://static.javashuo.com/static/loading.gif)
...{
![](http://static.javashuo.com/static/loading.gif)
/**
//// <summary>
/// DESEncrypt加密解密算法。
/// </summary>
public
sealed
class DESEncrypt
![](http://static.javashuo.com/static/loading.gif)
...{
private DESEncrypt()
![](http://static.javashuo.com/static/loading.gif)
...{
//
// TODO: 在此處添加構造函數邏輯
//
![](http://static.javashuo.com/static/loading.gif)
}
private
static
string key =
"zhoufoxcn";
![](http://static.javashuo.com/static/loading.gif)
/**
//// <summary>
/// 對稱加密解密的密鑰
/// </summary>
public
static
string Key
![](http://static.javashuo.com/static/loading.gif)
...{
![](http://static.javashuo.com/static/loading.gif)
get
![](http://static.javashuo.com/static/loading.gif)
...{
return key;
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
set
![](http://static.javashuo.com/static/loading.gif)
...{
![](http://static.javashuo.com/static/loading.gif)
key = value;
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
/**
//// <summary>
/// DES加密
/// </summary>
/// <param name="encryptString"></param>
/// <returns></returns>
public
static
string DesEncrypt(
string encryptString)
![](http://static.javashuo.com/static/loading.gif)
...{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
![](http://static.javashuo.com/static/loading.gif)
DESCryptoServiceProvider provider =
new DESCryptoServiceProvider();
![](http://static.javashuo.com/static/loading.gif)
MemoryStream mStream =
new MemoryStream();
![](http://static.javashuo.com/static/loading.gif)
CryptoStream cStream =
new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
![](http://static.javashuo.com/static/loading.gif)
cStream.Write(inputByteArray, 0, inputByteArray.Length);
![](http://static.javashuo.com/static/loading.gif)
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
/**
//// <summary>
/// DES解密
/// </summary>
/// <param name="decryptString"></param>
/// <returns></returns>
public
static
string DesDecrypt(
string decryptString)
![](http://static.javashuo.com/static/loading.gif)
...{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
![](http://static.javashuo.com/static/loading.gif)
DESCryptoServiceProvider provider =
new DESCryptoServiceProvider();
![](http://static.javashuo.com/static/loading.gif)
MemoryStream mStream =
new MemoryStream();
![](http://static.javashuo.com/static/loading.gif)
CryptoStream cStream =
new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
![](http://static.javashuo.com/static/loading.gif)
cStream.Write(inputByteArray, 0, inputByteArray.Length);
![](http://static.javashuo.com/static/loading.gif)
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}