收集几个用到的方法

  /// <summary>
 2        /// 颠倒字符串数组
 3         /// </summary>
 4        /// <param name="strSource">原始字符串数组</param>
 5        /// <returns>返回结果</returns>
 6        public string[] ReverseWord(string[] strSource)
 
7        {
 
8
 
9            //计算需要遍历的次数
10            int count = strSource.Length % 2 == 1 ? (strSource.Length - 1/ 2 : strSource.Length / 2;
11            //数组最后一位的index
12            int length = strSource.Length - 1;
13            //临时变量
14            string tempString = string.Empty;
15           
16            for (int i = 0; i < count; i++)
17            {
18                tempString = strSource[i];
19                strSource[i] = strSource[length - i];
20                strSource[length - i] = tempString;
21            }
22            return strSource;

23        }


ASP.NET 2.0 网页采集方法: 

 public string GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
 
2     {
 
3         MatchCollection m;
 
4         Regex r;
 
5         if (RightToLeft == true)
 
6         {
 
7             r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
 
8         }
 
9         else
10         {
11             r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
12         }
13         m = r.Matches(HtmlCode);
14         string[] MatchValue = new string[m.Count];
15         for (int i = 0; i < m.Count; i++)
16         {
17             MatchValue[i] = m[i].Groups[GroupKey].Value;
18         }
19         if (MatchValue.Length > 0)
20         {
21             return MatchValue[0].ToString().Trim();
22         }
23         else
24         {
25             return "";
26         }
27     }
28     public string SniffwebCodeReturnList(string code, string wordsBegin, string wordsEnd)
29     {
30         try
31         {
32             ArrayList urlList = new ArrayList();
33             //string NewsTitle = "";
34             Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
35             for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
36             {
37                 urlList.Add(match1.Groups["title"].ToString());
38             }
39             if (urlList.Count > 0)
40             {
41                 return urlList[0].ToString();
42             }
43             else
44             {
45                 return "";
46             }
47 
48         }
49         catch
50         {
51             return "";
52 
53         }
54 

55     } 


ASP.NET 2.0 加密 Cookies:

//加密 Cookies
//Response.Cookies["askCRM"]["USERID"] = Tools.Encrypt(strUserName.Trim(), Tools.myKey);
//解密
//string strUid = Request.Cookies["askCRM"]["USERID"];
//strUid = Tools.Decrypt(strUid, Tools.myKey);

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;

/// <summary>
/// Tools 的摘要说明


/// </summary>
/// 
namespace HuiTong.Common
{
    
public class Tools
    {


        
/// <summary>
        
/// 当前程序加密所使用的密钥

        
/// </summary>
        public static readonly string sKey = "w0x3hd89";

        
#region 加密方法
        
/// <summary>
        
/// 加密方法
        
/// </summary>
        
/// <param name="pToEncrypt">需要加密字符串</param>
        
/// <param name="sKey">密钥</param>
        
/// <returns>加密后的字符串</returns>
        public static string Encrypt(string pToEncrypt)
        {
            
try
            {
                DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
                
//把字符串放到byte数组中


                
//原来使用的UTF8编码,我改成Unicode编码了,不行
                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();
            }
            
catch (Exception ex)
            {
      
            }

            
return "";
        }
        
#endregion

        
#region 解密方法
        
/// <summary>
        
/// 解密方法
        
/// </summary>
        
/// <param name="pToDecrypt">需要解密的字符串</param>
        
/// <param name="sKey">密匙</param>
        
/// <returns>解密后的字符串</returns>
        public static string Decrypt(string pToDecrypt)
        {
            
try
            {
                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 * 22), 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();
                
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
                StringBuilder ret = new StringBuilder();
                
return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            
catch (Exception ex)
            {
 
            }
            
return "";
        }
        
#endregion
    }


 

posted @ 2008-09-23 11:55  TONYBINLJ  阅读(139)  评论(0编辑  收藏  举报