//加密
 private string EncodeHex( string sourcecode )
  {
   string result = "" ;
           
   for( int i = 0 ; i < sourcecode.Length ; i ++ )
   {
    if( sourcecode[i] <= 255 )
    {
     result += Convert.ToInt32( sourcecode[i] ).ToString( "X" ) ;
    }
    else
    {
     byte[] temp = Encoding.Default.GetBytes( sourcecode[i].ToString() ) ;

     for( int j = 0 ; j < temp.Length ; j ++ )
     {
      result += temp[j].ToString( "X" ) ;
     }
    }

   }

   return result ;
  }

  //解密
  private string DecodeHex( string sourcecode )
  {
   string result = "" ;
   byte[] buffer = null ;
            int i = 0 ;
 
   while( i < sourcecode.Length )
   {
    byte temp = (byte)HexToByte( sourcecode.Substring( i , 2 ) ) ;

    if( temp < 129 )
    {
     buffer = new byte[1] ;
     buffer[0] = temp ;
     result += Encoding.Default.GetString( buffer );
     i += 2 ;
    }
    else
    {
     buffer = new byte[2] ;
     byte temp1 = (byte)HexToByte( sourcecode.Substring( i + 2 , 2 ) ) ;

     buffer[0] = temp ;
     buffer[1] = temp1 ;

     result += Encoding.Default.GetString( buffer ) ;
     i += 4 ;
    }
   }

   return result ;
  } 
  private int HexToByte( string sourcecode )
  {
   int result = 0 ;
            int j = 1 ;
 
   for(int i = 0 ; i < 2 ; i ++)
   {
    if( sourcecode[i].ToString().ToUpper()[0] >= 65 &&
     sourcecode[i].ToString().ToUpper()[0] <= 70 )
    {
     result += ( sourcecode[i].ToString().ToUpper()[0] - 65 + 10 ) * (int)Math.Pow( 16 , j ) ;
    }
    else
    {
     result += Convert.ToInt32( sourcecode[i].ToString() ) * (int)Math.Pow( 16 , j ) ;
    }

    j -- ;
   }

   return result ;

  } 

posted on 2005-04-25 14:59  rose  阅读(252)  评论(0)    收藏  举报