玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

常用函數(加密/解密/數組)

Posted on 2007-09-03 13:56 玄铁剑 阅读(366) 评论(0)  编辑 收藏 引用 所属分类: asp.netC# Winform

        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();
            }
        }

只有注册用户登录后才能发表评论。