一个加密算法

using System;

namespace test001
{
 /**

 据说此加密算法每次加密的结果都不同,但解密的结果相同
   对sSource,进行加密、解密操作。
   @param sSource String :操作的字符串
   @param iFlag int :操作类型 1-加密  2-解密
   @return String :加密或解密后的字符串
    **/
 public class password
 {
  public password()
  {
  }
  public static string pWord(string sSource,int iFlag)
  {
   if(sSource==null)
    return null;
   if(sSource.Equals(""))
    return "";
   int li_len,i,li_asc,li_rand,li_head;
            string ls_i,ls_code="";
   if(iFlag ==1)
   {
    li_len=sSource.Length;
    Random rdm1 = new Random(~unchecked((int)DateTime.Now.Ticks));
    li_head=(int)(rdm1.NextDouble()*10);
    if(li_head ==0)
     li_head =1;
    for(i=0; i<li_len; i++)
    {
     Random rdm2 = new Random(~unchecked((int)DateTime.Now.Ticks));
     int rand2 =(int)(rdm2.NextDouble() *94);
     if(rand2 ==0)
      rand2 =1;
     li_rand=rand2+32;
     li_asc=Convert.ToInt32(sSource.ToCharArray(i,1)[0]);
     ls_i=((char)(li_asc -i)).ToString();

     if(li_asc+i+li_head>126)
     {
      if(li_rand%2 ==1)
       li_rand=li_rand+1;
      ls_i=((char)(li_rand)).ToString()+((char)(li_asc -i -li_head)).ToString();
     }
     else
     {
      if(li_rand%2 ==0)
       li_rand=li_rand+1;
      ls_i=((char)(li_rand)).ToString()+((char)(li_asc +i +li_head)).ToString();
     }
     ls_code=ls_code+ls_i;
    }
    Random rdm3 = new Random(~unchecked((int)DateTime.Now.Ticks));
    int rand1 =(int)(rdm3.NextDouble()*9);
    if(rand1 ==0)
     rand1 =1;
    ls_code=((char)(rand1*10+li_head+40)).ToString()+ls_code;
   }
   else
   {
    int li_ret;
    li_len=sSource.Length;
    ls_code="";
    li_ret=Convert.ToInt32(sSource.ToCharArray(0,1)[0]) %10;
    for( i=2;i <li_len; i=i+2)
    {
     li_asc=Convert.ToInt32(sSource.ToCharArray(i,1)[0]);
     if(Convert.ToInt32(sSource.ToCharArray(i - 1,1)[0]) %2 ==0)
     {
      ls_i=((char)(li_asc + (i - 1)/2 + li_ret)).ToString();
     }
     else
     {
      ls_i=((char)(li_asc - (i - 1)/2 - li_ret)).ToString();
     }
     ls_code=ls_code+ls_i;
    }
   }
   return ls_code;
  }
 }
}

posted @ 2009-11-11 11:35  周骏  阅读(230)  评论(0编辑  收藏  举报