一个加密算法
据说此加密算法每次加密的结果都不同,但解密的结果相同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; } }} 出处:http://www.cnblogs.com/tuyile006/archive/2006/07/19/454867.html
--------------------------------------------------------------------------------------------------------
我根据上面的代码做了注释的形式,如下代码:
////// 对sSource,进行加密、解密操作。 /// /// 需要加密或解密的字符串 /// 操作类型 1-加密 2-解密 ///加密或解密后的字符串 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);//产生0-9的随机数 if (li_head == 0) li_head = 1; for (i = 0; i < li_len; i++) { //ASCII可显示字符是从32-126,所以重新构建一个这个之间的数 Random rdm2 = new Random(~unchecked((int)DateTime.Now.Ticks));//可以尝试: Random rdm2 = new Random(~unchecked(rdm1.Next())); int rand2 = (int)(rdm2.NextDouble() * 94);//产生0-93之间的随机数, if (rand2 == 0) rand2 = 1; li_rand = rand2 + 32;//这个数就是32-126之间的数 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();//加密算法生成两个字符,第一关字符是随机生成的偶数可显示ASCII字符,第二个是真正的字符 } else { if (li_rand % 2 == 0) li_rand = li_rand + 1;//设置为奇数 ls_i = ((char)(li_rand)).ToString() + ((char)(li_asc + i + li_head)).ToString();//加密算法生成两个字符,第一关字符是随机生成的奇数可显示ASCII字符,第二个是真正的字符 } 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;//每次参与运算的li_head经过加密后放到密文的第一个字符,并且是可显字符 } 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();//因为sSource中的第一位保存了密钥,从第三位开始所有奇数位才是真正需要解密的数据 } else { ls_i = ((char)(li_asc - (i - 1) / 2 - li_ret)).ToString(); } ls_code = ls_code + ls_i; } } return ls_code; }
上面的就不用我说明了,注释都写清楚了,只是加密后的密文要比明文多一倍+1个字符