using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Business
{
/// <summary>
/// 加密类
/// </summary>
public class EncryptDes
{
/// <summary>
/// 解密连接字符串
/// </summary>
/// <param name="strInput">连接字符串</param>
/// <returns>解密后的连接字符串</returns>
public string Decrypt(string strInput)
{
bool bolSuccess = false;
strInput = strInput.Trim();
string strResult;
string strKey = "31415926";
string strIV = "31415926";
byte[] bytKey = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strKey);
byte[] bytIV = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strIV);
if ((strInput.Length > 0))
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
byte[] bytData = Convert.FromBase64String(strInput);
MemoryStream ms = new MemoryStream(bytData);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
strResult = sr.ReadToEnd();
bolSuccess = true;
}
catch (Exception ex)
{
strResult = strInput;
}
}
else
{
strResult = "";
}
return strResult;
}
/// <summary>
/// 加密连接字符串
/// </summary>
/// <param name="strInput">连接字符串</param>
/// <returns>加密后的连接字符串</returns>
public string Encrypt(string strInput)
{
bool bolSuccess = false;
strInput = strInput.Trim();
string strResult;
string strKey = "31415926";
string strIV = "31415926";
byte[] bytKey = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strKey);
byte[] bytIV = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strIV);
if ((strInput.Length > 0))
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(strInput);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
strResult = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
bolSuccess = true;
}
catch (Exception ex)
{
strResult = strInput;
}
}
else
{
strResult = "";
}
return strResult;
}
}
}