static public string EncryptText(string strData)
{
if (strData.Trim().Equals(""))
return "";
string strKey = "12345678";
byte[] arryKey = ASCIIEncoding.ASCII.GetBytes(strKey);
byte[] arrayIV = ASCIIEncoding.ASCII.GetBytes(strKey);
try
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream =
new CryptoStream(mStream, new DESCryptoServiceProvider().CreateEncryptor(arryKey, arrayIV),
CryptoStreamMode.Write);
byte[] toEncrypt = Encoding.Default.GetBytes(strData);
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in mStream.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
cStream.Close();
mStream.Close();
return ret.ToString();
}
catch (CryptographicException ex)
{
return "";
}
}
static public string DecryptText(string strData)
{
if (strData.Trim().Equals(""))
return "";
string strKey = "12345678";
byte[] arrayData = new byte[strData.Length / 2];
for (int x = 0; x < strData.Length / 2; x++)
{
int i = (Convert.ToInt32(strData.Substring(x * 2, 2), 16));
arrayData[x] = (byte)i;
}
byte[] arryKey = ASCIIEncoding.ASCII.GetBytes(strKey);
byte[] arrayIV = ASCIIEncoding.ASCII.GetBytes(strKey);
try
{
MemoryStream msDecrypt = new MemoryStream(arrayData);
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(arryKey,
arrayIV),
CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[arrayData.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return Encoding.Default.GetString(fromEncrypt, 0, fromEncrypt.Length - 1).TrimEnd('\0');
}
catch (CryptographicException ex)
{
return "";
}
}
static public object[] ConvertToArray(string strValue,char splitChar)
{
object[] split = strValue.Split(new Char[] { splitChar });
return split;
}
static public string StrArrayToString(string values)
{
string strResult = "";
try
{
object[] items = ConvertToArray(values, '\n');
foreach (object obj in items)
{
strResult = strResult + obj.ToString().Replace("'"," ") + "\\n";
}
}
catch
{
if (values != null)
{
strResult = values.ToString().Replace("'", " ") + "\\n";
}
}
return strResult;
}
static public string GetClientIP()
{
if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
}